1.2.2 • Published 3 years ago

deepref v1.2.2

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

DeepRef

Write to a deeply nested reference within an object.

Installation

npm install deepref --save

Usage

var deep = require('deepref');

var obj = {};

deep.set(obj, 'a', 'obj.a is set to this');

console.dir(obj);
// { a: 'obj.a is set to this' }

deep.set(obj, 'b.c', 'we can set nested fields');

console.dir(obj);
/*
* { a: 'obj.a is set to this',
*   b: { c: 'we can set nested fields' } }
*/

deep.set(obj, 'd.0', 'we can even..');
deep.set(obj, 'd.1', '..create arrays..');
deep.set(obj, 'd.2', '..by using an integer as the index');

console.dir(obj);
/*
 *{ a: 'obj.a is set to this',
 *  b: { c: 'we can set nested fields' },
 *  d:
 *   [ 'we can even..',
 *     '..create arrays..',
 *     '..by using an integer as the index' ] }
*/

Decorate

Use deep.decorate(obj) to give the object the 'setKey' method, which works the same as deep.set

var obj = {};

deep.decorate(obj);

obj.setKey('a.b.c', 123);

console.log(obj.a.b.c);
// Now set to to 123;

Use deep.undecorate(obj) to remove 'setKey'

var obj = {};

deep.decorate(obj);

obj.setKey('a.b.c', 123);

deep.undecorate(obj);

console.log(typeof obj.setKey);
// obj.setKey is now undefined

Tests

npm test

Contributing

Please use the included style guide. If you change anything, please test and add unit tests for any new functionality. If you're fixing a bug, please add a unit test that would have failed before the bug was fixed. Thanks!

1.2.2

3 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago