UNPKG

@finnair/path

Version:
128 lines (127 loc) 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnyProperty = exports.AnyIndex = exports.UnionMatcher = exports.PropertyMatcher = exports.IndexMatcher = void 0; exports.isPathExpression = isPathExpression; const Path_js_1 = require("./Path.js"); function isPathExpression(component) { return !!component && typeof component.test === 'function' && typeof component.find === 'function'; } class IndexMatcher { index; allowGaps = true; constructor(index) { this.index = index; Path_js_1.Path.validateIndex(index); } find(current, callback) { if (Array.isArray(current) && this.index < current.length) { return callback(current[this.index], this.index); } return true; } test(component) { return component === this.index; } toString() { return Path_js_1.Path.indexToString(this.index); } } exports.IndexMatcher = IndexMatcher; class PropertyMatcher { property; allowGaps = false; constructor(property) { this.property = property; Path_js_1.Path.validateProperty(property); } find(current, callback) { if (typeof current === 'object' && current.hasOwnProperty(this.property)) { return callback(current[this.property], this.property); } return true; } test(component) { return String(component) === this.property; } toString() { return Path_js_1.Path.propertyToString(this.property); } } exports.PropertyMatcher = PropertyMatcher; class UnionMatcher { components; constructor(components) { this.components = components; if (components.length < 2) { throw new Error('Expected at least 2 properties'); } components.forEach(Path_js_1.Path.validateComponent); } find(current, callback) { if (typeof current === 'object') { for (const component of this.components) { if (current.hasOwnProperty(component)) { if (!callback(current[component], component)) { return false; } } } } return true; } test(component) { const str = String(component); return this.components.find(component => String(component) === str) !== undefined; } get allowGaps() { return this.components.some(component => typeof component === 'number'); } toString() { return `[${this.components.map(this.propertyToString).join(',')}]`; } static of(...components) { return new UnionMatcher(components); } propertyToString(property) { return JSON.stringify(property); } } exports.UnionMatcher = UnionMatcher; exports.AnyIndex = { allowGaps: false, find: (current, callback) => { if (Array.isArray(current)) { for (let i = 0; i < current.length; i++) { if (!callback(current[i], i)) { return false; } } } return true; }, test: (component) => { return typeof component === 'number' && Number.isInteger(component) && component >= 0; }, toString: () => { return '[*]'; }, }; exports.AnyProperty = { allowGaps: false, find: (current, callback) => { if (typeof current === 'object') { for (let key in current) { if (!callback(current[key], key)) { return false; } } } return true; }, test: () => { return true; }, toString: () => { return '.*'; }, };