UNPKG

@tanishiking/aho-corasick

Version:

TypeScript implementation of the Aho-Corasick algorithm for efficient string matching

98 lines (97 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.State = void 0; // TODO Impelement Ascii State that has fixed length HashMap // https://github.com/hankcs/aho-corasick/blob/master/src/main/java/org/ahocorasick/trie/AsciiState.java var State = /** @class */ (function () { function State(depth) { this._failure = null; this.rootState = null; this._emits = []; this.success = new Map(); this._depth = depth; this.rootState = depth === 0 ? this : null; } Object.defineProperty(State.prototype, "failure", { get: function () { return this._failure; }, set: function (state) { this._failure = state; }, enumerable: false, configurable: true }); Object.defineProperty(State.prototype, "depth", { get: function () { return this._depth; }, enumerable: false, configurable: true }); Object.defineProperty(State.prototype, "emits", { get: function () { return this._emits; }, enumerable: false, configurable: true }); /** * Add outputs to the state. * * @param keywords the list of outputs. */ State.prototype.addEmits = function (keywords) { var _a; (_a = this._emits).push.apply(_a, keywords); }; /** * Transit to the next state with the given character. * * @param char input character for the transition. */ State.prototype.nextState = function (char, ignoreRootState) { if (ignoreRootState === void 0) { ignoreRootState = false; } var nextState = this.success.get(char); if (!ignoreRootState && typeof nextState === 'undefined' && this.rootState !== null) { return this.rootState; } else if (nextState) { return nextState; } else { return null; } }; /** * Create a new state and add a transition to the new state * with the given character. * * @param char input character for moving to the new state. */ State.prototype.addState = function (char) { var nextState = this.nextState(char, true); // ignore root state if (nextState === null) { var newState = new State(this.depth + 1); this.success.set(char, newState); return newState; } else { return nextState; } }; /** * Return the list of reachable states with one step. */ State.prototype.getStates = function () { return Array.from(this.success.values()); }; /** * Get the possible char to move to the next state. */ State.prototype.getTransitions = function () { return Array.from(this.success.keys()); }; return State; }()); exports.State = State;