UNPKG

@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

24 lines (23 loc) 1.29 kB
import { TYPE_NAMES, ANY_PROPERTY_KEY } from '../../constants'; import { getTypeForFilter } from '../../utils'; import { compareObjects } from '../object/object-compare.js'; export function deepCompare(actual, expected, comparator, config, anyPropertyKey = ANY_PROPERTY_KEY, matchAgainstAnyProp = false, dontMatchWholeObject = false, currentDepth = 0) { if (currentDepth > config.maxDepth) { return false; } const actualType = getTypeForFilter(actual); const expectedType = getTypeForFilter(expected); if (expectedType === TYPE_NAMES.STRING && expected.charAt(0) === '!') { return !deepCompare(actual, expected.substring(1), comparator, config, anyPropertyKey, matchAgainstAnyProp, dontMatchWholeObject, currentDepth + 1); } if (Array.isArray(actual)) { return actual.some((item) => deepCompare(item, expected, comparator, config, anyPropertyKey, matchAgainstAnyProp, dontMatchWholeObject, currentDepth + 1)); } if (actualType === TYPE_NAMES.OBJECT) { return compareObjects(actual, expected, comparator, config, anyPropertyKey, matchAgainstAnyProp, dontMatchWholeObject, expectedType, currentDepth); } if (actualType === TYPE_NAMES.FUNCTION) { return false; } return comparator(actual, expected); }