"use strict";
function None() {
this.isSome = false;
this.bindIt = function (func) { return this; };
}
function Some(pData) {
this.data = pData;
this.isSome = true;
this.bindIt = function (func) { return func(this.data); };
}
const none = new None();
function FirstRemainder(first, remainder) {
this.First = first;
this.Remainder = remainder;
}
function getFirstChars(str) {
let len = str.length;
if (len > 1) {
return new Some(new FirstRemainder(str.charAt(0), str.slice(1)));
} else if (len === 1) {
return new Some(new FirstRemainder(str.charAt(0), ''));
} else {
return none;
}
}
function add(trie, str) {
let chars = getFirstChars(str);
if (chars.isSome) {
if ((chars.bindIt(function (d) { return d.First; })) in trie) {
add(trie[chars.bindIt(function (d) { return d.First })], chars.bindIt(function (d) { return d.Remainder }));
} else {
let newTrie = {};
trie[chars.bindIt(function (d) { return d.First })] = newTrie;
add(newTrie, chars.bindIt(function (d) { return d.Remainder }));
}
} else {
return trie;//don't need this
}
}
function find(trie, str) {
let chars = getFirstChars(str);
if (chars.isSome) {
if ((chars.bindIt(function (d) { return d.First; })) in trie) {
return find(trie[chars.bindIt(function (d) { return d.First })], chars.bindIt(function (d) { return d.Remainder }));
} else {
return none;
}
} else {
return new Some(trie);
}
}
function display(trie) {
for (let c in trie) {
console.log(c);
display(trie[c]);
}
}
function strings(trie) {
let arr = [];
function stringsAux(trie, strs) {
for (let c in trie) {
stringsAux(trie[c], strs + c);
}
if (Object.keys(trie).length === 0) {
arr.push(strs);
strs = "";
}
}
stringsAux(trie, "");
return arr;
}
let trie = {};
add(trie, "G4143\n");
add(trie, "This is the first\n");
add(trie, "This is the second\n");
add(trie, "This is the third\n");
add(trie, "This is the fourth\n");
add(trie, "This is the fourth\n");
add(trie, "Here it ends...\n");
//try duplicates
add(trie, "G4143\n");
add(trie, "This is the first\n");
add(trie, "This is the second\n");
add(trie, "This is the third\n");
add(trie, "This is the fourth\n");
add(trie, "This is the fourth\n");
add(trie, "Here it ends...\n");
let ans = find(trie, "This is the ");
if (ans.isSome) {
ans.bindIt(function (d) { display(d) });
} else {
console.log("Nothing found!");
}
console.log("Lines...");
let strs = strings(trie);
for (let line in strs) {
console.log(`Line: ${strs[line]}`);
}
//display data structure
console.log(JSON.stringify(trie));