UNPKG

chai-latte

Version:

Build expressive & readable fluent interface libraries.

64 lines 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = void 0; const template_1 = require("./template"); const parse = ([...chunks], ...variables) => { const hasVariables = variables.length > 0; if (!hasVariables) { throw new Error('Parsing Error: a sentence should have at least one variable'); } const reservedKeywords = new Set([ 'if', 'while', ]); const validateAndShouldSkip = (word, chunkIndex) => { const hasWord = word.length > 0; if (!hasWord) { const isEmptyString = chunks.length === 1; if (isEmptyString) { throw new Error('Parsing Error: a sentence cannot be empty'); } const isStartingWithVariable = chunkIndex === 0; if (isStartingWithVariable) { throw new Error('Parsing Error: sentences cannot start with variable'); } const hasSuccessiveVariables = chunkIndex !== chunks.length - 1; if (hasSuccessiveVariables) { throw new Error('Parsing Error: two variables should be separated by a word'); } return true; } const isStartingWithReservedKeyword = reservedKeywords.has(word); if (isStartingWithReservedKeyword) { throw new Error('Parsing Error: sentence cannot start with a reserved keyword'); } return false; }; const words = chunks.reduce((acc, chunk, chunkIndex) => { chunk.trim().split(' ').forEach((word, index, arr) => { const shouldSkip = validateAndShouldSkip(word, chunkIndex); if (shouldSkip) { return; } const isCallable = index === arr.length - 1; const variable = isCallable ? variables[chunkIndex] : undefined; const templateVariable = variable ? ` \${${template_1.TemplateBuilder.createTemplateType(variable)}}` : ''; const parsedChunk = { name: word, isCallable: isCallable, arg: variable, template: `${word}` + templateVariable, }; acc.push(parsedChunk); }); return acc; }, []); const parsed = { words, template: words.map(word => word.template).join(' '), }; return parsed; }; exports.parse = parse; //# sourceMappingURL=parse.js.map