3.0.1 • Published 8 years ago

finboxio-dot-prop v3.0.1

Weekly downloads
9
License
MIT
Repository
github
Last release
8 years ago

dot-prop Build Status

Get, set, or delete a property from a nested object using a dot path MAIN DIFFERENCE FROM sindresorhus/dot-prop is that it will not throw errors when you do get({ a: null }, 'a.b.c')

Install

$ npm install --save dot-prop

Usage

const dotProp = require('dot-prop');

// getter
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> 'unicorn'

dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
//=> undefined

dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
//=> 'unicorn'

// setter
const obj = {foo: {bar: 'a'}};
dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
//=> {foo: {bar: 'b'}}

dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}

// deleter
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
//=> {foo: {}}

obj.foo.bar = {x: 'y', y: 'x'};
dotProp.delete(obj, 'foo.bar.x');
console.log(obj);
//=> {foo: {bar: {y: 'x'}}}

API

get(obj, path)

set(obj, path, value)

delete(obj, path)

obj

Type: object

Object to get, set, or delete the path value.

path

Type: string

Path of the property in the object. Use . for nested objects or \\. to add a . in a key.

value

Type: any

Value to set at path.

License

MIT © Sindre Sorhus