Comparing version 4.0.1 to 4.1.0
103
index.js
@@ -10,6 +10,8 @@ /*! | ||
const { deleteProperty } = Reflect; | ||
const isPrimitive = require('is-primitive'); | ||
const isPlainObject = require('is-plain-object'); | ||
const isObject = val => { | ||
return (typeof val === 'object' && val !== null) || typeof val === 'function'; | ||
const isObject = value => { | ||
return (typeof value === 'object' && value !== null) || typeof value === 'function'; | ||
}; | ||
@@ -22,4 +24,4 @@ | ||
const validateKey = key => { | ||
if (typeof key !== 'string' && typeof key !== 'number') { | ||
key = String(key); | ||
if (!isPrimitive(key)) { | ||
throw new TypeError('Object keys must be strings or symbols'); | ||
} | ||
@@ -32,3 +34,3 @@ | ||
const toString = input => { | ||
const toStringKey = input => { | ||
return Array.isArray(input) ? input.flat().map(String).join(',') : input; | ||
@@ -49,59 +51,62 @@ }; | ||
const memoize = (input, options, fn) => { | ||
const key = toString(options ? createMemoKey(input, options) : input); | ||
const key = toStringKey(options ? createMemoKey(input, options) : input); | ||
validateKey(key); | ||
const val = setValue.cache.get(key) || fn(); | ||
setValue.cache.set(key, val); | ||
return val; | ||
const value = setValue.cache.get(key) || fn(); | ||
setValue.cache.set(key, value); | ||
return value; | ||
}; | ||
const isNumber = value => { | ||
if (value.trim() !== '') { | ||
const number = Number(value); | ||
return { is: Number.isInteger(number), number }; | ||
} | ||
return { is: false }; | ||
}; | ||
const splitString = (input, options = {}) => { | ||
const sep = options.separator || '.'; | ||
const preserve = sep === '/' ? false : options.preservePaths; | ||
const splitString = (input, options) => { | ||
const opts = options || {}; | ||
const sep = opts.separator || '.'; | ||
const preserve = sep === '/' ? false : opts.preservePaths; | ||
if (typeof input === 'symbol') { | ||
if (typeof input === 'string' && preserve !== false && /\//.test(input)) { | ||
return [input]; | ||
} | ||
if (typeof opts.split === 'function') { | ||
return opts.split(input); | ||
} | ||
const parts = []; | ||
let part = ''; | ||
const keys = Array.isArray(input) ? input : input.split(sep); | ||
const push = part => { | ||
let number; | ||
if (part.trim() !== '' && Number.isInteger((number = Number(part)))) { | ||
parts.push(number); | ||
} else { | ||
parts.push(part); | ||
} | ||
}; | ||
if (typeof input === 'string' && preserve !== false && /\//.test(input)) { | ||
return [input]; | ||
} | ||
for (let i = 0; i < input.length; i++) { | ||
const value = input[i]; | ||
for (let i = 0; i < keys.length; i++) { | ||
if (typeof keys[i] !== 'string') break; | ||
const { is, number } = isNumber(keys[i]); | ||
if (value === '\\') { | ||
part += input[++i]; | ||
continue; | ||
} | ||
if (is) { | ||
keys[i] = number; | ||
if (value === sep) { | ||
push(part); | ||
part = ''; | ||
continue; | ||
} | ||
while (keys[i] && i < keys.length && keys[i].endsWith('\\') && typeof keys[i + 1] === 'string') { | ||
keys[i] = keys[i].slice(0, -1) + sep + keys.splice(i + 1, 1); | ||
} | ||
part += value; | ||
} | ||
return keys; | ||
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 setProp = (obj, prop, value, options) => { | ||
const assignProp = (obj, prop, value, options) => { | ||
validateKey(prop); | ||
@@ -111,6 +116,6 @@ | ||
if (value === undefined) { | ||
delete obj[prop]; | ||
deleteProperty(obj, prop); | ||
} else if (options && options.merge) { | ||
const merge = options.merge === true ? Object.assign : options.merge; | ||
const merge = options.merge === 'function' ? options.merge : Object.assign; | ||
@@ -131,11 +136,9 @@ // Only merge plain objects | ||
const setValue = (obj, path, value, options) => { | ||
if (!path) return obj; | ||
if (!isObject(obj)) return obj; | ||
const setValue = (target, path, value, options) => { | ||
if (!path || !isObject(target)) return target; | ||
const keys = split(path, options); | ||
const len = keys.length; | ||
const target = obj; | ||
let obj = target; | ||
for (let i = 0; i < len; i++) { | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
@@ -147,3 +150,3 @@ const next = keys[i + 1]; | ||
if (next === undefined) { | ||
setProp(obj, key, value, options); | ||
assignProp(obj, key, value, options); | ||
break; | ||
@@ -153,4 +156,3 @@ } | ||
if (typeof next === 'number' && !Array.isArray(obj[key])) { | ||
obj[key] = []; | ||
obj = obj[key]; | ||
obj = obj[key] = []; | ||
continue; | ||
@@ -169,2 +171,3 @@ } | ||
setValue.split = split; | ||
setValue.cache = new Map(); | ||
@@ -171,0 +174,0 @@ setValue.clear = () => { |
{ | ||
"name": "set-value", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Set nested properties on an object using dot notation.", | ||
@@ -31,7 +31,8 @@ "license": "MIT", | ||
"dependencies": { | ||
"is-plain-object": "^2.0.4" | ||
"is-plain-object": "^2.0.4", | ||
"is-primitive": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^2.0.0", | ||
"mocha": "^8.3.2", | ||
"mocha": "^9.1.1", | ||
"split-string": "^6.1.0" | ||
@@ -38,0 +39,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# set-value [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/jonathanschlinkert?locale.x=en_US) [![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) | ||
# 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) | ||
@@ -125,42 +125,42 @@ > Set nested properties on an object using dot notation. | ||
# 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) | ||
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 (by 210% avg) | ||
fastest is set-value, dot2val (by 203% 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) | ||
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 248% avg) | ||
fastest is set-value (by 206% 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) | ||
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 268% avg) | ||
fastest is set-value (by 213% avg) | ||
@@ -269,3 +269,3 @@ ``` | ||
* [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): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.") | ||
* [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.") | ||
@@ -278,3 +278,3 @@ * [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.") | ||
| --- | --- | | ||
| 83 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 87 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 4 | [doowb](https://github.com/doowb) | | ||
@@ -287,2 +287,3 @@ | 2 | [mbelsky](https://github.com/mbelsky) | | ||
| 1 | [zeidoo](https://github.com/zeidoo) | | ||
| 1 | [ready-research](https://github.com/ready-research) | | ||
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | | ||
@@ -305,2 +306,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on April 28, 2021._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on September 12, 2021._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20749
131
302
2
+ Addedis-primitive@^3.0.1
+ Addedis-primitive@3.0.1(transitive)