Comparing version 1.0.0 to 1.1.0
@@ -1,2 +0,2 @@ | ||
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}; | ||
function r(r){var t=r&&r.pop?[]:{};for(var n in r)t[n]=r[n];return t}export default function(t,n,l){n.split&&(n=n.split("."));for(var o=r(t),a=o,e=0,f=n.length;e<f;e++)a=a[n[e]]=e===f-1?l.call?l(a[n[e]]):l:r(a[n[e]]);return o}; | ||
//# sourceMappingURL=clean-set.es.js.map |
@@ -1,2 +0,2 @@ | ||
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}; | ||
function r(r){var n=r&&r.pop?[]:{};for(var t in r)n[t]=r[t];return n}module.exports=function(n,t,o){t.split&&(t=t.split("."));for(var l=r(n),e=l,i=0,p=t.length;i<p;i++)e=e[t[i]]=i===p-1?o.call?o(e[t[i]]):o:r(e[t[i]]);return l}; | ||
//# 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){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}}); | ||
!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){var n=e&&e.pop?[]:{};for(var t in e)n[t]=e[t];return n}return function(n,t,o){t.split&&(t=t.split("."));for(var f=e(n),i=f,r=0,u=t.length;r<u;r++)i=i[t[r]]=r===u-1?o.call?o(i[t[r]]):o:e(i[t[r]]);return f}}); | ||
//# sourceMappingURL=clean-set.min.js.map |
@@ -7,13 +7,11 @@ export default function(source, keys, update) { | ||
i = 0, | ||
l = keys.length, | ||
temp; | ||
l = keys.length; | ||
for (; i < l; i++) { | ||
temp = last[keys[i]]; | ||
last = last[keys[i]] = | ||
i === l - 1 | ||
? typeof update === 'function' | ||
? update(temp) | ||
? !!update.call | ||
? update(last[keys[i]]) | ||
: update | ||
: copy(temp); | ||
: copy(last[keys[i]]); | ||
} | ||
@@ -24,5 +22,6 @@ | ||
function copy(source, to = {}) { | ||
function copy(source) { | ||
let to = source && !!source.pop ? [] : {}; | ||
for (let i in source) to[i] = source[i]; | ||
return to; | ||
} |
{ | ||
"name": "clean-set", | ||
"version": "1.0.0", | ||
"version": "1.1.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", |
@@ -15,3 +15,3 @@ # clean-set | ||
Includes builds for commonjs, umd, and esm and is less than 200b gzip | ||
Includes builds for commonjs, umd, and esm and is less than ~~200b~~ 182b gzip (thanks to [@lukeed](https://github.com/lukeed)) | ||
@@ -61,3 +61,3 @@ ## Usage | ||
...current.e, | ||
h: { ...current.h, i: 1 }, | ||
h: { ...current.e.h, i: 1 }, | ||
}, | ||
@@ -64,0 +64,0 @@ }; |
@@ -7,3 +7,3 @@ import test from 'tape'; | ||
a: { b: [], c: true }, | ||
d: [], | ||
d: [{ m: [{ n: 2 }] }], | ||
e: { | ||
@@ -28,2 +28,10 @@ f: { g: 'hello' }, | ||
tap.assert(next.d === data.d, 'untouched node kept their reference'); | ||
tap.assert( | ||
next.d[0].m === data.d[0].m, | ||
'untouched node kept their reference' | ||
); | ||
tap.assert( | ||
next.d[0].m[0] === data.d[0].m[0], | ||
'untouched node kept their reference' | ||
); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
@@ -49,2 +57,10 @@ tap.assert(next.e.h.j === data.e.h.j, 'untouched node kept their reference'); | ||
tap.assert(next.d === data.d, 'untouched node kept their reference'); | ||
tap.assert( | ||
next.d[0].m === data.d[0].m, | ||
'untouched node kept their reference' | ||
); | ||
tap.assert( | ||
next.d[0].m[0] === data.d[0].m[0], | ||
'untouched node kept their reference' | ||
); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
@@ -68,2 +84,10 @@ | ||
tap.assert(next.d === data.d, 'untouched node kept their reference'); | ||
tap.assert( | ||
next.d[0].m === data.d[0].m, | ||
'untouched node kept their reference' | ||
); | ||
tap.assert( | ||
next.d[0].m[0] === data.d[0].m[0], | ||
'untouched node kept their reference' | ||
); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
@@ -88,2 +112,10 @@ tap.assert(next.e.h.j === data.e.h.j, 'untouched node kept their reference'); | ||
tap.assert(next.d === data.d, 'untouched node kept their reference'); | ||
tap.assert( | ||
next.d[0].m === data.d[0].m, | ||
'untouched node kept their reference' | ||
); | ||
tap.assert( | ||
next.d[0].m[0] === data.d[0].m[0], | ||
'untouched node kept their reference' | ||
); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
@@ -94,1 +126,51 @@ tap.assert(next.e.h.j === data.e.h.j, 'untouched node kept their reference'); | ||
}); | ||
test('clean-set: supports accessing an index of an array', tap => { | ||
// let next = cleanSet(data, 'd.0.m.0.n', n => n + 1); // either will work for this scenario | ||
let next = cleanSet(data, ['d', 0, 'm', 0, 'n'], n => n + 1); | ||
tap.assert(next != null, 'next has a value'); | ||
tap.assert(next !== data, 'next has a new reference'); | ||
tap.assert(next.d[0].m[0].n === 3, 'value was updated'); | ||
tap.assert( | ||
next.d[0].m[0] !== data.d[0].m[0], | ||
'touched node has a new reference' | ||
); | ||
tap.assert(next.d[0].m !== data.d[0].m, 'touched node has a new reference'); | ||
tap.assert(next.d[0] !== data.d[0], 'touched node has a new reference'); | ||
tap.assert(next.d !== data.d, '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.e.f === data.e.f, 'untouched node kept their reference'); | ||
tap.assert(next.e.h.i === data.e.h.i, '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 a record at the index if none exists', tap => { | ||
let next = cleanSet(data, ['d', 2, 'o'], { p: 'creates at index' }); | ||
tap.assert(next != null, 'next has a value'); | ||
tap.assert(next !== data, 'next has a new reference'); | ||
tap.same(next.d[2].o, { p: 'creates at index' }, 'value was created'); | ||
tap.assert(next.d !== data.d, '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[0].m === data.d[0].m, | ||
'untouched node kept their reference' | ||
); | ||
tap.assert( | ||
next.d[0].m[0] === data.d[0].m[0], | ||
'untouched node kept their reference' | ||
); | ||
tap.assert(next.e.f === data.e.f, 'untouched node kept their reference'); | ||
tap.assert(next.e.h.i === data.e.h.i, '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
30467
181