isMatch
lang
Performs a partial deep comparison between object and source to determine if object contains equivalent property values.
Installation
Import
import { isMatch } from '@tulx/utils';Source Code
Implementation
/**
* Performs a partial deep comparison between object and source to determine if object contains equivalent property values.
*
* @param object - The object to inspect.
* @param source - The object of property values to match.
* @returns Returns true if object is a match, else false.
*
* @example
* ```ts
* const object = { 'a': 1, 'b': 2 };
* isMatch(object, { 'b': 2 }); // true
* isMatch(object, { 'b': 1 }); // false
* ```
*/
export function isMatch(
object: Record<string, unknown>,
source: Record<string, unknown>
): boolean {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
const objValue = object[key];
const srcValue = source[key];
if (
typeof srcValue === 'object' &&
srcValue !== null &&
!Array.isArray(srcValue)
) {
if (
typeof objValue !== 'object' ||
objValue === null ||
Array.isArray(objValue)
) {
return false;
}
if (
!isMatch(
objValue as Record<string, unknown>,
srcValue as Record<string, unknown>
)
) {
return false;
}
} else if (objValue !== srcValue) {
return false;
}
}
}
return true;
}
Example
import { isMatch } from '@tulx/utils';
const object = { 'a': 1, 'b': 2 };
isMatch(object, { 'b': 2 }); // true
isMatch(object, { 'b': 1 }); // 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.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.