deep-compare-by
v1.0.0
Published
Deep compare with a function
Downloads
5
Maintainers
Readme
Deep compare with a function
const compare = require("deep-compare-by");
const cmp = compare((a, b) => (
(typeof a === "function") &&
(typeof b === "function") &&
(a.toString() == b.toString())
));
cmp({
a:1,
b:{
c:2,
d:()=>4
}
},{
a:1,
b:{
c:2,
d:()=>4
}
}); // returns true
more examples can be found in test.js
Curryable
compare(test)(lhs)(rhs)
compare(test,lhs)(rhs)
compare(test)(lhs,rhs)
compare(test,lhs,rhs)
const cmp = compare(null,{a:1});
cmp({a:0}); // false
cmp({a:1}); // true
cmp({a:1,b:2}); // false
Arguments
compare(compareFunctions,lhs,rhs);
Compare Functions
This set of functions will be run to compare each object or value in the passed items, if any of the functions return true the corresponding items will be considered equivalent, there is an implicit test (a,b)=>a===b
which exists unless using noDefault
compare(null); // same as compare []
compare((a,b)=>a<b); // same as [(a,b)=>a<b]
const cmp = compare(null);
const cmpt = compare([
(a, b) => (typeof a === typeof b),
(a, b) => (a === 9)
]);
cmp(1, 2); // false
cmpt(1, 2); // true
cmp(9, "8"); // false
cmpt(8, "8"); // false
cmpt(9, "8"); // true
Modifiers
Modifiers call a modified version of the function
noDefault
Call without implicit test (a,b)=>a===b
const cmp = compare.noDefault((a, b) => b === 3);
cmp({ a: 1 }, { a: 1 }); // false
cmp({ a: 1 }, { a: 3 }); // true
const cmp2 = compare((a, b) => b === 3, { a: 1 });
cmp2.noDefault({ a: 1 }); // false
cmp2.noDefault({ a: 3 }); // true
failPath
Instead of false, it returns the key path to the point where the compare failed. If it succeeds, return true;
const val1 = {
a: { b: { c: 2 } },
d: 3,
e: { f: 4 },
};
const val2 = {
a: { b: { c: 3 } },
d: 3,
e: { f: 4 },
};
compare(null).failPath(val1, val2); // ['a', 'b', 'c']
compare(null)(val1).failPath(val2); // ['a', 'b', 'c']
Content
Return data about the function
tests
Returns array of tests currently curried in
const cmp = compare(null);
cmp.tests(); // []
const cmpt = compare((a, b) => (typeof a === typeof b));
cmpt.tests(); // [(a, b) => (typeof a === typeof b)]
lhs
Returns lhs value currently curried in, otherwise undefined
compare(null,5).lhs(); // 5