merge-change
Version:
Advanced library for deep merging, patching, and immutable updates of data structures. Features declarative operations for specific merging behaviors, property management, custom type merging rules, and difference tracking. Supports complex data transform
55 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.match = match;
const get_1 = require("../get");
const flat_1 = require("../flat");
/**
* Checking properties in an object. Nested properties are specified using a path to them through separator
* @param value Object to check, which should contain condition properties
* @param condition Target object, all properties of which should be in value
* @param data Data for substitution in the condition template. For example, '$session.user.name' in condition will be substituted with the value from data.session.user.name
* @param separator Separator for templated value
* @param errors If an array is passed, the names of properties that don't match will be added to it
* @returns Whether the value matches the condition
*/
function match(value, condition = {}, data = {}, separator = '.', errors) {
let result = true;
const flatValue = (0, flat_1.flat)(value, separator);
if (typeof condition !== 'object' || condition === null) {
return condition === flatValue;
}
const conditionObj = condition;
const keys = Object.keys(conditionObj);
for (const key of keys) {
const conditionValue = conditionObj[key];
const valueAtKey = key in flatValue ? flatValue[key] : undefined;
if (conditionValue !== valueAtKey) {
// templated value
if (typeof conditionValue === 'string' && conditionValue.startsWith('$')) {
const realCondition = (0, get_1.get)(data, conditionValue.substring(1), undefined, separator);
if (realCondition === valueAtKey && key in flatValue) {
break;
}
}
let arrayEq = false;
if (Array.isArray(conditionValue) &&
Array.isArray(valueAtKey) &&
conditionValue.length === valueAtKey.length) {
arrayEq = true; // possibly match
for (let i = 0; i < conditionValue.length; i++) {
if (!match(valueAtKey[i], conditionValue[i], data, separator)) {
arrayEq = false;
break;
}
}
}
if (!arrayEq) {
if (errors)
errors.push(key);
result = false;
}
}
}
return result;
}
//# sourceMappingURL=index.js.map