'use strict'; /* * Copyright (c) 2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ /** * @see https://github.com/express-validator/express-validator/blob/bec1dcbaa29002dcd21093ec84818c4671063b5d/src/field-selection.ts#L214 * @param parts */ function arrayToPath(parts) { return parts.reduce((prev, segment)=>{ let part = ''; segment = segment.replace(/^\[(\d+)]$/g, '\\[$1]'); segment = segment.replace(/\./g, '\\.'); if (/^\d+$/.test(segment)) { // Index access part = `[${segment}]`; } else if (prev) { // Object key access part = `.${segment}`; } else { // Top level key part = segment; } return prev + part; }, ''); } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ const BRACKET_NUMBER_REGEX = RegExp("(?expandPathVerboseInternal(data[key], arrayToPath(rest), currPath.concat(key), currMatches.concat(key))); } if (key === Character.GLOBSTAR) { return Object.keys(data).flatMap((key)=>{ const nextPath = currPath.concat(key); const value = data[key]; // recursively find matching sub-paths & skip the first remaining segment, if it matches the current key const children = expandPathVerboseInternal(value, arrayToPath(segments), nextPath, [ key ]).concat(rest[0] === key ? expandPathVerboseInternal(value, arrayToPath(rest.slice(1)), nextPath, []) : []); const pathMatches = []; const output = []; for(let i = 0; i < children.length; i++){ /* istanbul ignore next */ if (pathMatches.indexOf(children[i].value) !== -1) { continue; } pathMatches.push(children[i].value); output.push({ value: children[i].value, matches: children[i].matches.length > 0 ? [ ...currMatches, children[i].matches.flat() ] : currMatches }); } return output; }); } return expandPathVerboseInternal(data[key], rest, currPath.concat(key), currMatches); } /** * Verbose expand wildcard and glob patterns. * Track wildcard/glob pattern matches. * * @param data * @param path */ function expandPathVerbose(data, path) { return expandPathVerboseInternal(data, path); } /** * Expand wildcard and glob patterns to paths. * * @param data * @param path */ function expandPath(data, path) { return expandPathVerbose(data, path).map((el)=>el.value); } function getPathValue(data, path) { const parts = Array.isArray(path) ? path : pathToArray(path); let res; let temp = data; let index = 0; while(index < parts.length){ if (temp === null || typeof temp === 'undefined') { break; } if (parts[index] in Object(temp)) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error temp = temp[parts[index]]; } else { break; } if (index === parts.length - 1) { res = temp; } index++; } return res; } const NUMBER_REGEX = /^\d+$/; function setPathValue(data, path, value) { const parts = Array.isArray(path) ? path : pathToArray(path); let temp = data; let index = 0; while(index < parts.length){ /* istanbul ignore next */ if (!Array.isArray(temp) && !isObject(temp)) { break; } const key = parts[index]; // [foo, '0'] if (typeof temp[key] === 'undefined') { const match = NUMBER_REGEX.test(key); if (match) { temp[key] = []; } else { temp[key] = {}; } } if (index === parts.length - 1) { temp[key] = value; break; } index++; temp = temp[key]; } return data; } class PathInfo { get value() { if (typeof this._value !== 'undefined') { return this._value; } if (this.pathParts.length > 0) { this._value = getPathValue(this.data, this.pathParts); } else { this._value = this.data; } return this._value; } get name() { if (this.pathParts.length > 0) { return this.pathParts[this.pathParts.length - 1]; } return null; } get parent() { if (typeof this._parent !== 'undefined') { return this._parent; } if (this.pathParts.length === 0) { this._parent = null; return this._parent; } if (this.pathParts.length > 1) { this._parent = new PathInfo(this.data, this.pathParts.slice(0, this.pathParts.length - 1)); } else { this._parent = new PathInfo(this.data, []); } return this._parent; } get exists() { if (typeof this._exists !== 'undefined') { return this._exists; } if (!this.name || !this.parent) { this._exists = true; return this._exists; } if (this.parent.value !== null && typeof this.parent.value !== 'undefined') { this._exists = this.name in Object(this.parent.value); } else { this._exists = false; } return this._exists; } constructor(data, path){ this.data = data; if (Array.isArray(path)) { this.pathParts = path; } else { this.pathParts = pathToArray(path); } } } function getPathInfo(data, path) { return new PathInfo(data, path); } function removePath(data, path) { const parts = Array.isArray(path) ? path : pathToArray(path); let temp = data; let index = 0; while(index < parts.length){ /* istanbul ignore next */ if (!Array.isArray(temp) && !isObject(temp)) { break; } const key = parts[index]; if (typeof temp[key] === 'undefined') { break; } if (index === parts.length - 1) { delete temp[key]; break; } index++; temp = temp[key]; } } exports.BRACKET_NUMBER_REGEX = BRACKET_NUMBER_REGEX; exports.PathInfo = PathInfo; exports.arrayToPath = arrayToPath; exports.expandPath = expandPath; exports.expandPathVerbose = expandPathVerbose; exports.getPathInfo = getPathInfo; exports.getPathValue = getPathValue; exports.pathToArray = pathToArray; exports.removePath = removePath; exports.setPathValue = setPathValue; //# sourceMappingURL=index.cjs.map