UNPKG

@designliquido/delegua

Version:

Linguagem de programação simples e moderna usando português estruturado.

181 lines 7.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const informacao_variavel_ou_constante_1 = require("../informacao-variavel-ou-constante"); exports.default = { adicionar: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('elemento', 'qualquer') ], implementacao: (interpretador, vetor, elemento) => { vetor.push(elemento); return Promise.resolve(vetor); }, }, concatenar: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('outroVetor', 'qualquer[]') ], implementacao: (interpretador, vetor, outroVetor) => { return Promise.resolve(vetor.concat(outroVetor)); }, }, empilhar: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('elemento', 'qualquer') ], implementacao: (interpretador, vetor, elemento) => { vetor.push(elemento); return Promise.resolve(vetor); }, }, encaixar: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('inicio', 'qualquer'), new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('excluirQuantidade', 'número'), new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('itens', 'qualquer[]') ], implementacao: (interpretador, vetor, inicio, excluirQuantidade, ...itens) => { let elementos = []; if (excluirQuantidade || excluirQuantidade === 0) { elementos = !itens.length ? vetor.splice(inicio, excluirQuantidade) : vetor.splice(inicio, excluirQuantidade, ...itens); } else { elementos = !itens.length ? vetor.splice(inicio) : vetor.splice(inicio, ...itens); } return Promise.resolve(elementos); }, }, fatiar: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('inicio', 'número'), new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('fim', 'número') ], implementacao: (interpretador, vetor, inicio, fim) => Promise.resolve(vetor.slice(inicio, fim)), }, filtrarPor: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('funcao', 'função') ], implementacao: async (interpretador, vetor, funcao) => { if (funcao === undefined || funcao === null) { return Promise.reject("É necessário passar uma função para o método 'filtrarPor'"); } const retorno = []; for (let elemento of vetor) { if (await funcao.chamar(interpretador, [elemento])) { retorno.push(elemento); } } return retorno; }, }, inclui: { tipoRetorno: 'lógico', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('elemento', 'qualquer') ], implementacao: (interpretador, vetor, elemento) => Promise.resolve(vetor.includes(elemento)), }, inverter: { tipoRetorno: 'qualquer[]', argumentos: [], implementacao: (interpretador, vetor) => Promise.resolve(vetor.reverse()), }, juntar: { tipoRetorno: 'texto', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('separador', 'texto') ], implementacao: (interpretador, vetor, separador) => Promise.resolve(vetor.join(separador)), }, mapear: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('funcao', 'função') ], implementacao: async (interpretador, vetor, funcao) => { if (funcao === undefined || funcao === null) { return Promise.reject("É necessário passar uma função para o método 'mapear'"); } const retorno = []; for (let elemento of vetor) { let resultado = await funcao.chamar(interpretador, [elemento]); retorno.push(resultado); } return retorno; }, }, ordenar: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('funcaoOrdenacao', 'função') ], implementacao: async (interpretador, vetor, funcaoOrdenacao) => { if (funcaoOrdenacao !== undefined && funcaoOrdenacao !== null) { for (let i = 0; i < vetor.length - 1; i++) { for (let j = 1; j < vetor.length; j++) { if ((await funcaoOrdenacao.chamar(interpretador, [vetor[j - 1], vetor[j]])) > 0) { const aux = vetor[j]; vetor[j] = vetor[j - 1]; vetor[j - 1] = aux; } } } return vetor; } if (!vetor.every((v) => typeof v === 'number')) { return vetor.sort(); } return vetor.sort((a, b) => a - b); }, }, remover: { tipoRetorno: 'qualquer[]', argumentos: [ new informacao_variavel_ou_constante_1.InformacaoVariavelOuConstante('elemento', 'qualquer') ], implementacao: (interpretador, vetor, elemento) => { const index = vetor.indexOf(elemento); if (index !== -1) vetor.splice(index, 1); return Promise.resolve(vetor); }, }, removerPrimeiro: { tipoRetorno: 'qualquer', argumentos: [], implementacao: (interpretador, vetor) => { let elemento = vetor.shift(); return Promise.resolve(elemento); }, }, removerUltimo: { tipoRetorno: 'qualquer', argumentos: [], implementacao: (interpretador, vetor) => { let elemento = vetor.pop(); return Promise.resolve(elemento); }, }, somar: { tipoRetorno: 'qualquer', argumentos: [], implementacao: (interpretador, vetor) => { return Promise.resolve(vetor.reduce((acc, item) => acc + (typeof item === 'number' ? item : item.valor), 0)); }, }, tamanho: { tipoRetorno: 'número', argumentos: [], implementacao: (interpretador, vetor) => Promise.resolve(vetor.length), }, }; //# sourceMappingURL=primitivas-vetor.js.map