@mcabreradev/filter
Version:
A powerful, SQL-like array filtering library for TypeScript and JavaScript with advanced pattern matching, MongoDB-style operators, deep object comparison, and zero dependencies
30 lines (29 loc) • 1.33 kB
JavaScript
import { ANY_PROPERTY_KEY } from '../../constants';
import { isFunction, isUndefined } from '../../utils';
import { deepCompare } from '../deep/deep-compare.js';
export function compareAgainstAnyProperty(actual, expected, comparator, config, anyPropertyKey, dontMatchWholeObject, currentDepth) {
for (const key in actual) {
if (key.charAt &&
key.charAt(0) !== ANY_PROPERTY_KEY &&
deepCompare(actual[key], expected, comparator, config, anyPropertyKey, true, false, currentDepth + 1)) {
return true;
}
}
return dontMatchWholeObject
? false
: deepCompare(actual, expected, comparator, config, anyPropertyKey, false, false, currentDepth + 1);
}
export function compareAllProperties(actual, expected, comparator, config, anyPropertyKey, currentDepth) {
for (const key in expected) {
const expectedVal = expected[key];
if (isFunction(expectedVal) || isUndefined(expectedVal)) {
continue;
}
const matchAnyProperty = key === anyPropertyKey;
const actualVal = matchAnyProperty ? actual : actual[key];
if (!deepCompare(actualVal, expectedVal, comparator, config, anyPropertyKey, matchAnyProperty, matchAnyProperty, currentDepth + 1)) {
return false;
}
}
return true;
}