UNPKG

nope-validator

Version:
104 lines (101 loc) 3.09 kB
import { NopeReference } from './NopeReference.js'; function resolvePathFromContext(path, context) { const optionWithPath = path.split('../'); const depth = optionWithPath.length - 1; const key = optionWithPath[optionWithPath.length - 1]; let ctx = context; for (let i = 0; i < depth; i++) { ctx = ctx === null || ctx === void 0 ? void 0 : ctx.___parent; } if (ctx && key !== undefined && key !== null) { return ctx[key]; } return key; } function resolveNopeRefsFromKeys(options, context) { const resolvedOptions = options.map((option) => { return resolvePathFromContext(option, context); }); return resolvedOptions; } function resolveNopeRef(option, context) { if (option instanceof NopeReference) { return resolvePathFromContext(option.key, context); } return option; } function deepEquals(a, b) { if (typeof a == 'object' && a != null && typeof b == 'object' && b != null) { if (a === b) { return true; } let aCount = 0; let bCount = 0; // eslint-disable-next-line @typescript-eslint/no-unused-vars for (const _ in a) { aCount++; } // eslint-disable-next-line @typescript-eslint/no-unused-vars for (const _ in b) { bCount++; } if (aCount - bCount !== 0) { return false; } for (const key in a) { if (!(key in b) || !deepEquals(a[key], b[key])) { return false; } } for (const key in b) { if (!(key in a) || !deepEquals(b[key], a[key])) { return false; } } return true; } return a === b; } function pathToArray(path) { return path.split(/[,[\].]/g).filter(Boolean); } function getFromPath(path, entry, dropLast = false) { if (!path) { return undefined; } let pathArray = pathToArray(path); pathArray = dropLast ? pathArray.slice(0, -1) : pathArray; let value = entry; for (const key of pathArray) { value = value[key]; } return value; } function runValidators(tasks, entry, context) { let done = false; return tasks.reduce(function (previous, next) { if (done) { return previous; } return previous .then(function (error) { if (error) { done = true; return error; } return next(entry, context); }) .catch(function (error) { if (error) { done = true; return error; } return next(entry, context); }); }, Promise.resolve()); } function isNil(entry) { return !!(entry === undefined || entry === null); } export { deepEquals, getFromPath, isNil, pathToArray, resolveNopeRef, resolveNopeRefsFromKeys, runValidators }; //# sourceMappingURL=utils.js.map