conformsTo
lang
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.
Installation
Import
import { conformsTo } from '@tulx/utils';Source Code
Implementation
/**
* Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.
*
* @param object - The object to inspect.
* @param source - The object of property predicates to conform to.
* @returns Returns true if object conforms, else false.
*
* @example
* ```ts
* const object = { 'a': 1, 'b': 2 };
* conformsTo(object, { 'b': (n: number) => n > 1 }); // true
* conformsTo(object, { 'b': (n: number) => n > 2 }); // false
* ```
*/
export function conformsTo<T extends Record<string, unknown>>(
object: T,
source: Record<string, (value: unknown) => boolean>
): boolean {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
const predicate = source[key];
const value = object[key];
if (!predicate(value)) {
return false;
}
}
}
return true;
}
Example
import { conformsTo } from '@tulx/utils';
const object = { 'a': 1, 'b': 2 };
conformsTo(object, { 'b': (n: number) => n > 1 }); // true
conformsTo(object, { 'b': (n: number) => n > 2 }); // falseRelated Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
eq
Performs a SameValueZero comparison between two values to determine if they are equivalent.