More changes to dexie observable change helper

This commit is contained in:
Mitchell McCaffrey 2021-05-27 17:19:02 +10:00
parent 3b3e9fba97
commit dd130a008e
3 changed files with 37 additions and 8 deletions

View File

@ -30,8 +30,8 @@
"konva": "^7.2.5",
"lodash.clonedeep": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.omit": "^4.5.0",
"lodash.set": "^4.3.2",
"lodash.unset": "^4.5.2",
"normalize-wheel": "^1.0.1",
"pepjs": "^0.5.3",
"polygon-clipping": "^0.15.2",

View File

@ -1,7 +1,32 @@
import set from "lodash.set";
import omit from "lodash.omit";
import unset from "lodash.unset";
import cloneDeep from "lodash.clonedeep";
/**
* Remove all empty values from an object recursively
* @param {Object} obj
*/
function trimArraysInObject(obj) {
for (let key in obj) {
const value = obj[key];
if (Array.isArray(value)) {
let arr = [];
for (let i = 0; i < value.length; i++) {
const el = value[i];
if (typeof el === "object") {
arr.push(trimArraysInObject(el));
} else if (el !== undefined) {
arr.push(el);
}
}
obj[key] = arr;
} else if (typeof obj[key] === "object") {
obj[key] = trimArraysInObject(obj[key]);
}
}
return obj;
}
export function applyObservableChange(change) {
// Custom application of dexie change to fix issue with array indices being wrong
// https://github.com/dfahlander/Dexie.js/issues/1176
@ -10,10 +35,14 @@ export function applyObservableChange(change) {
const changes = Object.entries(change.mods).reverse();
for (let [key, value] of changes) {
if (value === null) {
obj = omit(obj, [key]);
unset(obj, key);
} else {
obj = set(obj, key, value);
}
}
// Trim empty values from calling unset on arrays
obj = trimArraysInObject(obj);
return obj;
}

View File

@ -8591,11 +8591,6 @@ lodash.memoize@^4.1.2:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
lodash.omit@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=
lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
@ -8631,6 +8626,11 @@ lodash.uniq@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
lodash.unset@^4.5.2:
version "4.5.2"
resolved "https://registry.yarnpkg.com/lodash.unset/-/lodash.unset-4.5.2.tgz#370d1d3e85b72a7e1b0cdf2d272121306f23e4ed"
integrity sha1-Nw0dPoW3Kn4bDN8tJyEhMG8j5O0=
"lodash@>=3.5 <5", lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.5:
version "4.17.20"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"