@z-core/object-diff

1.0.0 • Public • Published

object-diff.js

A tiny method to compare two JavaScript objects.

It's initially created for a tool I called "React Wasted" to detect specific differences in state vs. prevState & props vs. prevProps to improve re-rendering and optimise performance. Tool was never opensourced, but maybe one day 🤞

As a return value this method either returns null in case there are no deep differences of an array or object (depending on input) with keys as array indexes or object properties, and values as an array [firstObject, secondObject].

Usage

Install: npm i -S @z-core/object-diff

Inlcude in the script: import getDiff from '@z-core/object-diff';

// when no deep differences
getDiff(1, 1); // => null
getDiff('1', '1'); // => null
getDiff(true, true); // => null
getDiff([1], [1]); // => null
getDiff({ a: 1 }, { a: 1 }); // => null
// when has differences
getDiff(1, 2); // => [1, 2]
getDiff('1', '2'); // => ['1', '2']
getDiff(true, false); // => [true, false]
getDiff([1], [2]); // => [[1, 2]]
getDiff({ a: 1 }, { a: 2 }); // => { a: [1, 2] }
getDiff({ a: 1, b: 1 }, { a: 2, b: 1 }); // => { a: [1, 2] }, no `b` property, as that part is equal

Package Sidebar

Install

npm i @z-core/object-diff

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

5.75 kB

Total Files

8

Last publish

Collaborators

  • vasyl-zubach