Comparing version 0.0.1 to 1.0.0
@@ -1,2 +0,2 @@ | ||
function n(n,t){for(var o in void 0===t&&(t={}),n)t[o]=n[o];return t}export default function(t,o,r){for(var f=n(t),i=f,e=o.split("."),u=0,v=e.length,a=void 0;u<v;u++)a=i[e[u]],i=i[e[u]]=u===v-1?"function"==typeof r?r(a):r:n(a);return f}; | ||
function t(t,n){for(var r in void 0===n&&(n={}),t)n[r]=t[r];return n}export default function(n,r,o){r.split&&(r=r.split("."));for(var f,i=t(n),e=i,u=0,l=r.length;u<l;u++)f=e[r[u]],e=e[r[u]]=u===l-1?"function"==typeof o?o(f):o:t(f);return i}; | ||
//# sourceMappingURL=clean-set.es.js.map |
@@ -1,2 +0,2 @@ | ||
function n(n,o){for(var r in void 0===o&&(o={}),n)o[r]=n[r];return o}module.exports=function(o,r,t){for(var i=n(o),e=i,f=r.split("."),u=0,v=f.length,c=void 0;u<v;u++)c=e[f[u]],e=e[f[u]]=u===v-1?"function"==typeof t?t(c):t:n(c);return i}; | ||
function n(n,t){for(var o in void 0===t&&(t={}),n)t[o]=n[o];return t}module.exports=function(t,o,r){o.split&&(o=o.split("."));for(var i,e=n(t),f=e,u=0,l=o.length;u<l;u++)i=f[o[u]],f=f[o[u]]=u===l-1?"function"==typeof r?r(i):r:n(i);return e}; | ||
//# sourceMappingURL=clean-set.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.cleanSet=n()}(this,function(){function e(e,n){for(var t in void 0===n&&(n={}),e)n[t]=e[t];return n}return function(n,t,o){for(var f=e(n),i=f,r=t.split("."),u=0,d=r.length,c=void 0;u<d;u++)c=i[r[u]],i=i[r[u]]=u===d-1?"function"==typeof o?o(c):o:e(c);return f}}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.cleanSet=n()}(this,function(){function e(e,n){for(var t in void 0===n&&(n={}),e)n[t]=e[t];return n}return function(n,t,o){t.split&&(t=t.split("."));for(var f,i=e(n),r=i,u=0,d=t.length;u<d;u++)f=r[t[u]],r=r[t[u]]=u===d-1?"function"==typeof o?o(f):o:e(f);return i}}); | ||
//# sourceMappingURL=clean-set.min.js.map |
@@ -5,3 +5,3 @@ declare module 'clean-set' { | ||
* @param source The object to be updated | ||
* @param location The dot notation path to the property you wish to update | ||
* @param location The dot notation or array key path to the property you wish to update | ||
* @param update Either a function that will receive the current value and return a new value OR the new value for the node | ||
@@ -12,5 +12,5 @@ * @returns The new object | ||
source: A, | ||
location: string, | ||
keys: string | string[], | ||
update: <B>(value: B) => B | any | ||
): A; | ||
} |
@@ -1,7 +0,11 @@ | ||
export default function(source, location, update) { | ||
let next = copy(source); | ||
let last = next; | ||
let keys = location.split('.'); | ||
export default function(source, keys, update) { | ||
keys.split && (keys = keys.split('.')); | ||
for (let i = 0, l = keys.length, temp; i < l; i++) { | ||
let next = copy(source), | ||
last = next, | ||
i = 0, | ||
l = keys.length, | ||
temp; | ||
for (; i < l; i++) { | ||
temp = last[keys[i]]; | ||
@@ -8,0 +12,0 @@ last = last[keys[i]] = |
{ | ||
"name": "clean-set", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "A fast deep assignment alternative to the object spread operator and Object.assign", | ||
@@ -5,0 +5,0 @@ "umd:main": "dist/clean-set.min.js", |
@@ -9,2 +9,12 @@ # clean-set | ||
Check out [dset](https://github.com/lukeed/dset) if you just want to do an in place mutation on a deeply nested value. | ||
## Install | ||
> npm i clean-set | ||
Includes builds for commonjs, umd, and esm and is less than 200b gzip | ||
## Usage | ||
```javascript | ||
@@ -60,4 +70,6 @@ let current = { | ||
> Note: YMMV canary and firefox dev have some impressive improvements for object assign and object spread respectively. | ||
Chrome 67 | ||
<img src="./assets/chrome_67.png"> |
@@ -6,19 +6,11 @@ import test from 'tape'; | ||
let data = { | ||
a: { | ||
b: [], | ||
c: true, | ||
}, | ||
a: { b: [], c: true }, | ||
d: [], | ||
e: { | ||
f: { | ||
g: 'hello', | ||
}, | ||
h: { | ||
i: 0, | ||
j: [], | ||
}, | ||
f: { g: 'hello' }, | ||
h: { i: 0, j: [] }, | ||
}, | ||
}; | ||
test('cleanSet: basic functionality', tap => { | ||
test('clean-set: basic functionality', tap => { | ||
let next = cleanSet(data, 'e.h.i', 1); | ||
@@ -42,3 +34,3 @@ | ||
test('cleanSet: update value as a function', tap => { | ||
test('clean-set: update value as a function', tap => { | ||
let next = cleanSet(data, 'e.h.j', j => j.concat('some-item')); | ||
@@ -61,1 +53,39 @@ | ||
}); | ||
test('clean-set: array key', tap => { | ||
let next = cleanSet(data, ['e', 'h', 'i'], i => i + 1); | ||
tap.assert(next != null, 'next has a value'); | ||
tap.assert(next !== data, 'next has a new reference'); | ||
tap.assert(next.e.h.i === 1, 'value was updated'); | ||
tap.assert(next.e.h !== data.e.h, 'touched node has a new reference'); | ||
tap.assert(next.e !== data.e, 'touched node has a new reference'); | ||
tap.assert(next.a === data.a, 'untouched node kept their reference'); | ||
tap.assert(next.a.b === data.a.b, 'untouched node kept their reference'); | ||
tap.assert(next.d === data.d, 'untouched node kept their reference'); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
tap.assert(next.e.h.j === data.e.h.j, 'untouched node kept their reference'); | ||
tap.end(); | ||
}); | ||
test('clean-set: creates an object if none exists', tap => { | ||
let next = cleanSet(data, 'e.h.k.l', true); | ||
tap.assert(next != null, 'next has a value'); | ||
tap.assert(next !== data, 'next has a new reference'); | ||
tap.same(next.e.h.k, { l: true }, 'object was created and value was added'); | ||
tap.assert(next.e.h !== data.e.h, 'touched node has a new reference'); | ||
tap.assert(next.e !== data.e, 'touched node has a new reference'); | ||
tap.assert(next.a === data.a, 'untouched node kept their reference'); | ||
tap.assert(next.a.b === data.a.b, 'untouched node kept their reference'); | ||
tap.assert(next.d === data.d, 'untouched node kept their reference'); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
tap.assert(next.e.h.j === data.e.h.j, 'untouched node kept their reference'); | ||
tap.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27468
109
0
74