Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
set-value
Advanced tools
Package description
The set-value npm package is a utility for setting deeply nested values within an object using dot-path notation or by providing a path array. It is useful for updating values in a mutable way without directly accessing properties, especially when dealing with complex or deeply nested objects.
Set nested values using dot-path
This feature allows you to set a value at a nested path within an object using a string dot-path notation.
const setValue = require('set-value');
const obj = {};
setValue(obj, 'a.b.c', 'value');
console.log(obj); //=> { a: { b: { c: 'value' } } }
Set nested values using array-path
This feature allows you to set a value at a nested path within an object using an array to describe the path.
const setValue = require('set-value');
const obj = {};
setValue(obj, ['a', 'b', 'c'], 'value');
console.log(obj); //=> { a: { b: { c: 'value' } } }
Set multiple values
This feature allows you to set multiple values at different paths within the same object.
const setValue = require('set-value');
const obj = {};
setValue(obj, 'a.b.c', 'value1');
setValue(obj, 'x.y.z', 'value2');
console.log(obj); //=> { a: { b: { c: 'value1' } }, x: { y: { z: 'value2' } } }
lodash.set is a method from the Lodash library that performs a similar function to set-value. It allows setting values at a given path of an object tree. Lodash is a more extensive utility library, and lodash.set is just one of its many functions.
dot-prop is another npm package that allows you to get, set, or delete properties from a nested object using dot-path notation. It is similar to set-value but also provides methods for getting and deleting properties.
deepdash is an extension for Lodash that adds deep object manipulation capabilities. It provides methods for deep setting, getting, and many other operations on nested objects. It is more feature-rich compared to set-value, which focuses solely on setting values.
Readme
Set nested properties on an object using dot notation.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm (requires Node.js >=11.0):
$ npm install --save set-value
Please update to version 3.0.1 or later, a critical bug was fixed in that version.
const set = require('set-value');
const obj = {};
set(obj, 'a.b.c', 'd');
console.log(obj);
//=> { a: { b: { c: 'd' } } }
Signature:
set(object, property_path, value[, options]);
object
{Object}: The object to set value
onpath
{String|Symbol|Array}: The path of the property to set.value
{any}: The value to set on obj[prop]
options
{Object}: See all available optionsYou may pass a string, symbol, or array of strings or symbols. By default, when a string is passed this library will split the string on .
or a custom separator It's useful to pass an array
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' }
Do not split properties that include a /
. By default, set-value assumes that properties with a /
are not intended to be split. This option allows you to disable default behavior.
Note that this option cannot be used if options.separator
is set to /
.
Type: boolean
Default: true
Example
console.log(set({}, 'https://github.com', true));
//=> { 'https://github.com': true }
console.log(set({}, 'https://github.com', true, { preservePaths: false }));
//=> { 'https://github': { com: true } }
Custom separator to use for splitting object paths.
Type: string
Default: .
Example
console.log(set(obj, 'auth/userpass/users/bob', '*****', { separator: '/' }));
//=> { auth: { userpass: { users: { bob: '*****' } } } }
Custom .split()
function to use.
Allows you to update plain object values, instead of overwriting them.
Type: boolean|function
- A custom merge
function may be defined if you need to deep merge. Otherwise, when merge
is true
, a shallow merge will be performed by Object.assign()
.
Default: undefined
Example
const obj = { foo: { bar: { baz: 'qux' } } };
set(obj, 'foo.bar.fez', 'zzz', { merge: true });
//=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } }
Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3.
# deep (194 bytes)
deep-object x 879,975 ops/sec ±0.85% (94 runs sampled)
deep-property x 1,746,617 ops/sec ±0.59% (96 runs sampled)
deephas x 792,449 ops/sec ±0.61% (94 runs sampled)
dot-prop x 1,258,403 ops/sec ±0.81% (93 runs sampled)
dot2val x 2,096,518 ops/sec ±0.71% (94 runs sampled)
es5-dot-prop x 1,627,431 ops/sec ±0.57% (94 runs sampled)
lodash-set x 1,100,116 ops/sec ±0.80% (94 runs sampled)
object-path-set x 1,292,062 ops/sec ±0.56% (91 runs sampled)
object-set x 276,868 ops/sec ±0.60% (92 runs sampled)
set-value x 2,587,076 ops/sec ±0.47% (92 runs sampled)
fastest is set-value (by 210% avg)
# medium (98 bytes)
deep-object x 5,823,296 ops/sec ±0.63% (92 runs sampled)
deep-property x 4,045,998 ops/sec ±0.69% (91 runs sampled)
deephas x 1,237,999 ops/sec ±0.56% (92 runs sampled)
dot-prop x 2,833,082 ops/sec ±0.82% (93 runs sampled)
dot2val x 4,669,511 ops/sec ±0.50% (94 runs sampled)
es5-dot-prop x 3,348,092 ops/sec ±0.71% (92 runs sampled)
lodash-set x 3,051,898 ops/sec ±0.61% (94 runs sampled)
object-path-set x 3,867,590 ops/sec ±0.48% (92 runs sampled)
object-set x 888,369 ops/sec ±0.52% (96 runs sampled)
set-value x 8,209,356 ops/sec ±0.68% (91 runs sampled)
fastest is set-value (by 248% avg)
# shallow (101 bytes)
deep-object x 9,558,659 ops/sec ±0.81% (92 runs sampled)
deep-property x 5,190,792 ops/sec ±0.72% (93 runs sampled)
deephas x 1,395,091 ops/sec ±0.55% (90 runs sampled)
dot-prop x 4,203,222 ops/sec ±0.67% (94 runs sampled)
dot2val x 5,908,936 ops/sec ±0.52% (93 runs sampled)
es5-dot-prop x 4,511,259 ops/sec ±0.65% (94 runs sampled)
lodash-set x 8,624,572 ops/sec ±0.87% (90 runs sampled)
object-path-set x 5,800,121 ops/sec ±0.73% (90 runs sampled)
object-set x 1,629,494 ops/sec ±0.68% (92 runs sampled)
set-value x 13,925,948 ops/sec ±0.68% (89 runs sampled)
fastest is set-value (by 268% avg)
Clone this library into a local directory:
$ git clone https://github.com/jonschlinkert/set-value.git
Then install devDependencies and run benchmarks:
$ npm install && node benchmark
These are just a few of the duplicate libraries on NPM.
this
being used improperly in the methods. I was able to patch it by binding the (plain) object to the methods, but it still fails 17 of 26 unit tests.Others that do the same thing, but use a completely different API
split
function to be passed on the options.If there are any regressions please create a bug report. Thanks!
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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
(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
You might also be interested in these projects:
'a.b.c'
) paths. | homepageCommits | Contributor |
---|---|
83 | jonschlinkert |
4 | doowb |
2 | mbelsky |
1 | dkebler |
1 | GlennKintscher |
1 | petermorlion |
1 | abetomo |
1 | zeidoo |
1 | wtgtybhertgeghgtwtg |
Jon Schlinkert
Copyright © 2021, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 28, 2021.
FAQs
Set nested properties on an object using dot notation.
The npm package set-value receives a total of 11,150,975 weekly downloads. As such, set-value popularity was classified as popular.
We found that set-value demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.