๐ก 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).
Using Yarn:
$ yarn add @blakek/deepโฆor using npm:
$ npm i --save @blakek/deepimport { get, getOr, has, pluck remove, set } from '@blakek/deep';
const user = {
id: 'abf87de',
roles: ['alert:create', 'alert:read'],
sites: {
github: {
username: 'blakek'
}
}
};
// Get a property value
get('sites.github.username', user); //ยป 'blakek'
// Arguments can be partially applied
const githubUsername = get('sites.github.username');
githubUsername(user); //ยป 'blakek'
// Get a property value with a fallback other than `undefined`
getOr('no-account', 'sites.facebook.username', user); //ยป 'no-account'
// Test for a property value
has('sites.github', user); //ยป true
// Pluck a subset of properties
pluck(['id', 'roles'], user);
//ยป { id: 'abf87de', roles: [ 'alert:create', 'alert:read' ] }
// Remove a property value
remove('a', { a: 42, b: 123 }); //ยป { b: 123 }
// Set a property value
set(123, 'a.b.c', { a: 42 }); //ยป { a: { b: { c: 123 } } }For all these:
pathcan be either a dot-notation string or array of path parts- arguments can be partially applied
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); //ยป 'abf87de'
get('roles.0', user); //ยป 'alert:create'
get('roles[0]', user); //ยป 'alert:create'
get(['roles', 1], user); //ยป 'alert:read'
get('sites.github.username', user); //ยป 'blakek'
const getID = get('id');
getID(user); //ยป 'abf87de'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); //ยป '/images/placeholder.png'
const getRoles = getOr([], 'roles');
getRoles(user); //ยป ['alert:create', 'alert:read']
getRoles({}); //ยป []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); //ยป true
has(['avability', 'sizes'], product); //ยป false
has('attributes.isCool', product); //ยป true; property exists but is undefined
// `get()` should be used if you want to ensure a value is not `undefined`
getOr(false, 'attributes.isCool', product); //ยป falsefunction 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); //ยป { username: 'blakek' }
remove('property.does.not.exist', user);
//ยป { username: 'blakek' } (same object from previous line)function pluck(properties: Path[], object: any): any;Gets a subset of properties from an object.
const user = {
username: 'blakek',
roles: ['alert:create', 'alert:read'],
sites: {
github: {
username: 'blakek'
}
}
};
pluck(['username'], user); //ยป { username: 'blakek' }
pluck(['username', 'roles'], user);
//ยป { username: 'blakek', roles: [ 'alert:create', 'alert:read' ] }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); //ยป { profile: { bgColor: 'tomato' } }
set('/images/user.png', 'profile.bgImage', user);
//ยป { profile: { bgColor: 'tomato', bgImage: '/images/user.png' } }
const logout = set(null, 'profile');
logout(user); //ยป { profile: null }Node.js and Yarn are required to work with this project.
To install all dependencies, run:
yarnyarn 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 |
MIT