UNPKG

autosuggestion

Version:

  Generates suggestions for text completion.  

96 lines 4.01 kB
var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; import { isWord } from './typegaurds'; var Suggestion = /** @class */ (function () { function Suggestion(tokens) { this.tokens = tokens; this.rank = 0; this._simplified = []; this._simplify(); } Object.defineProperty(Suggestion.prototype, "simplified", { get: function () { return this._simplified; }, enumerable: false, configurable: true }); Object.defineProperty(Suggestion.prototype, "length", { get: function () { return this.tokens.length; }, enumerable: false, configurable: true }); Suggestion.prototype.concat = function (suggestion) { this.tokens = this.tokens.concat(suggestion.tokens); this._simplify(); return this; }; Suggestion.prototype.splice = function (offset, tokens) { if (offset === -1) offset = this.tokens.length; this.tokens = __spreadArrays(this.tokens.slice(0, offset), tokens, this.tokens.slice(offset)); this._simplify(); return this; }; /** * Given a sequence of offset tokens and a range, `n`, to resolve, this method * will resolve the next `n` contiguous lookups which occur after the provided * offset tokens. If the last token in the offset tokens is a substring of the * token at that offset in this suggestion, then no further resolution will occur. * This is mean to only resolve sequences of contiguous lookups occuring directly * after the final offset token if the final offset token completes a word suggestion. */ Suggestion.prototype.resolveLookups = function (offsetTokens, n) { var offset = offsetTokens.length; if (n === 0) return [this]; if (this.tokens.length < offset) return [this]; // does the final offset token equal the word at that index in the suggestion? // if not, do not proceed. if (offsetTokens[offset - 1] !== this.tokens[offset - 1]) return [this]; // if the next token is a word (and not a lookup), do not proceed. if (isWord(this.tokens[offset])) return [this]; // okay, the next token is a lookup var suggestions = []; // iterate over each context of a lookup token. for (var _i = 0, _a = Object.values(this.tokens[offset])[0]; _i < _a.length; _i++) { var context = _a[_i]; for (var _b = 0, _c = context.completePattern([]); _b < _c.length; _b++) { var suggestion = _c[_b]; // splice the new suggestion with the pre-existing suggestion // (effectively expanding the next lookup) suggestion .splice(0, offsetTokens) .splice(-1, this.tokens.slice(offset + 1)); // is the next token a word? if so, resolve if (isWord(suggestion.tokens[offset])) { suggestions = suggestions.concat(suggestion.resolveLookups(suggestion.tokens.slice(0, offset + 1), n - 1)); } else { suggestions = suggestions.concat(suggestion.resolveLookups(offsetTokens, n)); } } } return suggestions; }; Suggestion.prototype._simplify = function () { this._simplified = this.tokens.map(function (t) { return isWord(t) ? t : "[" + Object.keys(t)[0] + "]"; // TODO make delimeters configurable }); }; return Suggestion; }()); export { Suggestion }; //# sourceMappingURL=suggestion.js.map