zhconver
Version:
Convert between Simplified Chinese and Traditional Chinese
3 lines (2 loc) • 842 B
JavaScript
var conver=function(t){"use strict";const n=Array.from;class e{constructor(){this.map=new Map,this.k="",this.v="",this.end=!1}}const o=(t,n,e)=>t.replace(new RegExp(n,"g"),e);return t.Trie=class{constructor(){this.root=new e}set(t,n){return this.add(t,n)}add(t,o){const r=n(t);let s=this.root;for(let t=0;t<r.length;t++){const n=r[t];s.map.has(n)||s.map.set(n,new e),s=s.map.get(n)}return s.end=!0,s.k=t,s.v=o,this}find(t,n=0){let e=this.root;const o=t.length,r=[];for(let s=n;s<o;s++){const o=t[s],c=e.map.get(o);if(!c)return r.pop()||[n];e=c,e.end&&r.push([s,e.k,e.v])}return r.pop()||[n]}},t.conver=function(t,e){const r=n(t),s=r.length,c=[];for(let t=0;t<s;t++){const[n,o,s]=e.find(r,t);t=n,o&&c.push([o,s])}const h=c.sort((([t],[n])=>n.length-t.length));for(const[n,e]of h)t=o(t,n,e);return t},t}({});
//# sourceMappingURL=conver.js.map