shallow-changes
Compares objects or arrays and categorizes all keys as
added
,deleted
,equal
orupdated
Shallow-Changes works in any JavaScript runtime. It has no dependencies and supports IE9+.
Install
$ npm install --save shallow-changes
// use as ES6 module;
// use as CommonJS modulevar shallowChanges = ;
<!-- use in HTML, available as shallowChanges, only 501b gzipped -->
Usage
;// returns { added: [], deleted: [], equal: [], updated: [] }// arrays contain classified property keys
If any of the values is not of type object (not an object, array or instance) the return value is false
.
Config
Shallow-Changes has an optional third argument. It receives an object that can contain functions to overwrite the compare function is
and/or the defined function isDefined
.
The compare function is
receives two values and returns a boolean. The default compare function tests if the values are strict equal ===
or both are NaN
.
The defined function isDefined
receives a value with a key and returns a boolean. If the function returns false the property will be ignored. The default defined function tests if the value is not undefined
, so a property that changes from undefined
to any value will be classified as added
, not updated
.
See tests for usage.
;