UNPKG

foxts

Version:

Opinionated collection of common TypeScript utils by @SukkaW

1 lines 638 B
"use strict";class t extends Map{wordEnd;fail;constructor(t,e){super(),this.wordEnd=t,this.fail=e}}exports.createAhoCorasick=function(e){const o=new t(!1,void 0);e.forEach(e=>{let r=o;for(let o=0,n=e.length;o<n;o++){const n=e[o];if(r.has(n))r=r.get(n);else{const e=new t(!1,void 0);r.set(n,e),r=e}}r!==o&&(r.wordEnd=!0)});const r=[o];let n=0;for(;n<r.length;){const t=r[n++];t.forEach((e,n)=>{let s=t.fail;for(;s&&!s.has(n);)s=s.fail;e.fail=s?s.get(n):o,e.fail?.wordEnd&&(e.wordEnd=!0),r.push(e)})}return t=>{let e=o;for(let r=0,n=t.length;r<n;r++){const n=t[r];for(;e&&!e.has(n);)e=e.fail;if((e=e?e.get(n):o).wordEnd)return!0}return!1}};