import { Value } from './Value'; import { Character } from './Character'; import { ESCAPE, PIPE, ESCPIPE } from './Predefined'; /** * Convert a string into Characters and preserve escaped pipe symbols * * @param {string} input * @return {*} {(Array { const escaped = Array.from(input, (c) => Character.from(c)) as Array; let escape: number = 0; while ((escape = escaped.indexOf(ESCAPE, escape)) >= 0) { (escaped[escape + 1] === PIPE) && escaped.splice(escape, 2, ESCPIPE); ++escape; } return escaped; } /** * split a list of Value instances on every token * * @export * @param {Array} list * @param {...Array} tokens * @returns {Array>} */ export function split(list: Array, ...tokens: Array): Array> { return list.reduce((carry: Array>, value: Value) => { const match = tokens.includes(value); const pos = carry.length - Number(!(match || !carry.length)); carry[pos] = (carry[pos] || []).concat(match ? [] : value); return carry; }, []); } /** * trim leading and trailing tokens from a list of Value instances * * @export * @param {Array} list * @param {...Array} tokens * @returns {Array} */ export function trim(list: Array, ...tokens: Array): Array { let before: number = 0; let after: number = list.length; while (tokens.includes(list[before]) && ++before); while (tokens.includes(list[after - 1]) && --after); return list.slice(before, after); } /** * interleave TemplateLiteral arguments into a single list of Value instances * * @export * @param {TemplateStringsArray} strings * @param {...Array} values * @returns {Array} */ export function interleave(strings: TemplateStringsArray, ...values: Array): Array { return strings.reduce( (carry, value, index) => carry.concat( characters(value), index < values.length ? Value.from(values[index]) : [] ), [] as Array ); }