chai-latte
Version:
Build expressive & readable fluent interface libraries.
64 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WordNode = void 0;
class WordNode {
isRoot = false;
word;
accessor;
parsedSentences = [];
childrenChunks = new Map();
childrenNodes = new Map();
get isCallable() {
return Array.from(this.childrenChunks.values())
.some(chunk => chunk && !!chunk.isCallable);
}
get isLeaf() {
return Array.from(this.childrenNodes.values())
.every(set => set.size === 0);
}
constructor(props) {
this.word = props.word;
this.accessor = props.accessor;
this.isRoot = !!props.isRoot;
}
addparsedSentence(parsed) {
this.parsedSentences.push(parsed);
}
addChunk(chunk) {
if (!this.childrenChunks.has(chunk.template)) {
this.childrenChunks.set(chunk.template, chunk);
this.childrenNodes.set(chunk.template, new Set());
}
}
addNextWord(nextWord, previousChunk) {
if (previousChunk) {
const chunk = this.childrenChunks.get(previousChunk.template);
const nextWords = this.childrenNodes.get(chunk.template);
nextWords.add(nextWord);
}
else if (this.isRoot) {
if (!this.childrenNodes.has(null)) {
this.childrenNodes.set(null, new Set());
}
this.childrenNodes.get(null).add(nextWord);
}
}
traverse(callback) {
for (const [template, children] of this.childrenNodes.entries()) {
for (const word of children.values()) {
const chunk = this.childrenChunks.get(template);
callback(word, chunk);
word.traverse(callback);
}
}
}
static createRootNode(accessor) {
const word = new WordNode({
accessor: accessor,
isRoot: true,
});
return word;
}
}
exports.WordNode = WordNode;
//# sourceMappingURL=WordNode.js.map