0.1.3 • Published 6 years ago

object-take v0.1.3

Weekly downloads
18
License
MIT
Repository
github
Last release
6 years ago

object-take

Build Status

A really small library, almost 150 bytes minified and gzipped, to take value from object using a string for path expression or returns a placeholder if value is undefined or unreachable.

It's approach is based on idx, from Facebook, and a insight from Vue.js meetup where @IgorHalfeld shows a way to get a property using eval. Yeah, eval.

Install

This module is published under NPM registry, so you can install from any package manager.

npm install --save object-take

# If you're using Yarn use the command below.
yarn add object-take

Usage

Just import take function and take a value from object using a path expression.

import take from 'object-take';

// ...

const name = take(response, 'data.user.name', 'Unknown');

You can use brackets, even on first property.

const title = take(paragraphs, '["data"].children[0].textContent');

Also, expressions could be attached to path.

const id = take(response, 'data.keys[0].number.toString(16)');

License

Released under the MIT License.