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

48 lines 1.78 kB
import { memoization } from '../utils/memoization'; export const applyStringOperators = (value, operators, caseSensitive = false) => { if (typeof value !== 'string') return false; const strValue = caseSensitive ? value : value.toLowerCase(); if (operators.$startsWith !== undefined) { const compareValue = caseSensitive ? operators.$startsWith : operators.$startsWith.toLowerCase(); if (!strValue.startsWith(compareValue)) return false; } if (operators.$endsWith !== undefined) { const compareValue = caseSensitive ? operators.$endsWith : operators.$endsWith.toLowerCase(); if (!strValue.endsWith(compareValue)) return false; } if (operators.$contains !== undefined) { const compareValue = caseSensitive ? operators.$contains : operators.$contains.toLowerCase(); if (!strValue.includes(compareValue)) return false; } if (operators.$regex !== undefined) { const regex = getOrCreateRegex(operators.$regex, caseSensitive); if (!regex.test(value)) return false; } if (operators.$match !== undefined) { const regex = getOrCreateRegex(operators.$match, caseSensitive); if (!regex.test(value)) return false; } return true; }; function getOrCreateRegex(pattern, caseSensitive) { if (pattern instanceof RegExp) { return pattern; } const flags = caseSensitive ? '' : 'i'; const cached = memoization.getCachedRegex(pattern, flags); if (cached) { return cached; } const regex = new RegExp(pattern, flags); memoization.setCachedRegex(pattern, regex, flags); return regex; } //# sourceMappingURL=string.operators.js.map