UNPKG

compromise

Version:
72 lines (67 loc) 1.68 kB
'use strict'; const miscMethods = (Terms) => { const methods = { term: function (n) { return this.terms[n]; }, first: function () { let t = this.terms[0]; return new Terms([t], this.lexicon, this.parent); }, last: function () { let t = this.terms[this.terms.length - 1]; return new Terms([t], this.lexicon, this.parent); }, endPunctuation: function () { return this.last().terms[0].endPunctuation(); }, canBe: function (tag) { tag = tag || ''; tag = tag.replace(/^#/, ''); //atleast one of these for (let i = 0; i < this.terms.length; i++) { if (!this.terms[i].term.canBe(tag)) { return false; } } return true; }, index: function() { let parent = this.parentTerms; let first = this.terms[0]; if (!parent || !first) { return null; //maybe.. } for(let i = 0; i < parent.terms.length; i++) { if (first === parent.terms[i]) { return i; } } return null; }, termIndex: function() { let first = this.terms[0]; let ref = this.parent; if (!ref || !first) { return null; //maybe.. } let n = 0; for(let i = 0; i < ref.list.length; i++) { let ts = ref.list[i]; for(let o = 0; o < ts.terms.length; o++) { if (ts.terms[o] === first) { return n; } n += 1; } } return n; } }; //hook them into result.proto Object.keys(methods).forEach((k) => { Terms.prototype[k] = methods[k]; }); return Terms; }; module.exports = miscMethods;