es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
31 lines (30 loc) • 1.08 kB
JavaScript
import { cloneDeep } from "../../object/cloneDeep.mjs";
import { conformsTo } from "./conformsTo.mjs";
//#region src/compat/predicate/conforms.ts
/**
* Creates a function that invokes the predicate properties of `source` with the corresponding property values of a given object, returning `true` if all predicates return truthy, else `false`.
*
* Note: The created function is equivalent to `conformsTo` with source partially applied.
*
* @param source The object of property predicates to conform to.
* @returns Returns the new spec function.
*
* @example
* const isPositive = (n) => n > 0;
* const isEven = (n) => n % 2 === 0;
* const predicates = { a: isPositive, b: isEven };
* const conform = conforms(predicates);
*
* console.log(conform({ a: 2, b: 4 })); // true
* console.log(conform({ a: -1, b: 4 })); // false
* console.log(conform({ a: 2, b: 3 })); // false
* console.log(conform({ a: 0, b: 2 })); // false
*/
function conforms(source) {
source = cloneDeep(source);
return function(object) {
return conformsTo(object, source);
};
}
//#endregion
export { conforms };