Deep compare with a function
npm install deep-compare-byDeep compare with a function
``js`
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.jsCurryable
js`
compare(test)(lhs)(rhs)
compare(test,lhs)(rhs)
compare(test)(lhs,rhs)
compare(test,lhs,rhs)`js`
const cmp = compare(null,{a:1});
cmp({a:0}); // false
cmp({a:1}); // true
cmp({a:1,b:2}); // false`Arguments
js`
compare(compareFunctions,lhs,rhs);(a,b)=>a===bCompare 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 which exists unless using noDefault`js`
compare(null); // same as compare []
compare((a,b)=>aaconst 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(a,b)=>a===bModifiers
Modifiers call a modified version of the function$3
Call without implicit test
`js`
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
Instead of false, it returns the key path to the point where the compare failed. If it succeeds, return true;
`js`
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']
js
const cmp = compare(null);
cmp.tests(); // []
const cmpt = compare((a, b) => (typeof a === typeof b));
cmpt.tests(); // [(a, b) => (typeof a === typeof b)]
`
$3
Returns lhs value currently curried in, otherwise undefined
`js
compare(null,5).lhs(); // 5
``