UNPKG

@clarketm/supertrie

Version:
8 lines • 3.17 kB
/** * Copyright (c) 2018, Travis Clarke * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.Trie={})}(this,function(e){"use strict";var t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o={ROOT:"√"},i=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o.ROOT;n(this,e),this._char=t,this._isCompleteWord=!1,this._children=new Map}return r(e,[{key:"has",value:function(e){return this._children.has(e)}},{key:"get",value:function(e){return this._children.get(e)}},{key:"set",value:function(e,t){this._children.set(e,t)}},{key:"delete",value:function(e){this._children.delete(e)}},{key:"count",get:function(){return this._children.size}},{key:"char",get:function(){return this._char}},{key:"isCompleteWord",get:function(){return this._isCompleteWord}},{key:"isLeafNode",get:function(){return 0===this.count}}]),e}();e.Trie=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];n(this,e),this._root=new i;var r=!0,o=!1,u=void 0;try{for(var s,a=t[Symbol.iterator]();!(r=(s=a.next()).done);r=!0){this.insert(s.value)}}catch(e){o=!0,u=e}finally{try{!r&&a.return&&a.return()}finally{if(o)throw u}}}return r(e,[{key:"insert",value:function(e){if("string"!==(void 0===e?"undefined":t(e)))throw Error("Unable to insert non-string value: "+e);var n=this.root,r=!0,o=!1,u=void 0;try{for(var s,a=e[Symbol.iterator]();!(r=(s=a.next()).done);r=!0){var l=s.value;if(n.has(l))n=n.get(l);else{var f=new i(l);n.set(l,f),n=f}}}catch(e){o=!0,u=e}finally{try{ !r&&a.return&&a.return()}finally{if(o)throw u}}n._isCompleteWord=!0}},{key:"remove",value:function(e){function n(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(!t)return!1;if(r===e.length)return t._isCompleteWord=!1,t.isLeafNode;var o=e[r];return n(t.get(o),r+1)?(t.delete(o),t.isLeafNode):void 0}if("string"!==(void 0===e?"undefined":t(e)))throw Error("Unable to remove non-string value: "+e);return n(this.root)}},{key:"search",value:function(e){for(var t=this.root,n=0;n<e.length&&(t=t.get(e[n]));)n++;return{query:e,matchedChars:n,isMatch:e.length===n,isCompleteWord:e.length===n&&t.isCompleteWord,node:t}}},{key:"includes",value:function(e){return this.search(e).isCompleteWord}},{key:"contains",value:function(e){return this.includes(e)}},{key:"startsWith",value:function(e){return this.search(e).isMatch}},{key:"root",get:function(){return this._root}}]),e}(),Object.defineProperty(e,"__esModule",{value:!0})});