From f7628f5bc27a1c8393aad708f9926b5443957406 Mon Sep 17 00:00:00 2001 From: therohk Date: Sun, 25 Aug 2024 03:29:53 +0530 Subject: [PATCH] minor upgrades --- src/diff-lib/deep-diff.ts | 57 +++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/diff-lib/deep-diff.ts b/src/diff-lib/deep-diff.ts index 64d69fc..e31c1e1 100644 --- a/src/diff-lib/deep-diff.ts +++ b/src/diff-lib/deep-diff.ts @@ -12,7 +12,10 @@ export { observableDiff }; export { orderIndependentObservableDiff }; export { applyChange, revertChange }; export { applyDiff }; //prefer merge -export { getOrderIndependentHash }; //for tests + +//for tests +export { realTypeOf }; +export { getOrderIndependentHash }; type DiffNew = { readonly kind: "N"; @@ -43,11 +46,6 @@ type DiffArray = { type Diff = DiffNew | DiffDeleted | DiffEdit | DiffArray; type DiffKind = Diff["kind"]; -type Primitive = string | number | boolean; -type PrimitiveAll = Primitive | bigint | symbol; -type FieldKey = string | number | symbol; -type FieldPath = FieldKey[]; - type PreFilterFunction = (path: any[], key: any) => boolean; type PreFilterObject = { prefilter?(path: any[], key: any): boolean; @@ -63,9 +61,10 @@ type Accumulator = { type Observer = (diff: Diff) => void; +type FieldKey = string | number | symbol; +type FieldPath = FieldKey[]; // type StackItem = { lhs: LHS, rhs: RHS }; - const typeNormalizer: PreFilterObject = { normalize: function (currentPath: any, key: any, lhs: any, rhs: any): [any, any] { if (realTypeOf(lhs) === 'regexp' && realTypeOf(rhs) === 'regexp') { @@ -148,7 +147,6 @@ function orderIndependentObservableDiff( deepDiff(lhs, rhs, changes, prefilter, path, key, stack, true); } -// function deepDiff(lhs, rhs, changes, prefilter, path, key, stack, orderIndependent) { function deepDiff( lhs: LHS, rhs: RHS, @@ -224,9 +222,6 @@ function deepDiff( return; } - let i: number, j: number; - var k, other; - if (realTypeOf(lhs) === 'date' && ((lhs as Date).valueOf() - (rhs as Date).valueOf()) !== 0) { changes.push({ @@ -236,6 +231,8 @@ function deepDiff( rhs } as DiffEdit); new Date() } else if (ltype === 'object' && lhs !== null && rhs !== null) { + let i: number, j: number; + let other = false; for (i = stack.length - 1; i > -1; --i) { if (stack[i].lhs === lhs) { other = true; @@ -277,22 +274,20 @@ function deepDiff( deepDiff(lhs[i], rhs[i], changes, prefilter, currentPath, i, stack, orderIndependent); } } else { - // let k: FieldKey; - // let other: number; const akeys = [...Object.keys(lhs), ...Object.getOwnPropertySymbols(lhs)]; const pkeys = [...Object.keys(rhs), ...Object.getOwnPropertySymbols(rhs)]; for (i = 0; i < akeys.length; ++i) { - k = akeys[i]; - other = pkeys.indexOf(k); - if (other >= 0) { + const k = akeys[i]; + const ki = pkeys.indexOf(k); + if (ki >= 0) { deepDiff(lhs[k], rhs[k], changes, prefilter, currentPath, k, stack, orderIndependent); - pkeys[other] = null; + pkeys[ki] = null; } else { deepDiff(lhs[k], undefined, changes, prefilter, currentPath, k, stack, orderIndependent); } } for (i = 0; i < pkeys.length; ++i) { - k = pkeys[i]; + const k = pkeys[i]; if (k) { deepDiff(undefined, rhs[k], changes, prefilter, currentPath, k, stack, orderIndependent); } @@ -509,15 +504,24 @@ function revertArrayChange( function arrayRemove( arr: any[], - from: number, - to?: number + index: number, ): any[] { - const rest = arr.slice((to || from) + 1 || arr.length); - arr.length = from < 0 ? arr.length + from : from; - arr.push.apply(arr, rest); + index = index < 0 ? arr.length + index : index; + arr.splice(index, 1); return arr; } +// function arrayRemove( +// arr: any[], +// from: number, +// to?: number +// ): any[] { +// const rest = arr.slice((to || from) + 1 || arr.length); +// arr.length = from < 0 ? arr.length + from : from; +// arr.push.apply(arr, rest); +// return arr; +// } + function realTypeOf(val: any): string { const type = typeof val; if (type !== 'object') { @@ -547,20 +551,21 @@ function getOrderIndependentHash(val: any): number { // Addition is commutative so this is order indep accum += getOrderIndependentHash(item); }); - const arrayString = '[type: array, hash: ' + accum + ']'; + const arrayString = `[type: array, hash: ${accum}]`; return accum + hashThisString(arrayString); } if (type === 'object') { for (let key in val) { if (val.hasOwnProperty(key)) { - const keyValueString = '[ type: object, key: ' + key + ', value hash: ' + getOrderIndependentHash(val[key]) + ']'; + const keyValueHash = getOrderIndependentHash(val[key]); + const keyValueString = `[ type: object, key: ${key}, value hash: ${keyValueHash}]`; accum += hashThisString(keyValueString); } } return accum; } // Non object, non array...should be good? - const stringToHash = '[ type: ' + type + ' ; value: ' + val + ']'; + const stringToHash = `[ type: ${type} ; value: ${val}]`; return accum + hashThisString(stringToHash); }