UNPKG

@configurator/ravendb

Version:
70 lines 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RuleBasedReplacerFactory = exports.SkippingReplacerFactory = exports.ReplacerContext = void 0; class ReplacerContext { constructor() { this.path = ""; this.key = null; this.value = null; this.parent = null; this._parentsStack = []; this._pathSegments = []; } update(parent, key, value) { if (!key) { return; } this.key = key; this.value = value; this.parent = parent; const parentIdx = this._parentsStack.indexOf(parent); if (this._parentsStack.length && parentIdx === this._parentsStack.length - 1) { this._pathSegments[this._pathSegments.length - 1] = key; } else if (parentIdx === -1) { this._parentsStack.push(parent); this._pathSegments.push(key); } else { const deleteCount = this._parentsStack.length - parentIdx; this._parentsStack.splice(parentIdx, deleteCount, parent); this._pathSegments.splice(parentIdx, deleteCount, key); } } get currentPath() { return this._pathSegments.join("."); } } exports.ReplacerContext = ReplacerContext; class SkippingReplacerFactory { static build(toSkip, replacer) { const toSkipSet = new Set(toSkip); return RuleBasedReplacerFactory.build([ { contextMatcher: (context) => !toSkipSet.has(context.value), replacer } ]); } } exports.SkippingReplacerFactory = SkippingReplacerFactory; class RuleBasedReplacerFactory { static build(rules, fieldCallback) { const context = new ReplacerContext(); return function (key, value) { context.update(this, key, value); if (fieldCallback) { fieldCallback(context); } for (const entry of rules) { if (entry.contextMatcher(context)) { return entry.replacer.call(this, key, value); } } return value; }; } } exports.RuleBasedReplacerFactory = RuleBasedReplacerFactory; //# sourceMappingURL=ReplacerFactory.js.map