pax_global_header00006660000000000000000000000064141173417020014512gustar00rootroot0000000000000052 comment=c574eb8d94ba21d22b6fe5a5741fd4dbfc9dc47a set-value-4.1.0/000077500000000000000000000000001411734170200134215ustar00rootroot00000000000000set-value-4.1.0/.editorconfig000066400000000000000000000002561411734170200161010ustar00rootroot00000000000000# http://editorconfig.org/ root = true [*] charset = utf-8 end_of_line = lf indent_size = 2 indent_style = space insert_final_newline = true trim_trailing_whitespace = true set-value-4.1.0/.eslintrc.json000066400000000000000000000075121411734170200162220ustar00rootroot00000000000000{ "extends": [ "eslint:recommended" ], "env": { "es2021": true, "node": true }, "parserOptions": { "ecmaVersion": 12 }, "rules": { "accessor-pairs": 2, "arrow-parens": [2, "as-needed"], "arrow-spacing": [2, { "before": true, "after": true }], "block-spacing": [2, "always"], "brace-style": [2, "1tbs", { "allowSingleLine": true }], "comma-dangle": [2, "never"], "comma-spacing": [2, { "before": false, "after": true }], "comma-style": [2, "last"], "constructor-super": 2, "curly": [2, "multi-line"], "dot-location": [2, "property"], "eol-last": 2, "eqeqeq": [2, "allow-null"], "generator-star-spacing": [2, { "before": true, "after": true }], "handle-callback-err": [2, "^(err|error)$"], "indent": [2, 2, { "SwitchCase": 1 }], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "keyword-spacing": [2, { "before": true, "after": true }], "new-cap": [2, { "newIsCap": true, "capIsNew": false }], "new-parens": 2, "no-array-constructor": 2, "no-caller": 2, "no-class-assign": 2, "no-cond-assign": 2, "no-const-assign": 2, "no-control-regex": 2, "no-debugger": 2, "no-delete-var": 2, "no-dupe-args": 2, "no-dupe-class-members": 2, "no-dupe-keys": 2, "no-duplicate-case": 2, "no-empty-character-class": 2, "no-eval": 2, "no-ex-assign": 2, "no-extend-native": 2, "no-extra-bind": 2, "no-extra-boolean-cast": 2, "no-extra-parens": [2, "functions"], "no-fallthrough": 2, "no-floating-decimal": 2, "no-func-assign": 2, "no-implied-eval": 2, "no-implicit-coercion": 2, "no-inner-declarations": [2, "functions"], "no-invalid-regexp": 2, "no-irregular-whitespace": 2, "no-iterator": 2, "no-label-var": 2, "no-labels": 2, "no-lone-blocks": 2, "no-lonely-if": 2, "no-mixed-spaces-and-tabs": 2, "no-multi-spaces": 0, "no-multi-str": 2, "no-multiple-empty-lines": [2, { "max": 1 }], "no-native-reassign": 2, "no-negated-in-lhs": 2, "no-new": 2, "no-new-func": 2, "no-new-object": 2, "no-new-require": 2, "no-new-wrappers": 2, "no-obj-calls": 2, "no-octal": 2, "no-octal-escape": 2, "no-proto": 2, "no-redeclare": 2, "no-regex-spaces": 2, "no-return-assign": 2, "no-self-compare": 2, "no-sequences": 2, "no-shadow-restricted-names": 2, "no-spaced-func": 2, "no-sparse-arrays": 2, "no-this-before-super": 2, "no-throw-literal": 2, "no-trailing-spaces": 2, "no-undef": 2, "no-undef-init": 2, "no-unexpected-multiline": 2, "no-unneeded-ternary": [2, { "defaultAssignment": false }], "no-unreachable": 2, "no-unused-expressions": 2, "no-unused-vars": [2, { "vars": "all", "args": "none" }], "no-useless-call": 2, "no-with": 2, "object-curly-spacing": ["error", "always", { "objectsInObjects": true }], "one-var": [2, { "initialized": "never" }], "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], "padded-blocks": [0, "never"], "prefer-const": [2, { "destructuring": "all", "ignoreReadBeforeAssign": false }], "quotes": [2, "single", "avoid-escape"], "radix": 2, "semi": [2, "always"], "semi-spacing": [2, { "before": false, "after": true }], "space-before-blocks": [2, "always"], "space-before-function-paren": [2, { "anonymous": "never", "named": "never", "asyncArrow": "always" }], "space-in-parens": [2, "never"], "space-infix-ops": 2, "space-unary-ops": [2, { "words": true, "nonwords": false }], "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], "strict": 2, "use-isnan": 2, "valid-typeof": 2, "wrap-iife": [2, "any"], "yoda": [2, "never"] } } set-value-4.1.0/.gitattributes000066400000000000000000000001771411734170200163210ustar00rootroot00000000000000# Enforce Unix newlines * text eol=lf # binaries *.ai binary *.psd binary *.jpg binary *.gif binary *.png binary *.jpeg binaryset-value-4.1.0/.github/000077500000000000000000000000001411734170200147615ustar00rootroot00000000000000set-value-4.1.0/.github/FUNDING.yml000066400000000000000000000001161411734170200165740ustar00rootroot00000000000000# These are supported funding model platforms github: [jonschlinkert, doowb] set-value-4.1.0/.github/workflows/000077500000000000000000000000001411734170200170165ustar00rootroot00000000000000set-value-4.1.0/.github/workflows/main.yml000066400000000000000000000007601411734170200204700ustar00rootroot00000000000000name: Tests on: [push, pull_request] jobs: test: name: Node.js ${{ matrix.node-version }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] node-version: [11, 12, 13, 14, 15, 16] steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test set-value-4.1.0/.gitignore000066400000000000000000000004561411734170200154160ustar00rootroot00000000000000# always ignore files *.DS_Store .idea .vscode *.sublime-* # test related, or directories generated by tests test/actual actual coverage .nyc* # npm node_modules npm-debug.log # yarn yarn.lock yarn-error.log # misc _gh_pages _draft _drafts bower_components vendor temp tmp TODO.md package-lock.jsonset-value-4.1.0/.verb.md000066400000000000000000000111771411734170200147660ustar00rootroot00000000000000## Heads up! [Please update][update] to version 3.0.1 or later, a critical bug was fixed in that version. ## Usage ```js const set = require('{%= name %}'); const obj = {}; set(obj, 'a.b.c', 'd'); console.log(obj); //=> { a: { b: { c: 'd' } } } ``` ### Params Signature: ```js set(object, property_path, value[, options]); ``` - `object` **{Object}**: The object to set `value` on - `path` **{String|Symbol|Array}**: The [path](#object-paths) of the property to set. - `value` **{any}**: The value to set on `obj[prop]` - `options` **{Object}**: See all [available options](#options) ### Object paths You 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](#options-separator) It's useful to pass an array ### Escaping **Escaping with backslashes** Prevent set-value from splitting on a dot by prefixing it with backslashes: ```js console.log(set({}, 'a\\.b.c', 'd')); //=> { 'a.b': { c: 'd' } } console.log(set({}, 'a\\.b\\.c', 'd')); //=> { 'a.b.c': 'd' } ``` ## Options ### options.preservePaths 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** ```js console.log(set({}, 'https://github.com', true)); //=> { 'https://github.com': true } console.log(set({}, 'https://github.com', true, { preservePaths: false })); //=> { 'https://github': { com: true } } ``` ### options.separator Custom separator to use for splitting object paths. **Type**: `string` **Default**: `.` **Example** ```js console.log(set(obj, 'auth/userpass/users/bob', '*****', { separator: '/' })); //=> { auth: { userpass: { users: { bob: '*****' } } } } ``` ### options.split Custom `.split()` function to use. ### options.merge 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** ```js const obj = { foo: { bar: { baz: 'qux' } } }; set(obj, 'foo.bar.fez', 'zzz', { merge: true }); //=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } } ``` ## Benchmarks Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3. ``` {%= include("./benchmark/stats.md") %} ``` ### Running the benchmarks Clone this library into a local directory: ```sh $ git clone https://github.com/jonschlinkert/set-value.git ``` Then install devDependencies and run benchmarks: ```sh $ npm install && node benchmark ``` ## Comparisons to other libs, or _"the list of shame"_ These are just a few of the duplicate libraries on NPM. - [bury][] fails all of the tests. I even wrapped it to have it return the object instead of the value, but with all of that work it still fails the vast majority of tests. - [deep-get-set][] fails 22 of 26 unit tests. - [deep-object][] fails 25 of 26 unit tests, completely butchered given objects. - [deep-property][] fails 17 of 26 unit tests. - [deep-set][] fails 13 of 26 unit tests. - [deephas][] fails 17 of 26 unit tests. - [dot-prop][] fails 9 of 26 unit tests. - [dot2val][] fails 17 of 26 unit tests. - [es5-dot-prop][] fails 15 of 26 unit tests. - [getsetdeep][] fails all unit tests due to `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. - [lodash.set][] fails 11 of 26 unit tests. - [object-path-set][] fails 12 of 26 unit tests. - [object-path][] fails 16 of 26 unit tests. - [object-set][] fails 13 of 26 unit tests. - [set-nested-prop][] fails 24 of 26 unit tests. - [setvalue][] (this library is almost identical to a previous version of this library) - Many dozens of others **Others that do the same thing, but use a completely different API** - [deep-set-in][] - [set-deep][] - [set-deep-prop][] - [bury][] - Many dozens of others ## History ### v3.0.0 - Added support for a custom `split` function to be passed on the options. - Removed support for splitting on brackets, since a [custom function][split-string] can be passed to do this now. ### v2.0.0 - Adds support for escaping with double or single quotes. See [escaping](#escaping) for examples. - Will no longer split inside brackets or braces. See [bracket support](#bracket-support) for examples. If there are any regressions please create a [bug report](../../issues/new). Thanks! set-value-4.1.0/LICENSE000066400000000000000000000021031411734170200144220ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014-present, Jon Schlinkert. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. set-value-4.1.0/README.md000066400000000000000000000314721411734170200147070ustar00rootroot00000000000000# set-value [![NPM version](https://img.shields.io/npm/v/set-value.svg?style=flat)](https://www.npmjs.com/package/set-value) [![NPM monthly downloads](https://img.shields.io/npm/dm/set-value.svg?style=flat)](https://npmjs.org/package/set-value) [![NPM total downloads](https://img.shields.io/npm/dt/set-value.svg?style=flat)](https://npmjs.org/package/set-value) [![Tests](https://github.com/jonschlinkert/set-value/actions/workflows/main.yml/badge.svg)](https://github.com/jonschlinkert/set-value/actions/workflows/main.yml) > Set nested properties on an object using dot notation. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. ## Install Install with [npm](https://www.npmjs.com/) (requires [Node.js](https://nodejs.org/en/) >=11.0): ```sh $ npm install --save set-value ``` ## Heads up! [Please update](https://github.com/update/update) to version 3.0.1 or later, a critical bug was fixed in that version. ## Usage ```js const set = require('set-value'); const obj = {}; set(obj, 'a.b.c', 'd'); console.log(obj); //=> { a: { b: { c: 'd' } } } ``` ### Params Signature: ```js set(object, property_path, value[, options]); ``` * `object` **{Object}**: The object to set `value` on * `path` **{String|Symbol|Array}**: The [path](#object-paths) of the property to set. * `value` **{any}**: The value to set on `obj[prop]` * `options` **{Object}**: See all [available options](#options) ### Object paths You 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](#options-separator) It's useful to pass an array ### Escaping **Escaping with backslashes** Prevent set-value from splitting on a dot by prefixing it with backslashes: ```js console.log(set({}, 'a\\.b.c', 'd')); //=> { 'a.b': { c: 'd' } } console.log(set({}, 'a\\.b\\.c', 'd')); //=> { 'a.b.c': 'd' } ``` ## Options ### options.preservePaths 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** ```js console.log(set({}, 'https://github.com', true)); //=> { 'https://github.com': true } console.log(set({}, 'https://github.com', true, { preservePaths: false })); //=> { 'https://github': { com: true } } ``` ### options.separator Custom separator to use for splitting object paths. **Type**: `string` **Default**: `.` **Example** ```js console.log(set(obj, 'auth/userpass/users/bob', '*****', { separator: '/' })); //=> { auth: { userpass: { users: { bob: '*****' } } } } ``` ### options.split Custom `.split()` function to use. ### options.merge 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** ```js const obj = { foo: { bar: { baz: 'qux' } } }; set(obj, 'foo.bar.fez', 'zzz', { merge: true }); //=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } } ``` ## Benchmarks Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3. ``` # deep (194 bytes) deep-object x 823,287 ops/sec ±1.00% (90 runs sampled) deep-property x 1,787,990 ops/sec ±0.82% (92 runs sampled) deephas x 840,700 ops/sec ±0.95% (93 runs sampled) dot-prop x 1,249,663 ops/sec ±0.89% (90 runs sampled) dot2val x 2,067,212 ops/sec ±1.08% (91 runs sampled) es5-dot-prop x 1,668,806 ops/sec ±0.92% (92 runs sampled) lodash-set x 1,286,725 ops/sec ±0.82% (90 runs sampled) object-path-set x 1,261,242 ops/sec ±1.63% (90 runs sampled) object-set x 285,369 ops/sec ±0.91% (90 runs sampled) set-value x 2,076,931 ops/sec ±0.86% (93 runs sampled) fastest is set-value, dot2val (by 203% avg) # medium (98 bytes) deep-object x 5,811,161 ops/sec ±1.12% (90 runs sampled) deep-property x 4,075,885 ops/sec ±0.91% (90 runs sampled) deephas x 1,508,136 ops/sec ±0.82% (92 runs sampled) dot-prop x 2,809,838 ops/sec ±1.16% (87 runs sampled) dot2val x 4,600,890 ops/sec ±0.76% (91 runs sampled) es5-dot-prop x 3,263,790 ops/sec ±0.97% (91 runs sampled) lodash-set x 3,486,628 ops/sec ±1.20% (90 runs sampled) object-path-set x 3,729,018 ops/sec ±0.90% (92 runs sampled) object-set x 973,961 ops/sec ±0.80% (92 runs sampled) set-value x 6,941,474 ops/sec ±1.24% (90 runs sampled) fastest is set-value (by 206% avg) # shallow (101 bytes) deep-object x 9,416,410 ops/sec ±1.19% (89 runs sampled) deep-property x 5,108,536 ops/sec ±0.98% (93 runs sampled) deephas x 1,706,979 ops/sec ±0.98% (86 runs sampled) dot-prop x 4,045,902 ops/sec ±1.10% (92 runs sampled) dot2val x 5,862,418 ops/sec ±0.88% (91 runs sampled) es5-dot-prop x 4,439,646 ops/sec ±1.18% (90 runs sampled) lodash-set x 9,303,292 ops/sec ±1.19% (89 runs sampled) object-path-set x 5,657,479 ops/sec ±0.95% (93 runs sampled) object-set x 2,020,041 ops/sec ±0.92% (91 runs sampled) set-value x 11,272,227 ops/sec ±1.36% (88 runs sampled) fastest is set-value (by 213% avg) ``` ### Running the benchmarks Clone this library into a local directory: ```sh $ git clone https://github.com/jonschlinkert/set-value.git ``` Then install devDependencies and run benchmarks: ```sh $ npm install && node benchmark ``` ## Comparisons to other libs, or _"the list of shame"_ These are just a few of the duplicate libraries on NPM. * [bury](https://github.com/kalmbach/bury) fails all of the tests. I even wrapped it to have it return the object instead of the value, but with all of that work it still fails the vast majority of tests. * [deep-get-set](https://github.com/acstll/deep-get-set) fails 22 of 26 unit tests. * [deep-object](https://github.com/ayushgp/deep-object) fails 25 of 26 unit tests, completely butchered given objects. * [deep-property](https://github.com/mikattack/node-deep-property) fails 17 of 26 unit tests. * [deep-set](https://github.com/klaemo/deep-set) fails 13 of 26 unit tests. * [deephas](https://github.com/sharpred/deepHas) fails 17 of 26 unit tests. * [dot-prop](https://github.com/sindresorhus/dot-prop) fails 9 of 26 unit tests. * [dot2val](https://github.com/yangg/dot2val) fails 17 of 26 unit tests. * [es5-dot-prop](https://github.com/sindresorhus/dot-prop) fails 15 of 26 unit tests. * [getsetdeep](https://github.com/bevry/getsetdeep) fails all unit tests due to `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. * [lodash.set](https://lodash.com/) fails 11 of 26 unit tests. * [object-path-set](https://github.com/skratchdot/object-path-set) fails 12 of 26 unit tests. * [object-path](https://github.com/mariocasciaro/object-path) fails 16 of 26 unit tests. * [object-set](https://github.com/gearcase/object-set) fails 13 of 26 unit tests. * [set-nested-prop](https://github.com/tiaanduplessis/set-nested-prop) fails 24 of 26 unit tests. * [setvalue](https://github.com/blakeembrey/setvalue) (this library is almost identical to a previous version of this library) * Many dozens of others **Others that do the same thing, but use a completely different API** * [deep-set-in](https://github.com/KulikovskyIgor/deep-set-in) * [set-deep](https://github.com/radubrehar/set-deep) * [set-deep-prop](https://github.com/mmckelvy/set-deep-prop) * [bury](https://github.com/kalmbach/bury) * Many dozens of others ## History ### v3.0.0 * Added support for a custom `split` function to be passed on the options. * Removed support for splitting on brackets, since a [custom function](https://github.com/jonschlinkert/split-string) can be passed to do this now. ### v2.0.0 * Adds support for escaping with double or single quotes. See [escaping](#escaping) for examples. * Will no longer split inside brackets or braces. See [bracket support](#bracket-support) for examples. If there are any regressions please create a [bug report](../../issues/new). Thanks! ## About
Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
Running Tests 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: ```sh $ npm install && npm test ```
Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ```
### Related projects You might also be interested in these projects: * [assign-value](https://www.npmjs.com/package/assign-value): Assign a value or extend a deeply nested property of an object using object path… [more](https://github.com/jonschlinkert/assign-value) | [homepage](https://github.com/jonschlinkert/assign-value "Assign a value or extend a deeply nested property of an object using object path notation.") * [get-value](https://www.npmjs.com/package/get-value): Use property paths like 'a.b.c' to get a nested value from an object. Even works… [more](https://github.com/jonschlinkert/get-value) | [homepage](https://github.com/jonschlinkert/get-value "Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).") * [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.") * [merge-value](https://www.npmjs.com/package/merge-value): Similar to assign-value but deeply merges object values or nested values using object path/dot notation. | [homepage](https://github.com/jonschlinkert/merge-value "Similar to assign-value but deeply merges object values or nested values using object path/dot notation.") * [omit-value](https://www.npmjs.com/package/omit-value): Omit properties from an object or deeply nested property of an object using object path… [more](https://github.com/jonschlinkert/omit-value) | [homepage](https://github.com/jonschlinkert/omit-value "Omit properties from an object or deeply nested property of an object using object path notation.") * [set-value](https://www.npmjs.com/package/set-value): Set nested properties on an object using dot notation. | [homepage](https://github.com/jonschlinkert/set-value "Set nested properties on an object using dot notation.") * [union-value](https://www.npmjs.com/package/union-value): Set an array of unique values as the property of an object. Supports setting deeply… [more](https://github.com/jonschlinkert/union-value) | [homepage](https://github.com/jonschlinkert/union-value "Set an array of unique values as the property of an object. Supports setting deeply nested properties using using object-paths/dot notation.") * [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value "Delete nested properties from an object using dot notation.") ### Contributors | **Commits** | **Contributor** | | --- | --- | | 87 | [jonschlinkert](https://github.com/jonschlinkert) | | 4 | [doowb](https://github.com/doowb) | | 2 | [mbelsky](https://github.com/mbelsky) | | 1 | [dkebler](https://github.com/dkebler) | | 1 | [GlennKintscher](https://github.com/GlennKintscher) | | 1 | [petermorlion](https://github.com/petermorlion) | | 1 | [abetomo](https://github.com/abetomo) | | 1 | [zeidoo](https://github.com/zeidoo) | | 1 | [ready-research](https://github.com/ready-research) | | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | ### Author **Jon Schlinkert** * [GitHub Profile](https://github.com/jonschlinkert) * [Twitter Profile](https://twitter.com/jonschlinkert) * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) ### License Copyright © 2021, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on September 12, 2021._set-value-4.1.0/benchmark/000077500000000000000000000000001411734170200153535ustar00rootroot00000000000000set-value-4.1.0/benchmark/code/000077500000000000000000000000001411734170200162655ustar00rootroot00000000000000set-value-4.1.0/benchmark/code/deep-object.js000066400000000000000000000002761411734170200210110ustar00rootroot00000000000000'use strict'; const { set } = require('deep-object'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/deep-property.js000066400000000000000000000003001411734170200214130ustar00rootroot00000000000000'use strict'; const { set } = require('deep-property'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/deephas.js000066400000000000000000000002711411734170200202340ustar00rootroot00000000000000'use strict'; const { set } = require('deephas'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/dot-prop.js000066400000000000000000000000711411734170200203650ustar00rootroot00000000000000'use strict'; module.exports = require('dot-prop').set; set-value-4.1.0/benchmark/code/dot2val.js000066400000000000000000000002721411734170200201770ustar00rootroot00000000000000'use strict'; const { set } = require('dot2val'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/es5-dot-prop.js000066400000000000000000000002771411734170200210670ustar00rootroot00000000000000'use strict'; const { set } = require('es5-dot-prop'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/lodash-set.js000066400000000000000000000000671411734170200206710ustar00rootroot00000000000000'use strict'; module.exports = require('lodash.set'); set-value-4.1.0/benchmark/code/object-path-set.js000066400000000000000000000002761411734170200216210ustar00rootroot00000000000000'use strict'; const set = require('object-path-set'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/object-set.js000066400000000000000000000002711411734170200206620ustar00rootroot00000000000000'use strict'; const set = require('object-set'); module.exports = (obj, path, value) => { try { set(obj, path, value); } catch (err) { // do nothing } return obj; }; set-value-4.1.0/benchmark/code/set-value.js000066400000000000000000000001521411734170200205260ustar00rootroot00000000000000'use strict'; const set = require('../..'); module.exports = (obj, key, value) => set(obj, key, value); set-value-4.1.0/benchmark/fixtures/000077500000000000000000000000001411734170200172245ustar00rootroot00000000000000set-value-4.1.0/benchmark/fixtures/deep.js000066400000000000000000000003021411734170200204720ustar00rootroot00000000000000/** * Deeply nested values */ module.exports = [{}, 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o', 'LAST VALUE!', {a: {b: {c: {d: {e: {f: {g: {h: {i: {j: {k: {l: {m: {n: {o: 'LAST VALUE!'}}}}}}}}}}}}}}}]; set-value-4.1.0/benchmark/fixtures/medium.js000066400000000000000000000001421411734170200210370ustar00rootroot00000000000000/** * Deeply nested values */ module.exports = [{}, 'x.y.z', 'foooo', {x: {y: {z: 'foooo'}}}]; set-value-4.1.0/benchmark/fixtures/shallow.js000066400000000000000000000001451411734170200212330ustar00rootroot00000000000000/** * Deeply nested values */ module.exports = [{}, 'foo', {i: {k: 'L'}}, { foo: {i: {k: 'L'}}}]; set-value-4.1.0/benchmark/index.js000066400000000000000000000007331411734170200170230ustar00rootroot00000000000000'use strict'; const path = require('path'); const argv = require('minimist')(process.argv.slice(2)); const bench = require('benchmarked'); const write = require('write'); bench.run({ fixtures: 'fixtures/*.js', code: 'code/*.js', dry: Boolean(argv['dry-run']) }) .then(function(stats) { write.sync(path.join(__dirname, 'stats.json'), JSON.stringify(stats, null, 2)); write.sync(path.join(__dirname, 'stats.md'), bench.render(stats)); }) .catch(console.error); set-value-4.1.0/benchmark/package.json000066400000000000000000000010551411734170200176420ustar00rootroot00000000000000{ "name": "set-value-benchmarks", "description": "Node.js library and API for set-value-benchmarks", "version": "0.0.0", "private": true, "dependencies": { "benchmarked": "^2.0.0", "minimist": "^1.2.5", "write": "^2.0.0" }, "devDependencies": { "deep-object": "^1.0.0", "deep-property": "^1.1.0", "deep-set": "^1.0.1", "deephas": "^1.0.7", "dot-prop": "^6.0.1", "dot2val": "^1.2.2", "es5-dot-prop": "^4.1.1", "lodash.set": "^4.3.2", "object-path-set": "^1.0.1", "object-set": "^1.0.1" } } set-value-4.1.0/benchmark/stats.json000066400000000000000000000467701411734170200174220ustar00rootroot00000000000000[ { "name": "deep", "file": { "stat": null, "_contents": null, "history": [ "/Users/jonschlinkert/dev/js-utils/objects/set-value/benchmark/fixtures/deep.js" ], "_cwd": "/Users/jonschlinkert/dev/js-utils/objects/set-value", "_isVinyl": true, "_symlink": null, "key": "deep", "content": [ { "a": { "b": { "c": { "d": { "e": { "f": { "g": { "h": { "i": { "j": { "k": { "l": { "m": { "n": { "o": "LAST VALUE!" } } } } } } } } } } } } } } }, "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o", "LAST VALUE!", { "a": { "b": { "c": { "d": { "e": { "f": { "g": { "h": { "i": { "j": { "k": { "l": { "m": { "n": { "o": "LAST VALUE!" } } } } } } } } } } } } } } } ], "bytes": "(194 bytes)", "suite": { "0": { "name": "set-value", "options": { "async": false, "defer": false, "delay": 0.005, "initCount": 1, "maxTime": 5, "minSamples": 5, "minTime": 0.05 }, "async": false, "defer": false, "delay": 0.005, "initCount": 1, "maxTime": 5, "minSamples": 5, "minTime": 0.05, "events": { "abort": [ null ], "complete": [ null ], "cycle": [ null ], "error": [ null ] }, "id": 1, "stats": { "moe": 3.810901178799246e-9, "rme": 0.8959004252713377, "sem": 1.9443373361220644e-9, "deviation": 1.8342841743328754e-8, "mean": 4.253710648306762e-7, "sample": [ 4.124695904213662e-7, 4.7009577019029703e-7, 4.1375394601926384e-7, 4.187159673499706e-7, 4.29260489750442e-7, 4.542271292356631e-7, 4.458841263049112e-7, 4.181324893629046e-7, 4.08162167273415e-7, 4.405099364884245e-7, 4.0496943406931114e-7, 4.196227443934881e-7, 4.533282902305643e-7, 4.7281762879611684e-7, 4.2752410445526614e-7, 4.5114443761524276e-7, 3.964522638803524e-7, 4.3548496524986995e-7, 4.2174224228956864e-7, 4.0180679400501156e-7, 4.411657841236821e-7, 4.0904827982916487e-7, 4.2758062660551904e-7, 4.062287519896617e-7, 4.2483961357225033e-7, 3.958674530755047e-7, 4.2714552503427734e-7, 4.024448804627047e-7, 4.136043449482294e-7, 4.388891699368036e-7, 4.194249286873749e-7, 4.4161789828692103e-7, 4.1910239074590645e-7, 4.277524624525239e-7, 4.0223467763541515e-7, 4.408363670748428e-7, 4.042942650465699e-7, 4.049482766772257e-7, 4.4092800182812476e-7, 4.332984886451389e-7, 4.2993797771578966e-7, 4.1214870061305216e-7, 4.228288890990182e-7, 4.257713425685153e-7, 4.7044721289773537e-7, 4.4872404772035994e-7, 4.5691524435408883e-7, 4.4584328558145395e-7, 4.3605419759507037e-7, 4.3901979417836827e-7, 4.2798011914330295e-7, 3.9513171954044726e-7, 4.1999102485304085e-7, 4.1657109198934646e-7, 4.2225151687075475e-7, 4.301233669014862e-7, 4.107231336579831e-7, 4.258073613540731e-7, 3.9610634642964084e-7, 4.15103273288891e-7, 4.212582451675738e-7, 4.1837121515587076e-7, 4.18461148157748e-7, 4.4809447285122963e-7, 4.263997659385628e-7, 4.5855999591570644e-7, 4.0928287659935435e-7, 4.20754101965959e-7, 3.9890959573348413e-7, 4.285326390034324e-7, 4.07307460904671e-7, 3.98448628227181e-7, 4.4001270058201183e-7, 4.2113036750787404e-7, 4.3667451322290034e-7, 4.03753175145503e-7, 4.4546282900162584e-7, 4.405591162217143e-7, 4.5685275336365136e-7, 4.10804668661687e-7, 4.1720792981298653e-7, 4.365575374851748e-7, 4.1525157677293685e-7, 4.214629075457323e-7, 4.058729234901859e-7, 4.2954227126868594e-7, 3.961749863035141e-7, 4.3700438287547935e-7, 4.2448363465602257e-7 ], "variance": 3.364598432208039e-16 }, "times": { "cycle": 0.0543496609534155, "elapsed": 5.489, "period": 4.253710648306762e-7, "timeStamp": 1631437113733 }, "running": false, "count": 127770, "cycles": 7, "hz": 2350888.6303727813 }, "name": "(194 bytes)", "options": { "name": "deep" }, "events": { "start": [ null ], "complete": [ null ] }, "length": 1, "running": false } }, "results": [ { "name": "set-value", "file": { "stat": null, "_contents": null, "history": [ "/Users/jonschlinkert/dev/js-utils/objects/set-value/benchmark/code/set-value.js" ], "_cwd": "/Users/jonschlinkert/dev/js-utils/objects/set-value", "_isVinyl": true, "_symlink": null, "key": "set-value" }, "runs": 89, "hz": 2350888.6303727813, "ops": "2,350,889", "rme": "0.90" } ] }, { "name": "medium", "file": { "stat": null, "_contents": null, "history": [ "/Users/jonschlinkert/dev/js-utils/objects/set-value/benchmark/fixtures/medium.js" ], "_cwd": "/Users/jonschlinkert/dev/js-utils/objects/set-value", "_isVinyl": true, "_symlink": null, "key": "medium", "content": [ { "x": { "y": { "z": "foooo" } } }, "x.y.z", "foooo", { "x": { "y": { "z": "foooo" } } } ], "bytes": "(98 bytes)", "suite": { "0": { "name": "set-value", "options": { "async": false, "defer": false, "delay": 0.005, "initCount": 1, "maxTime": 5, "minSamples": 5, "minTime": 0.05 }, "async": false, "defer": false, "delay": 0.005, "initCount": 1, "maxTime": 5, "minSamples": 5, "minTime": 0.05, "events": { "abort": [ null ], "complete": [ null ], "cycle": [ null ], "error": [ null ] }, "id": 2, "stats": { "moe": 1.0803601908881484e-9, "rme": 0.8070290968306569, "sem": 5.512041790245655e-10, "deviation": 5.229181984562506e-9, "mean": 1.3386880288838532e-7, "sample": [ 1.2586806847317884e-7, 1.4585648785883063e-7, 1.2683933688602888e-7, 1.311958172037605e-7, 1.367016917198733e-7, 1.3342903574480922e-7, 1.3838017445075662e-7, 1.354059499271027e-7, 1.3037146196772408e-7, 1.4162180193521666e-7, 1.2669746614625822e-7, 1.317335826608602e-7, 1.3906323966817686e-7, 1.4370313374581143e-7, 1.3545984613646918e-7, 1.4097549980547432e-7, 1.266423543940107e-7, 1.440246010820393e-7, 1.2816768372384844e-7, 1.322993210489756e-7, 1.3904989182096807e-7, 1.39271037647299e-7, 1.3553541774036535e-7, 1.3842928776120443e-7, 1.2584332339928644e-7, 1.3275226628225142e-7, 1.3494471790045537e-7, 1.2825857900803937e-7, 1.3843763086675181e-7, 1.290115922111097e-7, 1.4372392810638144e-7, 1.3741903751783274e-7, 1.3534694472039018e-7, 1.3527674431183955e-7, 1.316260664532531e-7, 1.2756648521594603e-7, 1.3931558410904747e-7, 1.3402746593496294e-7, 1.3862562880788352e-7, 1.3321506581891316e-7, 1.3317759796617837e-7, 1.2511520891419692e-7, 1.3666665191457556e-7, 1.3134119128053068e-7, 1.3099377953491574e-7, 1.4267014570148652e-7, 1.3032739807534386e-7, 1.396849494249143e-7, 1.2737865913326547e-7, 1.3346059470596624e-7, 1.247158894773334e-7, 1.4161341407644534e-7, 1.3268476748245732e-7, 1.332347475671343e-7, 1.3507633715412494e-7, 1.336740230427663e-7, 1.379879131200181e-7, 1.301945382841351e-7, 1.2790126917157174e-7, 1.2603768913410143e-7, 1.379709506739247e-7, 1.2613686252526296e-7, 1.3256865869070275e-7, 1.372925585535083e-7, 1.3456888734811493e-7, 1.3554240734457444e-7, 1.325053156701629e-7, 1.271406562713598e-7, 1.335377112622381e-7, 1.4482470581871647e-7, 1.3094733011737748e-7, 1.36001804672479e-7, 1.3342936698777052e-7, 1.4096517031289185e-7, 1.3301769513328515e-7, 1.3401149187897385e-7, 1.2631596028737074e-7, 1.3643789861379517e-7, 1.3072781654299497e-7, 1.2626249625051017e-7, 1.385911777577805e-7, 1.299946228627908e-7, 1.3835222584468014e-7, 1.2814054563067649e-7, 1.3484052743643078e-7, 1.2839612315045659e-7, 1.3608970992471516e-7, 1.3435631217391731e-7, 1.2541887087494652e-7, 1.377563839674274e-7 ], "variance": 2.7344344227673075e-17 }, "times": { "cycle": 0.05444738724836985, "elapsed": 5.489, "period": 1.3386880288838532e-7, "timeStamp": 1631437119222 }, "running": false, "count": 406722, "cycles": 8, "hz": 7470000.3169055125 }, "name": "(98 bytes)", "options": { "name": "medium" }, "events": { "start": [ null ], "complete": [ null ] }, "length": 1, "running": false } }, "results": [ { "name": "set-value", "file": { "stat": null, "_contents": null, "history": [ "/Users/jonschlinkert/dev/js-utils/objects/set-value/benchmark/code/set-value.js" ], "_cwd": "/Users/jonschlinkert/dev/js-utils/objects/set-value", "_isVinyl": true, "_symlink": null, "key": "set-value" }, "runs": 90, "hz": 7470000.3169055125, "ops": "7,470,000", "rme": "0.81" } ] }, { "name": "shallow", "file": { "stat": null, "_contents": null, "history": [ "/Users/jonschlinkert/dev/js-utils/objects/set-value/benchmark/fixtures/shallow.js" ], "_cwd": "/Users/jonschlinkert/dev/js-utils/objects/set-value", "_isVinyl": true, "_symlink": null, "key": "shallow", "content": [ { "foo": { "i": { "k": "L" } } }, "foo", { "i": { "k": "L" } }, { "foo": { "i": { "k": "L" } } } ], "bytes": "(101 bytes)", "suite": { "0": { "name": "set-value", "options": { "async": false, "defer": false, "delay": 0.005, "initCount": 1, "maxTime": 5, "minSamples": 5, "minTime": 0.05 }, "async": false, "defer": false, "delay": 0.005, "initCount": 1, "maxTime": 5, "minSamples": 5, "minTime": 0.05, "events": { "abort": [ null ], "complete": [ null ], "cycle": [ null ], "error": [ null ] }, "id": 3, "stats": { "moe": 7.325476289493897e-10, "rme": 0.903432224810049, "sem": 3.737487902803009e-10, "deviation": 3.525939035633339e-9, "mean": 8.108495677176131e-8, "sample": [ 8.305923336477406e-8, 8.177847376646847e-8, 8.196885877491305e-8, 7.948966441426558e-8, 8.587929548049602e-8, 8.47644168781014e-8, 8.61609434574075e-8, 8.30022129571716e-8, 8.421524543078131e-8, 7.788224762683395e-8, 8.456992253113123e-8, 8.276447681235814e-8, 7.85972832877167e-8, 8.706829739070835e-8, 8.046921607528972e-8, 9.681608942805815e-8, 8.01013933946302e-8, 8.049219087370316e-8, 8.576562650552657e-8, 7.906724417710116e-8, 7.724164437531448e-8, 8.245908408925105e-8, 8.331908063275322e-8, 8.283924990938165e-8, 7.866853715199873e-8, 7.905276359237184e-8, 8.850674136783433e-8, 8.058689217886622e-8, 7.807339208604319e-8, 8.364810256116132e-8, 8.019336473720514e-8, 8.486670460085337e-8, 7.931103869739839e-8, 7.580877849403291e-8, 7.55876226886685e-8, 8.759498363144874e-8, 8.102292804629379e-8, 7.98568427185228e-8, 8.239872704139002e-8, 8.117721805566666e-8, 8.169286892631662e-8, 8.207891769841115e-8, 7.885031793778895e-8, 8.439302843916438e-8, 7.76215726495212e-8, 7.808337910909582e-8, 8.302041473593279e-8, 8.11676611921363e-8, 8.114628027408268e-8, 7.945343288119422e-8, 7.706396736328519e-8, 8.398616974379283e-8, 7.574974633322623e-8, 7.950717514588641e-8, 8.492352787345801e-8, 8.233461299939478e-8, 8.261256603181482e-8, 8.108616152483991e-8, 7.643010527731493e-8, 7.853419607432922e-8, 8.457282365862953e-8, 7.709262162182357e-8, 8.162701644537759e-8, 8.117974102826572e-8, 8.459285847710275e-8, 7.923084498307642e-8, 8.105100383302077e-8, 7.917051377184798e-8, 7.920606416821564e-8, 7.694740175298102e-8, 7.954250574898428e-8, 8.187964235072845e-8, 7.871995503669798e-8, 8.00944978730595e-8, 8.161824834142721e-8, 7.450728302635349e-8, 8.274469954669351e-8, 8.087301540654319e-8, 7.54707525475531e-8, 8.331316205156086e-8, 7.757052846573753e-8, 8.3828872023569e-8, 7.996989809787599e-8, 7.948619141729913e-8, 7.525599694367098e-8, 8.488487925296265e-8, 7.62940864441522e-8, 7.579118954382819e-8, 8.418272733283864e-8 ], "variance": 1.2432246083002961e-17 }, "times": { "cycle": 0.05518277275580602, "elapsed": 5.507, "period": 8.108495677176131e-8, "timeStamp": 1631437124712 }, "running": false, "count": 680555, "cycles": 6, "hz": 12332743.825896205 }, "name": "(101 bytes)", "options": { "name": "shallow" }, "events": { "start": [ null ], "complete": [ null ] }, "length": 1, "running": false } }, "results": [ { "name": "set-value", "file": { "stat": null, "_contents": null, "history": [ "/Users/jonschlinkert/dev/js-utils/objects/set-value/benchmark/code/set-value.js" ], "_cwd": "/Users/jonschlinkert/dev/js-utils/objects/set-value", "_isVinyl": true, "_symlink": null, "key": "set-value" }, "runs": 89, "hz": 12332743.825896205, "ops": "12,332,744", "rme": "0.90" } ] } ]set-value-4.1.0/benchmark/stats.md000066400000000000000000000036431411734170200170410ustar00rootroot00000000000000# deep (194 bytes) deep-object x 823,287 ops/sec ±1.00% (90 runs sampled) deep-property x 1,787,990 ops/sec ±0.82% (92 runs sampled) deephas x 840,700 ops/sec ±0.95% (93 runs sampled) dot-prop x 1,249,663 ops/sec ±0.89% (90 runs sampled) dot2val x 2,067,212 ops/sec ±1.08% (91 runs sampled) es5-dot-prop x 1,668,806 ops/sec ±0.92% (92 runs sampled) lodash-set x 1,286,725 ops/sec ±0.82% (90 runs sampled) object-path-set x 1,261,242 ops/sec ±1.63% (90 runs sampled) object-set x 285,369 ops/sec ±0.91% (90 runs sampled) set-value x 2,076,931 ops/sec ±0.86% (93 runs sampled) fastest is set-value, dot2val (by 203% avg) # medium (98 bytes) deep-object x 5,811,161 ops/sec ±1.12% (90 runs sampled) deep-property x 4,075,885 ops/sec ±0.91% (90 runs sampled) deephas x 1,508,136 ops/sec ±0.82% (92 runs sampled) dot-prop x 2,809,838 ops/sec ±1.16% (87 runs sampled) dot2val x 4,600,890 ops/sec ±0.76% (91 runs sampled) es5-dot-prop x 3,263,790 ops/sec ±0.97% (91 runs sampled) lodash-set x 3,486,628 ops/sec ±1.20% (90 runs sampled) object-path-set x 3,729,018 ops/sec ±0.90% (92 runs sampled) object-set x 973,961 ops/sec ±0.80% (92 runs sampled) set-value x 6,941,474 ops/sec ±1.24% (90 runs sampled) fastest is set-value (by 206% avg) # shallow (101 bytes) deep-object x 9,416,410 ops/sec ±1.19% (89 runs sampled) deep-property x 5,108,536 ops/sec ±0.98% (93 runs sampled) deephas x 1,706,979 ops/sec ±0.98% (86 runs sampled) dot-prop x 4,045,902 ops/sec ±1.10% (92 runs sampled) dot2val x 5,862,418 ops/sec ±0.88% (91 runs sampled) es5-dot-prop x 4,439,646 ops/sec ±1.18% (90 runs sampled) lodash-set x 9,303,292 ops/sec ±1.19% (89 runs sampled) object-path-set x 5,657,479 ops/sec ±0.95% (93 runs sampled) object-set x 2,020,041 ops/sec ±0.92% (91 runs sampled) set-value x 11,272,227 ops/sec ±1.36% (88 runs sampled) fastest is set-value (by 213% avg) set-value-4.1.0/examples.js000066400000000000000000000017001411734170200155730ustar00rootroot00000000000000'use strict'; const set = require('.'); const data = {}; const key1 = Symbol('key-1'); const key2 = Symbol('key-2'); const key3 = Symbol('key-3'); const key4 = Symbol('key-4'); set(data, 'a.b.c', true); set(data, '"a.b".c', true, { split: set.parse }); set(data, 'foo-bar', true); set(data, ['one', 'two'], true); set(data, ['one', key1], true); set(data, [key2, key3, key4], true); set(data, key1, true); console.log(data); // 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' } } // 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-4.1.0/index.js000066400000000000000000000076511411734170200150770ustar00rootroot00000000000000/*! * set-value * * Copyright (c) Jon Schlinkert (https://github.com/jonschlinkert). * Released under the MIT License. */ 'use strict'; const { deleteProperty } = Reflect; const isPrimitive = require('is-primitive'); const isPlainObject = require('is-plain-object'); const isObject = value => { return (typeof value === 'object' && value !== null) || typeof value === 'function'; }; const isUnsafeKey = key => { return key === '__proto__' || key === 'constructor' || key === 'prototype'; }; const validateKey = key => { if (!isPrimitive(key)) { throw new TypeError('Object keys must be strings or symbols'); } if (isUnsafeKey(key)) { throw new Error(`Cannot set unsafe key: "${key}"`); } }; const toStringKey = input => { return Array.isArray(input) ? input.flat().map(String).join(',') : input; }; const createMemoKey = (input, options) => { if (typeof input !== 'string' || !options) return input; let key = input + ';'; if (options.arrays !== undefined) key += `arrays=${options.arrays};`; if (options.separator !== undefined) key += `separator=${options.separator};`; if (options.split !== undefined) key += `split=${options.split};`; if (options.merge !== undefined) key += `merge=${options.merge};`; if (options.preservePaths !== undefined) key += `preservePaths=${options.preservePaths};`; return key; }; const memoize = (input, options, fn) => { const key = toStringKey(options ? createMemoKey(input, options) : input); validateKey(key); const value = setValue.cache.get(key) || fn(); setValue.cache.set(key, value); return value; }; const splitString = (input, options = {}) => { const sep = options.separator || '.'; const preserve = sep === '/' ? false : options.preservePaths; if (typeof input === 'string' && preserve !== false && /\//.test(input)) { return [input]; } const parts = []; let part = ''; const push = part => { let number; if (part.trim() !== '' && Number.isInteger((number = Number(part)))) { parts.push(number); } else { parts.push(part); } }; for (let i = 0; i < input.length; i++) { const value = input[i]; if (value === '\\') { part += input[++i]; continue; } if (value === sep) { push(part); part = ''; continue; } part += value; } if (part) { push(part); } return parts; }; const split = (input, options) => { if (options && typeof options.split === 'function') return options.split(input); if (typeof input === 'symbol') return [input]; if (Array.isArray(input)) return input; return memoize(input, options, () => splitString(input, options)); }; const assignProp = (obj, prop, value, options) => { validateKey(prop); // Delete property when "value" is undefined if (value === undefined) { deleteProperty(obj, prop); } else if (options && options.merge) { const merge = options.merge === 'function' ? options.merge : Object.assign; // Only merge plain objects if (merge && isPlainObject(obj[prop]) && isPlainObject(value)) { obj[prop] = merge(obj[prop], value); } else { obj[prop] = value; } } else { obj[prop] = value; } return obj; }; const setValue = (target, path, value, options) => { if (!path || !isObject(target)) return target; const keys = split(path, options); let obj = target; for (let i = 0; i < keys.length; i++) { const key = keys[i]; const next = keys[i + 1]; validateKey(key); if (next === undefined) { assignProp(obj, key, value, options); break; } if (typeof next === 'number' && !Array.isArray(obj[key])) { obj = obj[key] = []; continue; } if (!isObject(obj[key])) { obj[key] = {}; } obj = obj[key]; } return target; }; setValue.split = split; setValue.cache = new Map(); setValue.clear = () => { setValue.cache = new Map(); }; module.exports = setValue; set-value-4.1.0/package.json000066400000000000000000000047471411734170200157230ustar00rootroot00000000000000{ "name": "set-value", "version": "4.1.0", "description": "Set nested properties on an object using dot notation.", "license": "MIT", "repository": "jonschlinkert/set-value", "homepage": "https://github.com/jonschlinkert/set-value", "bugs": "https://github.com/jonschlinkert/set-value/issues", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "funding": [ "https://github.com/sponsors/jonschlinkert", "https://paypal.me/jonathanschlinkert", "https://jonschlinkert.dev/sponsor" ], "contributors": [ "Jon Schlinkert (http://twitter.com/jonschlinkert)", "(https://github.com/wtgtybhertgeghgtwtg)", "Vadim Demedes (https://vadimdemedes.com)" ], "files": [ "index.js" ], "main": "index.js", "engines": { "node": ">=11.0" }, "scripts": { "test": "mocha" }, "dependencies": { "is-plain-object": "^2.0.4", "is-primitive": "^3.0.1" }, "devDependencies": { "gulp-format-md": "^2.0.0", "mocha": "^9.1.1", "split-string": "^6.1.0" }, "keywords": [ "bury", "deep-get-set", "deep-object", "deep-property", "deep-set-in", "deep-set", "deephas", "dot-prop", "dot2val", "es5-dot-prop", "get", "getsetdeep", "has", "hasown", "key", "keys", "lodash.set", "nested", "notation", "object-path-set", "object-path", "object-set", "object", "patch", "prop", "properties", "property", "props", "put", "set-deep-prop", "set-deep", "set-nested-prop", "set", "setvalue", "split-string", "value", "values" ], "verb": { "toc": false, "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "related": { "list": [ "assign-value", "get-value", "has-value", "merge-value", "omit-value", "set-value", "union-value", "unset-value" ] }, "lint": { "reflinks": true }, "reflinks": [ "bury", "deep-get-set", "deep-object", "deep-property", "deep-set", "deep-set-in", "deephas", "dot-prop", "dot2val", "es5-dot-prop", "getsetdeep", "lodash.set", "object-path", "object-path-set", "object-set", "set-deep", "set-deep-prop", "set-nested-prop", "setvalue", "split-string", "update" ] } } set-value-4.1.0/test/000077500000000000000000000000001411734170200144005ustar00rootroot00000000000000set-value-4.1.0/test/.eslintrc.json000066400000000000000000000001211411734170200171660ustar00rootroot00000000000000{ "extends": [ "../.eslintrc.json" ], "env": { "mocha": true } } set-value-4.1.0/test/test.js000066400000000000000000000235441411734170200157250ustar00rootroot00000000000000'use strict'; require('mocha'); const split = require('split-string'); const assert = require('assert'); const set = require('..'); describe('set-value', () => { describe('unsafe properties', () => { it('should not allow setting constructor', () => { assert.throws(() => set({}, 'a.constructor.b', 'c')); assert.throws(() => set({}, 'a.constructor', 'c')); assert.throws(() => set({}, 'constructor', 'c')); }); it('should not allow setting prototype', () => { assert.throws(() => set({}, 'a.prototype.b', 'c')); assert.throws(() => set({}, 'a.prototype', 'c')); assert.throws(() => set({}, 'prototype', 'c')); }); it('should not allow setting __proto__', () => { assert.throws(() => set({}, 'a.__proto__.b', 'c')); assert.throws(() => set({}, 'a.__proto__', 'c')); assert.throws(() => set({}, '__proto__', 'c')); }); }); describe('set', () => { it('should return non-objects', () => { const str = set('foo', 'a.b', 'c'); assert.equal(str, 'foo'); const _null = set(null, 'a.b', 'c'); assert.equal(_null, null); }); it('should set when key is a symbol', () => { const key = Symbol('foo'); const obj = {}; set(obj, key, 'bar'); assert.equal(obj[key], 'bar'); }); it('should set on the root of the object', () => { const o = {}; set(o, 'foo', 'bar'); assert.equal(o.foo, 'bar'); }); it('should set the specified property.', () => { assert.deepEqual(set({ a: 'aaa', b: 'b' }, 'a', 'bbb'), { a: 'bbb', b: 'b' }); }); it('should set a nested property', () => { const o = {}; set(o, 'a.b', 'c'); assert.equal(o.a.b, 'c'); }); it('should set a nested property where the last key is a symbol', () => { const o = {}; set(o, 'a.b', 'c'); assert.equal(o.a.b, 'c'); }); it('should support passing an array as the key', () => { const actual = set({ a: 'a', b: { c: 'd' } }, ['b', 'c', 'd'], 'eee'); assert.deepEqual(actual, { a: 'a', b: { c: { d: 'eee' } } }); }); it('should set a deeply nested value.', () => { const actual = set({ a: 'a', b: { c: 'd' } }, 'b.c.d', 'eee'); assert.deepEqual(actual, { a: 'a', b: { c: { d: 'eee' } } }); }); it('should allow keys to be whitespace', () => { const o = {}; set(o, 'a. .a', { y: 'z' }); assert.deepEqual(o.a[' '].a, { y: 'z' }); }); it('should extend an array', () => { const o = { a: [] }; set(o, 'a.0', { y: 'z' }); assert(Array.isArray(o.a)); assert.deepEqual(o.a[0], { y: 'z' }); }); it('should create an array if it does not already exist', () => { const o = {}; set(o, 'a.0.a', { y: 'z' }); set(o, 'a.1.b', { y: 'z' }); set(o, 'a.2.c', { y: 'z' }); set(o, 'b.0', { y: 'z' }); set(o, '0', { y: 'z' }); assert(Array.isArray(o.a)); assert.deepEqual(o.a[0].a, { y: 'z' }); assert.deepEqual(o.a[1].b, { y: 'z' }); assert.deepEqual(o.a[2].c, { y: 'z' }); assert.deepEqual(o.b, [{ y: 'z' }]); assert.deepEqual(o['0'], { y: 'z' }); }); it('should extend a function', () => { const log = () => {}; const warning = () => {}; const o = {}; set(o, 'helpers.foo', log); set(o, 'helpers.foo.warning', warning); assert.equal(typeof o.helpers.foo, 'function'); assert.equal(typeof o.helpers.foo.warning, 'function'); }); it('should extend an object in an array', () => { const o = { a: [{}, {}, {}] }; set(o, 'a.0.a', { y: 'z' }); set(o, 'a.1.b', { y: 'z' }); set(o, 'a.2.c', { y: 'z' }); assert(Array.isArray(o.a)); assert.deepEqual(o.a[0].a, { y: 'z' }); assert.deepEqual(o.a[1].b, { y: 'z' }); assert.deepEqual(o.a[2].c, { y: 'z' }); }); it('should create a deeply nested property if it does not already exist', () => { const o = {}; set(o, 'a.b.c.d.e', 'c'); assert.equal(o.a.b.c.d.e, 'c'); }); it('should not create a nested property if it does already exist', () => { const first = { name: 'Halle' }; const o = { a: first }; set(o, 'a.b', 'c'); assert.equal(o.a.b, 'c'); assert.equal(o.a, first); assert.equal(o.a.name, 'Halle'); }); it('should support immediate properties', () => { const o = {}; set(o, 'a', 'b'); assert.equal(o.a, 'b'); }); it('should use property paths to set nested values from the source object.', () => { const o = {}; set(o, 'a.locals.name', { first: 'Brian' }); set(o, 'b.locals.name', { last: 'Woodward' }); set(o, 'b.locals.name.last', 'Woodward'); assert.deepEqual(o, { a: { locals: { name: { first: 'Brian' } } }, b: { locals: { name: { last: 'Woodward' } } } }); }); it('should delete the property when value is undefined', () => { const fixture = {}; assert.deepEqual(set(fixture, 'a.locals.name'), { a: { locals: {} } }); assert.deepEqual(set(fixture, 'b.locals.name'), { b: { locals: {} }, a: { locals: {} } }); assert.deepEqual(set({ a: 'a', b: { c: 'd' } }, 'b.c'), { a: 'a', b: {} }); }); it('should return the entire object if no property is passed.', () => { assert.deepEqual(set({ a: 'a', b: { c: 'd' } }), { a: 'a', b: { c: 'd' } }); }); it('should set non-plain objects', done => { const o = {}; set(o, 'a.b', new Date()); const firstDate = o.a.b.getTime(); setTimeout(function() { set(o, 'a.b', new Date()); const secondDate = o.a.b.getTime(); assert.notDeepEqual(firstDate, secondDate); done(); }, 10); }); }); describe('escaping', () => { it('should not split escaped dots', () => { const o = {}; set(o, 'a\\.b.c.d.e', 'c'); assert.equal(o['a.b'].c.d.e, 'c'); }); it('should support multiple escaped dots', () => { const obj1 = {}; set(obj1, 'e\\.f\\.g', 1); assert.equal(obj1['e.f.g'], 1); const obj2 = {}; set(obj2, 'e\\.f.g\\.h\\.i.j', 1); assert.deepEqual(obj2, { 'e.f': { 'g.h.i': { j: 1 } } }); }); it('should support multiple escaped dots', () => { const obj1 = {}; const key = Symbol('key'); set(obj1, [key, 'e.f', 'g'], 1); assert.equal(obj1[key]['e.f'].g, 1); const obj2 = {}; set(obj2, 'e\\.f.g\\.h\\.i.j', 1); assert.deepEqual(obj2, { 'e.f': { 'g.h.i': { j: 1 } } }); }); it('should correctly parse multiple consecutive backslashes', () => { assert.deepEqual(set.split('a.b.c'), ['a', 'b', 'c']); assert.deepEqual(set.split('b\\.c\\.d'), ['b.c.d']); assert.deepEqual(set.split('b\\\\.c\\.d'), ['b\\', 'c.d']); assert.deepEqual(set.split('a.b\\.c'), ['a', 'b.c']); assert.deepEqual(set.split('a.b\\\\.c'), ['a', 'b\\', 'c']); assert.deepEqual(set.split('a.b\\\\\\.c'), ['a', 'b\\.c']); }); }); describe('options.merge', () => { it('should merge an existing value with the given value', () => { const o = { a: { b: { c: 'd' } } }; set(o, 'a.b', { y: 'z' }, { merge: true }); assert.deepEqual(o.a.b, { c: 'd', y: 'z' }); const obj = { foo: { bar: { baz: 'qux' } } }; set(obj, 'foo.bar.fez', 'zzz', { merge: true }); assert.deepEqual(obj, { foo: { bar: { baz: 'qux', fez: 'zzz' } } }); }); it('should update an object by merging values', () => { const o = {}; set(o, 'a', { b: 'c' }); set(o, 'a', { c: 'd' }, { merge: true }); assert.deepEqual(o, { a: { b: 'c', c: 'd' } }); set(o, 'a', 'b'); assert.equal(o.a, 'b'); }); }); describe('options.preservePaths', () => { it('should split properties with a forward slash when preservePaths is false', () => { const obj = {}; set(obj, 'https://github.com', true, { preservePaths: false }); assert.deepEqual(obj, { 'https://github': { com: true } }); }); it('should not split properties with a forward slash', () => { const obj = {}; set(obj, 'foo/bar/baz.md', 'c'); assert.equal(obj['foo/bar/baz.md'], 'c'); }); }); describe('options.split', () => { const keep = (value, state) => { return value !== '"' && value !== '\''; }; const options = { split(prop) { return split(prop, { separator: '.', brackets: true, quotes: true, keep }); } }; it('should use simple String.split() when options.split is not defined', () => { const o = {}; set(o, 'a."b.c.d".e', 'c'); assert.equal(o.a['"b'].c['d"'].e, 'c'); }); it('should take a custom separator', () => { const o = {}; set(o, 'a/b/c/d/e', 'c', { separator: '/' }); assert.equal(o.a.b.c.d.e, 'c'); }); it('should use a custom function to not split inside double quotes', () => { const o = {}; set(o, 'a."b.c.d".e', 'c', options); assert.equal(o.a['b.c.d'].e, 'c'); }); it('should use a custom function to not split inside single quotes', () => { const o = {}; set(o, "a.'b.c.d'.e", 'c', options); assert.equal(o.a['b.c.d'].e, 'c'); }); it('should use a custom function to not split inside square brackets', () => { const o = {}; set(o, 'a.[b.c.d].e', 'c', options); assert.equal(o.a['[b.c.d]'].e, 'c'); }); it('should use a custom function to not split inside parens', () => { const o = {}; set(o, 'a.(b.c.d).e', 'c', options); assert.equal(o.a['(b.c.d)'].e, 'c'); }); it('should use a custom function to not split inside angle brackets', () => { const o = {}; set(o, 'a..e', 'c', options); assert.equal(o.a[''].e, 'c'); }); it('should use a custom function to not split inside curly braces', () => { const o = {}; set(o, 'a.{b.c.d }.e', 'c', options); assert.equal(o.a['{b.c.d }'].e, 'c'); }); }); });