openapi-directory
Version:
Building & bundling https://github.com/APIs-guru/openapi-directory for easy use from JS
129 lines • 5.43 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Trie = exports.isLeafValue = void 0;
function isLeafValue(value) {
return typeof value === 'string' || Array.isArray(value);
}
exports.isLeafValue = isLeafValue;
var Trie = /** @class */ (function () {
function Trie(root) {
this.root = root;
}
Trie.prototype._getLongestMatchingPrefix = function (key) {
var e_1, _a;
var remainingKey = key.toLowerCase();
var node = this.root;
while (node) {
// Calculate the max key length. String keys should be all the same length,
// except one optional '' key if there's an on-path leaf node here, so we
// just use the first non-zero non-regex length.
var maxKeyLength = void 0;
try {
for (var _b = (e_1 = void 0, __values(node.keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
var k = _c.value;
if (k && !(k instanceof RegExp)) {
maxKeyLength = k.length;
break;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
// Given a common key length L, we try to see if the first L characters
// of our remaining key are an existing key here
var keyToMatch = remainingKey.slice(0, maxKeyLength);
// We check for the key with a hash lookup (as we know it would have to be an exact match),
// _not_ by looping through keys with startsWith - this is key (ha!) to perf here.
var nextNode = node.get(keyToMatch);
if (nextNode) {
// If that bit of the key matched, we can remove it from the key to match,
// and move on to match the next bit.
remainingKey = remainingKey.slice(maxKeyLength);
}
else {
// If it didn't match, we need to check regexes, if present, and check
// for an on-path leaf node here ('' key)
var matchedRegex = Array.from(node.keys()).map(function (k) {
var match = k instanceof RegExp && k.exec(remainingKey);
if (!!match && match.index === 0) {
return {
matchedNode: node.get(k),
matchLength: match[0].length
};
}
;
}).filter(function (r) { return !!r; })[0];
if (matchedRegex) {
// If we match a regex, we no longer need to match the part of the
// key that the regex has consumed
remainingKey = remainingKey.slice(matchedRegex.matchLength);
nextNode = matchedRegex.matchedNode;
}
else {
nextNode = node.get('');
}
}
if (isLeafValue(nextNode)) {
// We've reached the end of a key - if we're out of
// input, that's good, if not it's just a prefix.
return {
remainingKey: remainingKey,
matchedKey: key.slice(0, -1 * remainingKey.length),
value: nextNode
};
}
else {
node = nextNode;
}
}
// We failed to match - this means at some point we either had no key left, and
// no on-path key present, or we had a key left that disagreed with every option.
return undefined;
};
/*
* Given a key, finds an exact match and returns the value(s).
* Returns undefined if no match can be found.
*/
Trie.prototype.get = function (key) {
var searchResult = this._getLongestMatchingPrefix(key);
if (!searchResult)
return undefined;
var remainingKey = searchResult.remainingKey, value = searchResult.value;
return remainingKey.length === 0 ?
value : undefined;
};
/*
* Given a key, finds the longest key that is a prefix of this
* key, and returns its value(s). I.e. for input 'abcdef', 'abc'
* would match in preference to 'ab', and 'abcdefg' would never
* be matched.
*
* Returns undefined if no match can be found.
*/
Trie.prototype.getMatchingPrefix = function (key) {
var searchResult = this._getLongestMatchingPrefix(key);
if (!searchResult)
return undefined;
var value = searchResult.value;
return value;
};
return Trie;
}());
exports.Trie = Trie;
//# sourceMappingURL=trie.js.map
;