UNPKG

silegismg-interpretador-articulacao

Version:
47 lines (46 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Contexto { constructor() { this.ultimoItem = null; this.textoAnterior = ''; this.articulacao = []; } getUltimoItemTipo(tipo) { let item = this.ultimoItem; if (item === null) { return null; } if (typeof tipo === 'function') { while (item && !(item instanceof tipo)) { item = item.$parent; } } else if (tipo instanceof Array) { do { let j; for (j = 0; j < tipo.length; j++) { if (item instanceof tipo[j]) { return item; } } item = item.$parent; } while (item); } else { throw new Error('Argumento inválido: ' + tipo); } return item; } adicionar(tipoUltimoItem, dispositivo) { const ultimoItem = this.getUltimoItemTipo(tipoUltimoItem); if (ultimoItem) { ultimoItem.adicionar(dispositivo); } else { this.articulacao.push(dispositivo); } this.ultimoItem = dispositivo; } } exports.default = Contexto;