Skip to content
/ get Public

🚚 A really small and type-safe (requires TypeScript >= 4.1.3) function, that gets a nested value from an object using a path string (like "a.b[0].d"). If value is 'undefined' or unreachable returns the placeholder instead.

License

Notifications You must be signed in to change notification settings

VitorLuizC/get

Repository files navigation

@bitty/get

A really small library, almost 150 bytes minified and gzipped, that gets a value from object using a path expression, and returns received placeholder when value is undefinedss or unreachable.

It's approach is based on idx, from Facebook, and a insight from Vue.js meetup where @IgorHalfeld shows a way to access 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 @bitty/get

# If you're using Yarn use the command below.
yarn add @bitty/get

Usage

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

import get from '@bitty/get';

// ...

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

You can use brackets, even on first property.

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

Also, expressions could be attached to path.

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

License

Released under the MIT License.

About

🚚 A really small and type-safe (requires TypeScript >= 4.1.3) function, that gets a nested value from an object using a path string (like "a.b[0].d"). If value is 'undefined' or unreachable returns the placeholder instead.

Topics

Resources

License

Stars

Watchers

Forks