strict-diff
v0.0.3
Published
Find all observable difference between two values.
Downloads
547
Maintainers
Readme
Features
- Strict: Finds the most minuscule differences between values
- Structured: Diff objects have paths and left vs right values
- Lazy: Returns a lazy iterable over the diffs
Install
$ npm i strict-diffUsage
import strictDiff from 'strict-diff'
// Primitive value diff
console.log([...strictDiff(1, 2)])
//=> [{ kind: 'value', path: [], left: 1, right: 2 }]
// Type diff
console.log([...strictDiff(null, undefined)])
//=> [{ kind: 'type', path: [], left: 'null', right: 'undefined' }]
// Object property value diff
console.log([...strictDiff({ a: 1 }, { a: 2 })])
//=> [
// {
// kind: 'value',
// path: [
// { kind: 'property', index: 0, key: 'a' },
// { kind: 'internal-slot', slot: 'Value' },
// ],
// left: 1,
// right: 2,
// },
// ]
// Object key diff
console.log([...strictDiff({ a: 1 }, { b: 1 })])
//=> [{ kind: 'key', path: [], index: 0, left: 'a', right: 'b' }]
// Property descriptor diff (non-writable vs writable)
const left = Object.defineProperty({}, `a`, {
value: 1,
writable: false,
enumerable: true,
configurable: true,
})
console.log([...strictDiff(left, { a: 1 })])
//=> [
// {
// kind: 'value',
// path: [
// { kind: 'property', index: 0, key: 'a' },
// { kind: 'internal-slot', slot: 'Writable' },
// ],
// left: false,
// right: true,
// },
// ]
// Reference structure diff (shared object used at different positions)
const shared1 = {}
const shared2 = {}
const leftArray = [shared1, shared2, shared1, shared2]
const rightArray = [shared1, shared2, shared1, shared1]
console.log([...strictDiff(leftArray, rightArray)])
//=> [
// {
// kind: 'reference',
// path: [
// { kind: 'property', index: 3, key: 3 },
// { kind: 'internal-slot', slot: 'Value' },
// ],
// leftFirstSeenPath: [
// { kind: 'property', index: 1, key: 1 },
// { kind: 'internal-slot', slot: 'Value' },
// ],
// rightFirstSeenPath: [
// { kind: 'property', index: 0, key: 0 },
// { kind: 'internal-slot', slot: 'Value' },
// ],
// },
// ]
// Lazily iterated
const diffs = strictDiff({ a: 1, b: 2 }, { a: 99, b: 99 })
const [firstDiff] = diffs
console.log(firstDiff)
//=> {
// kind: 'value',
// path: [
// { kind: 'property', index: 0, key: 'a' },
// { kind: 'internal-slot', slot: 'Value' },
// ],
// left: 1,
// right: 99,
// }See the tests for other example diffs.
Contributing
Stars are always welcome!
For bugs and feature requests, please create an issue.
