sensordb2 e64a8defe5 3.17 | 1 year ago | |
---|---|---|
.. | ||
node_modules | 1 year ago | |
LICENSE | 1 year ago | |
README.md | 1 year ago | |
index.js | 1 year ago | |
package.json | 1 year ago |
Create nested values and any intermediaries using dot notation (
'a.b.c'
) paths.
Install with npm:
$ npm install --save set-value
var set = require('set-value');
set(object, prop, value);
object
{object}: The object to set value
onprop
{string}: The property to set. Dot-notation may be used.value
{any}: The value to set on object[prop]
Updates and returns the given object:
var obj = {};
set(obj, 'a.b.c', 'd');
console.log(obj);
//=> { a: { b: { c: 'd' } } }
Escaping with backslashes
Prevent set-value from splitting on a dot by prefixing it with backslashes:
console.log(set({}, 'a\\.b.c', 'd'));
//=> { 'a.b': { c: 'd' } }
console.log(set({}, 'a\\.b\\.c', 'd'));
//=> { 'a.b.c': 'd' }
Escaping with double-quotes or single-quotes
Wrap double or single quotes around the string, or part of the string, that should not be split by set-value:
console.log(set({}, '"a.b".c', 'd'));
//=> { 'a.b': { c: 'd' } }
console.log(set({}, "'a.b'.c", "d"));
//=> { 'a.b': { c: 'd' } }
console.log(set({}, '"this/is/a/.file.path"', 'd'));
//=> { 'this/is/a/file.path': 'd' }
set-value does not split inside brackets or braces:
console.log(set({}, '[a.b].c', 'd'));
//=> { '[a.b]': { c: 'd' } }
console.log(set({}, "(a.b).c", "d"));
//=> { '(a.b)': { c: 'd' } }
console.log(set({}, "<a.b>.c", "d"));
//=> { '<a.b>': { c: 'd' } }
console.log(set({}, "{a..b}.c", "d"));
//=> { '{a..b}': { c: 'd' } }
If there are any regressions please create a bug report. Thanks!
a.b.c
) to get a nested value from an object. | homepage to get a nested value from an object.")'a.b.c'
) paths. | homepage paths.")Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
| Commits | Contributor | | --- | --- | | 59 | jonschlinkert | | 1 | vadimdemedes | | 1 | wtgtybhertgeghgtwtg |
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on June 21, 2017.