UNPKG

word-math

Version:

Extended version to compatible with OMML of Word Processing Document library

85 lines 3.5 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Trie = exports.trieNode = void 0; var fileTypeHandler_1 = require("./fileTypeHandler"); var keys = Object.keys(fileTypeHandler_1.fileTypeHandler); var trieNode = /** @class */ (function () { function trieNode() { } return trieNode; }()); exports.trieNode = trieNode; var Trie = /** @class */ (function () { function Trie() { this.tree = new trieNode(); } Trie.prototype._traversePrint = function (keys, node, level) { keys = keys.filter(function (key) { return key !== "value"; }); for (var _i = 0, _a = keys.entries(); _i < _a.length; _i++) { var _b = _a[_i], index_1 = _b[0], key = _b[1]; //Prints values with indentation console.log("".concat("| ".repeat(level - 1), "|-> ").concat(level, ".").concat(index_1, " ").concat(node[key].value)); //Recursion step this._traversePrint(Object.keys(node[key]), node[key], level + 1); } return; }; Trie.prototype._traverseInsert = function (node, string, index, fileType) { if (index === string.length - 1) { return (node[string[index]] = { value: string, fileType: fileType }); } else { if (!node[string[index]]) { node[string[index]] = { value: string.slice(0, index + 1) }; } node[string[index]][string[index + 1]] = __assign(__assign({}, node[string[index]][string[index + 1]]), this._traverseInsert(node[string[index]], string, index + 1, fileType)); } return node[string[index]]; }; Trie.prototype._traverseSearch = function (string, node, index) { if (!node) { console.error("Not there lul!"); return "INVALID"; } if (index === string.length) { console.log("Got em'"); return node.value; } else { return this._traverseSearch(string, node[string[index]], index + 1); } }; Trie.prototype.insert = function (string, fileType) { this._traverseInsert(this.tree, string, 0, fileType); }; Trie.prototype.search = function (string) { console.log(this._traverseSearch(string, this.tree, 0)); }; Trie.prototype.print = function () { console.log("Tree"); this._traversePrint(Object.keys(this.tree), this.tree, 1); }; return Trie; }()); exports.Trie = Trie; var testTrie = new Trie(); // const testWordsOne: string[] = ["test", "teseract","tennis","testing","insertion","insert","inseparable","international"] // const testWordsTwo: string[] = ["to", "teal", "ted", "ten", "an", "ion", "io", "inn"] // testWordsOne.forEach(word => testTrie.insert(word)) keys.forEach(function (key) { testTrie.insert(fileTypeHandler_1.fileTypeHandler[key].signature.toString(), fileTypeHandler_1.fileTypeHandler[key].fileExtension); }); testTrie.print(); console.log(testTrie); //# sourceMappingURL=trie.js.map