es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
34 lines (31 loc) • 918 B
JavaScript
import { isMatch } from './isMatch.mjs';
import { toKey } from '../_internal/toKey.mjs';
import { cloneDeep } from '../object/cloneDeep.mjs';
import { get } from '../object/get.mjs';
import { has } from '../object/has.mjs';
function matchesProperty(property, source) {
switch (typeof property) {
case 'object': {
if (Object.is(property?.valueOf(), -0)) {
property = '-0';
}
break;
}
case 'number': {
property = toKey(property);
break;
}
}
source = cloneDeep(source);
return function (target) {
const result = get(target, property);
if (result === undefined) {
return has(target, property);
}
if (source === undefined) {
return result === undefined;
}
return isMatch(result, source);
};
}
export { matchesProperty };