deep
🐡 Get, set, remove, and test for deeply nested properties
Helps you safely work with nested properties.
Note: set()
and remove()
modify the passed-in object rather than creating a
copy. If you'd rather return a new object each time, there are several other
solutions (unchanged is really good).
Install
Using Yarn:
$ yarn add @blakek/deep
…or using npm:
$ npm i --save @blakek/deep
Usage
import { get, getOr, has, remove, set } from '@blakek/deep';
const user = {
id: 'abf87de',
roles: ['alert:create', 'alert:read'],
sites: {
github: {
username: 'blakek'
}
}
};
get('sites.github.username', user);
const githubUsername = get('sites.github.username');
githubUsername(user);
getOr('no-account', 'sites.facebook.username', user);
has('sites.github', user);
remove('a', { a: 42, b: 123 });
set(123, 'a.b.c', { a: 42 });
API
For all these:
path
can be either a dot-notation string or array of path parts- arguments can be partially applied
get
function get(path: Path, object: any): any;
Gets the value for a given path with an optional fallback value.
const user = {
id: 'abf87de',
roles: ['alert:create', 'alert:read'],
sites: {
github: {
username: 'blakek'
}
}
};
get('id', user);
get('roles.0', user);
get('roles[0]', user);
get(['roles', 1], user);
get('sites.github.username', user);
const getID = get('id');
getID(user);
getOr
function getOr(defaultValue: any, path: Path, object: any): any;
Like get
, gets a value from an object. Will return a fallback other than
undefined
if the value was not found equal to undefined
.
const user = {
id: 'abf87de',
roles: ['alert:create', 'alert:read'],
sites: {
github: {
username: 'blakek'
}
}
};
getOr('/images/placeholder.png', 'sites.github.image', user);
const getRoles = getOr([], 'roles');
getRoles(user);
getRoles({});
has
function has(path: Path, object: any): boolean;
Returns true
if a value was found at the given path or false
if nothing was
found.
const product = {
id: 'abf87de',
name: 'Logo T-Shirt',
attributes: {
isCool: undefined,
materials: ['cotton']
}
};
has('attributes.materials', product);
has(['avability', 'sizes'], product);
has('attributes.isCool', product);
getOr(false, 'attributes.isCool', product);
remove
function remove(path: Path, object: any): any;
Removes a value at a path and returns the object.
const user = {
username: 'blakek',
password: 'wouldntyouliketoknow'
};
remove('password', user);
remove('property.does.not.exist', user);
set
function set(value: any, path: Path, object: any): any;
Sets a value at a path and returns the object.
const user = {
profile: {
bgColor: '#639'
}
};
set('tomato', 'profile.bgColor', user);
set('/images/user.png', 'profile.bgImage', user);
const logout = set(null, 'profile');
logout(user);
Contributing
Node.js and Yarn are required to work with this project.
To install all dependencies, run:
yarn
Useful Commands
| |
---|
yarn build | Builds the project to ./dist |
yarn format | Format the source following the Prettier styles |
yarn test | Run project tests |
yarn test --watch | Run project tests, watching for file changes |
License
MIT