Skip to content

Commit

Permalink
Resolves #1270.
Browse files Browse the repository at this point in the history
Did simplify this a bit and remove some code.
We only need to go deep diffing when both ap and bp are POJOs. All else can be treated as diff.
This means that updating an object with an array property that has identical content but is another array instance, will be treated as a diff. This is ok. I don't think we earn so much by using getValueOf() to do some half-baked comparision between arrays.
  • Loading branch information
dfahlander committed Jun 10, 2021
1 parent 50b1ab1 commit d76c29e
Showing 1 changed file with 35 additions and 47 deletions.
82 changes: 35 additions & 47 deletions src/functions/get-object-diff.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,39 @@
import { keys, hasOwn, toStringTag, intrinsicTypeNameSet, isArray } from "./utils";
import { keys, hasOwn, toStringTag } from './utils';

export const getValueOf = (val:any, type: string) =>
type === "Array" ? ''+val.map(v => getValueOf(v, toStringTag(v))) :
type === "ArrayBuffer" ? ''+new Uint8Array(val) :
type === "Date" ? val.getTime() :
ArrayBuffer.isView(val) ? ''+new Uint8Array(val.buffer) :
val;
export function getObjectDiff(a: any, b: any, rv?: any, prfx?: string) {
// Compares objects a and b and produces a diff object.
rv = rv || {};
prfx = prfx || '';
keys(a).forEach((prop) => {
if (!hasOwn(b, prop)) {
// Property removed
rv[prfx + prop] = undefined;
} else {
var ap = a[prop],
bp = b[prop];
if (typeof ap === 'object' && typeof bp === 'object' && ap && bp) {
const apTypeName = toStringTag(ap);
const bpTypeName = toStringTag(bp);

export function getObjectDiff(a, b, rv?, prfx?) {
// Compares objects a and b and produces a diff object.
rv = rv || {};
prfx = prfx || '';
keys(a).forEach(prop => {
if (!hasOwn(b, prop))
rv[prfx+prop] = undefined; // Property removed
else {
var ap = a[prop],
bp = b[prop];
if (typeof ap === 'object' && typeof bp === 'object' && ap && bp)
{
const apTypeName = toStringTag(ap);
const bpTypeName = toStringTag(bp);

if (apTypeName === bpTypeName) {
if (intrinsicTypeNameSet[apTypeName] || isArray(ap)) {
// This is an intrinsic type. Don't go deep diffing it.
// Instead compare its value in best-effort:
// (Can compare real values of Date, ArrayBuffers and views)
if (getValueOf(ap, apTypeName) !== getValueOf(bp, bpTypeName)) {
rv[prfx + prop] = b[prop]; // Date / ArrayBuffer etc is of different value
}
} else {
// This is not an intrinsic object. Compare the it deeply:
getObjectDiff(ap, bp, rv, prfx + prop + ".");
}
} else {
rv[prfx + prop] = b[prop];// Property changed to other type
}
} else if (ap !== bp)
rv[prfx + prop] = b[prop];// Primitive value changed
}
});
keys(b).forEach(prop => {
if (!hasOwn(a, prop)) {
rv[prfx+prop] = b[prop]; // Property added
if (apTypeName !== bpTypeName) {
rv[prfx + prop] = b[prop]; // Property changed to other type
} else if (apTypeName === 'Object') {
// Pojo objects (not Date, ArrayBuffer, Array etc). Go deep.
getObjectDiff(ap, bp, rv, prfx + prop + '.');
} else if (ap !== bp) {
// Values differ.
// Could have checked if Date, arrays or binary types have same
// content here but I think that would be a suboptimation.
// Prefer simplicity.
rv[prfx + prop] = b[prop];
}
});
return rv;
} else if (ap !== bp) rv[prfx + prop] = b[prop]; // Primitive value changed
}
});
keys(b).forEach((prop) => {
if (!hasOwn(a, prop)) {
rv[prfx + prop] = b[prop]; // Property added
}
});
return rv;
}

0 comments on commit d76c29e

Please sign in to comment.