-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor _global and getObjectDiff to own modules
- Loading branch information
1 parent
3148a56
commit 50b1ab1
Showing
8 changed files
with
68 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { keys, hasOwn, toStringTag, intrinsicTypeNameSet, isArray } 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, 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 | ||
} | ||
}); | ||
return rv; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare var global; | ||
export const _global: any = | ||
typeof globalThis !== 'undefined' ? globalThis : | ||
typeof self !== 'undefined' ? self : | ||
typeof window !== 'undefined' ? window : | ||
global; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters