1.0.0 • Published 6 years ago

swift-dot-prop v1.0.0

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

dot-properties Build Status codecov

Get, set, or delete a property from a nested object using a dot path. The module is optimized and extremly fast.

Install

$ npm install dot-properties

Usage

const dp = require('dot-properties');

// Getter
dp.get({name: {first: 'lea'}}, 'name.first');
//=> 'lea'

dp.get({name: {first: 'a'}}, 'name.notDefined.deep');
//=> undefined

dp.get({name: {first: 'a'}}, 'name.notDefined.deep', 'default value');
//=> 'default value'

// Setter
const obj = {foo: {bar: 'a'}};
dp.set(obj, 'name.first', 'lea');
console.log(obj);
//=> {name: {first: 'lea'}}

const person = dp.set({}, 'name.first', 'julia');
console.log(person);
//=> {name: {first: 'name.first'}}

dp.set(obj, 'name.middle', 'marie');
console.log(obj);
//=> {name: {first: 'lea', middle: 'marie'}}

// Has
dp.has({name: {first: 'lea'}}, 'name.first');
//=> true

// Deleter
const obj = {name: {first: 'lea'}};
dotProp.delete(obj, 'name.first');
console.log(obj);
//=> {name: {}}

obj.name = {first: 'lea', middle: 'marie'};
dotProp.delete(obj, 'name.first');
console.log(obj);
//=> {name: {middle: 'marie'}}

API

get(obj, path, defaultValue)

set(obj, path, value)

Returns the object or false on error.

has(obj, path)

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, using . to separate each nested key.

Do not use a . in the key (name).

value

Type: any

Value to set at path.

defaultValue

Type: any

Default value.

License

MIT © Thor Sedeke