Comparing version 1.0.0 to 2.0.0
@@ -44,3 +44,52 @@ var dlv = require('dlv'); | ||
var digitRe = /^\[\d+\]/; | ||
function deepSet (source, keys, update, merge) { | ||
var removeNullAndEmpty = function removeNullAndEmpty(original) { | ||
var removeEmpty = function removeEmpty(obj) { | ||
var isObj = typeof obj === 'object'; | ||
var isArray = Array.isArray(obj); | ||
if (!isObj) { | ||
return obj; | ||
} | ||
if (isArray) { | ||
for (var i = 0, l = obj.length; i < l; i++) { | ||
var value = obj[i]; | ||
if (value === null || isEmpty(value)) { | ||
obj.splice(i, 1); | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(value); | ||
} | ||
} | ||
} else { | ||
for (var key in obj) { | ||
var _value = obj[key]; | ||
if (_value === null || isEmpty(_value)) { | ||
delete obj[key]; | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(_value); | ||
} | ||
} | ||
} | ||
return obj; | ||
}; | ||
return removeEmpty(original); | ||
}; | ||
var copy = function copy(source, isArr) { | ||
var to = source && !!source.pop || isArr ? [] : {}; | ||
for (var i in source) { | ||
to[i] = source[i]; | ||
} | ||
return to; | ||
}; | ||
var deepSet = (function (source, keys, update, merge) { | ||
if (merge === void 0) { | ||
@@ -77,35 +126,7 @@ merge = true; | ||
} | ||
} // if we are deleting clean out empty | ||
// objects and arrays | ||
if (shouldDelete) { | ||
var _last = next, | ||
_i = 0; | ||
for (; _i < l; _i++) { | ||
var _currentKey = keys[_i]; | ||
if (isEmpty(_last[_currentKey])) { | ||
delete _last[_currentKey]; | ||
return next; | ||
} | ||
_last = _last[_currentKey]; | ||
} | ||
} | ||
return next; | ||
} | ||
return removeNullAndEmpty(next); | ||
}); | ||
function copy(source, isArr) { | ||
var to = source && !!source.pop || isArr ? [] : {}; | ||
for (var i in source) { | ||
to[i] = source[i]; | ||
} | ||
return to; | ||
} | ||
var str = function str(val) { | ||
@@ -228,3 +249,3 @@ return typeof val === 'string'; | ||
if (modifiedType === 'object') { | ||
if (modifiedType === 'object' && modified[key] !== null) { | ||
// we pass through "ignored" for nested stuff, but not the ignored keys | ||
@@ -231,0 +252,0 @@ // those only apply at the top level |
@@ -40,3 +40,50 @@ import dlv from 'dlv'; | ||
const digitRe = /^\[\d+\]/; | ||
function deepSet (source, keys, update, merge = true) { | ||
const removeNullAndEmpty = original => { | ||
const removeEmpty = obj => { | ||
const isObj = typeof obj === 'object'; | ||
const isArray = Array.isArray(obj); | ||
if (!isObj) { | ||
return obj; | ||
} | ||
if (isArray) { | ||
for (let i = 0, l = obj.length; i < l; i++) { | ||
const value = obj[i]; | ||
if (value === null || isEmpty(value)) { | ||
obj.splice(i, 1); | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(value); | ||
} | ||
} | ||
} else { | ||
for (const key in obj) { | ||
const value = obj[key]; | ||
if (value === null || isEmpty(value)) { | ||
delete obj[key]; | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(value); | ||
} | ||
} | ||
} | ||
return obj; | ||
}; | ||
return removeEmpty(original); | ||
}; | ||
const copy = (source, isArr) => { | ||
let to = source && !!source.pop || isArr ? [] : {}; | ||
for (let i in source) to[i] = source[i]; | ||
return to; | ||
}; | ||
var deepSet = ((source, keys, update, merge = true) => { | ||
keys.split && (keys = keys.split('.')); | ||
@@ -67,33 +114,7 @@ keys = keys.map(key => digitRe.test(key) ? Number(key.slice(1, -1)) : key); | ||
} | ||
} // if we are deleting clean out empty | ||
// objects and arrays | ||
if (shouldDelete) { | ||
let _last = next, | ||
_i = 0; | ||
for (; _i < l; _i++) { | ||
const currentKey = keys[_i]; | ||
if (isEmpty(_last[currentKey])) { | ||
delete _last[currentKey]; | ||
return next; | ||
} | ||
_last = _last[currentKey]; | ||
} | ||
} | ||
return next; | ||
} | ||
return removeNullAndEmpty(next); | ||
}); | ||
function copy(source, isArr) { | ||
let to = source && !!source.pop || isArr ? [] : {}; | ||
for (let i in source) to[i] = source[i]; | ||
return to; | ||
} | ||
const str = val => typeof val === 'string'; | ||
@@ -192,3 +213,3 @@ | ||
if (modifiedType === 'object') { | ||
if (modifiedType === 'object' && modified[key] !== null) { | ||
// we pass through "ignored" for nested stuff, but not the ignored keys | ||
@@ -195,0 +216,0 @@ // those only apply at the top level |
@@ -40,3 +40,52 @@ import dlv from 'dlv'; | ||
var digitRe = /^\[\d+\]/; | ||
function deepSet (source, keys, update, merge) { | ||
var removeNullAndEmpty = function removeNullAndEmpty(original) { | ||
var removeEmpty = function removeEmpty(obj) { | ||
var isObj = typeof obj === 'object'; | ||
var isArray = Array.isArray(obj); | ||
if (!isObj) { | ||
return obj; | ||
} | ||
if (isArray) { | ||
for (var i = 0, l = obj.length; i < l; i++) { | ||
var value = obj[i]; | ||
if (value === null || isEmpty(value)) { | ||
obj.splice(i, 1); | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(value); | ||
} | ||
} | ||
} else { | ||
for (var key in obj) { | ||
var _value = obj[key]; | ||
if (_value === null || isEmpty(_value)) { | ||
delete obj[key]; | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(_value); | ||
} | ||
} | ||
} | ||
return obj; | ||
}; | ||
return removeEmpty(original); | ||
}; | ||
var copy = function copy(source, isArr) { | ||
var to = source && !!source.pop || isArr ? [] : {}; | ||
for (var i in source) { | ||
to[i] = source[i]; | ||
} | ||
return to; | ||
}; | ||
var deepSet = (function (source, keys, update, merge) { | ||
if (merge === void 0) { | ||
@@ -73,35 +122,7 @@ merge = true; | ||
} | ||
} // if we are deleting clean out empty | ||
// objects and arrays | ||
if (shouldDelete) { | ||
var _last = next, | ||
_i = 0; | ||
for (; _i < l; _i++) { | ||
var _currentKey = keys[_i]; | ||
if (isEmpty(_last[_currentKey])) { | ||
delete _last[_currentKey]; | ||
return next; | ||
} | ||
_last = _last[_currentKey]; | ||
} | ||
} | ||
return next; | ||
} | ||
return removeNullAndEmpty(next); | ||
}); | ||
function copy(source, isArr) { | ||
var to = source && !!source.pop || isArr ? [] : {}; | ||
for (var i in source) { | ||
to[i] = source[i]; | ||
} | ||
return to; | ||
} | ||
var str = function str(val) { | ||
@@ -224,3 +245,3 @@ return typeof val === 'string'; | ||
if (modifiedType === 'object') { | ||
if (modifiedType === 'object' && modified[key] !== null) { | ||
// we pass through "ignored" for nested stuff, but not the ignored keys | ||
@@ -227,0 +248,0 @@ // those only apply at the top level |
@@ -47,3 +47,52 @@ (function (global, factory) { | ||
var digitRe = /^\[\d+\]/; | ||
function deepSet (source, keys, update, merge) { | ||
var removeNullAndEmpty = function removeNullAndEmpty(original) { | ||
var removeEmpty = function removeEmpty(obj) { | ||
var isObj = typeof obj === 'object'; | ||
var isArray = Array.isArray(obj); | ||
if (!isObj) { | ||
return obj; | ||
} | ||
if (isArray) { | ||
for (var i = 0, l = obj.length; i < l; i++) { | ||
var value = obj[i]; | ||
if (value === null || isEmpty(value)) { | ||
obj.splice(i, 1); | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(value); | ||
} | ||
} | ||
} else { | ||
for (var key in obj) { | ||
var _value = obj[key]; | ||
if (_value === null || isEmpty(_value)) { | ||
delete obj[key]; | ||
removeEmpty(original); | ||
} else { | ||
removeEmpty(_value); | ||
} | ||
} | ||
} | ||
return obj; | ||
}; | ||
return removeEmpty(original); | ||
}; | ||
var copy = function copy(source, isArr) { | ||
var to = source && !!source.pop || isArr ? [] : {}; | ||
for (var i in source) { | ||
to[i] = source[i]; | ||
} | ||
return to; | ||
}; | ||
var deepSet = (function (source, keys, update, merge) { | ||
if (merge === void 0) { | ||
@@ -80,35 +129,7 @@ merge = true; | ||
} | ||
} // if we are deleting clean out empty | ||
// objects and arrays | ||
if (shouldDelete) { | ||
var _last = next, | ||
_i = 0; | ||
for (; _i < l; _i++) { | ||
var _currentKey = keys[_i]; | ||
if (isEmpty(_last[_currentKey])) { | ||
delete _last[_currentKey]; | ||
return next; | ||
} | ||
_last = _last[_currentKey]; | ||
} | ||
} | ||
return next; | ||
} | ||
return removeNullAndEmpty(next); | ||
}); | ||
function copy(source, isArr) { | ||
var to = source && !!source.pop || isArr ? [] : {}; | ||
for (var i in source) { | ||
to[i] = source[i]; | ||
} | ||
return to; | ||
} | ||
var str = function str(val) { | ||
@@ -231,3 +252,3 @@ return typeof val === 'string'; | ||
if (modifiedType === 'object') { | ||
if (modifiedType === 'object' && modified[key] !== null) { | ||
// we pass through "ignored" for nested stuff, but not the ignored keys | ||
@@ -234,0 +255,0 @@ // those only apply at the top level |
{ | ||
"name": "sinks", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Tools for object sync (get it?!), validation, diffing, and immutable deep setting", | ||
@@ -5,0 +5,0 @@ "source": "src/index.js", |
@@ -17,3 +17,3 @@ # sinks | ||
1. You have a large object and modified version of that large object, you want them to be the same, but you don't want to send the entire new object. | ||
1. You have a large object and a modified version of that large object, you want them to be the same, but you don't want to send the entire new object. | ||
@@ -45,3 +45,3 @@ 2. Defining/validating/updating objects stored in redux reducers. Updates are always immutable! | ||
const updatedObject = updateObj(obj1, { | ||
const updatedObject = updateObject(obj1, { | ||
'favoriteColors.foo.name': 'yellow', | ||
@@ -72,3 +72,3 @@ }) | ||
// setting a value to `null` deletes it | ||
const updatedObject = updateObj(obj1, { | ||
const updatedObject = updateObject(obj1, { | ||
'something.foo': null, | ||
@@ -95,3 +95,3 @@ }) | ||
// index as a number in your update path: | ||
const updated = updateObj(obj1, { | ||
const updated = updateObject(obj1, { | ||
'myStuff.0.description': 'skis', | ||
@@ -119,3 +119,3 @@ }) | ||
// instead of an object with a key named '0' | ||
const updated = updateObj(obj1, { | ||
const updated = updateObject(obj1, { | ||
'myStuff.[0].description': 'skis', | ||
@@ -132,4 +132,4 @@ }) | ||
console.log( | ||
updateObj(obj1, { | ||
'myStuff.[0].description': 'skis', | ||
updateObject(obj1, { | ||
'myStuff.0.description': 'skis', | ||
}) | ||
@@ -156,3 +156,3 @@ ) | ||
console.log( | ||
updateObj(obj1, { | ||
updateObject(obj1, { | ||
other: { | ||
@@ -171,2 +171,30 @@ nested: 'thing', | ||
Empty objects and arrays and `null` values are automatically removed. | ||
```js | ||
const obj1 = { | ||
name: 'Henrik', | ||
} | ||
// even if you set a deeply nested set of | ||
// objects and the very last value is empty | ||
// the whole chain of empty stuff is removed | ||
console.log( | ||
updateObject(obj1, { | ||
other: { | ||
nested: { | ||
// note final value is an empty array | ||
// once this is removed, the parent ones | ||
// will be empty. So the whole thing is | ||
// removed. | ||
hi: [], | ||
}, | ||
}, | ||
}) | ||
) | ||
// { | ||
// name: 'Henrik', | ||
// } | ||
``` | ||
### `setValue(obj1, keyPath, updatedValue)` | ||
@@ -300,2 +328,3 @@ | ||
- `2.0.0`: Now recursively removes all keys with values `{}`, `[]`, or `null` at the end of all set/update operations. | ||
- `1.0.0`: `getChanges` now takes an options object instead of just a boolean and that option option now can take a `ignoredKeys: []` option to ignore changes to specified top-level keys. | ||
@@ -302,0 +331,0 @@ - `0.0.1`: First public release. |
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
Sorry, the diff of this file is not supported yet
134518
1378
333