import { CallVector } from './call-vector.model'; export interface VectorStep { order: number; type: VectorStepType; content: string; linkedVector?: CallVector; } export const vectorStepTypes = [ 'wait-time', 'route-to', 'goto-step', 'goto-vector', 'stop', 'messaging', 'queue-to', 'collect', 'comment', 'disconnect', ] as const; export type VectorStepType = typeof vectorStepTypes[number]; /** * A function used to type-check a string to determine if there is a corresponding VectorStepType value. * @param String value to type-checked. * @returns a truthy value, based on if the parameter was found. */ export function isVectorStepType(value: string): value is VectorStepType { return vectorStepTypes.indexOf(value as VectorStepType) !== -1; } export function parseVectorStepType(content: string): VectorStepType { // Take first word of content. let type = (content.match(/=(\w+-?\w+|#)/) ?? [''])[1]; // Determine the type of goto statement. if (type === 'goto') { type += '-' + content.split(' ')[1]; } return isVectorStepType(type) ? type : 'comment'; } /** RegEx heavy function to increase content readability. */ export function parseVectorStepContent(step: string) { // Match everything after '=' sign. let content = (step.match(/=(.*)/) ?? [''])[1]; if (/goto vector|goto step/.test(content)) { // Add 'step' before second number in content (the step of the Vector we are 'goto'ing). content = content.replace(/(goto vector [\d]+ )([\d]+)(.*)/, '$1step $2$3'); if (/time-of-day/.test(content)) { content = content.replace('time-of-day', 'if from'); content = content.replace(/(from )?all/g, 'anyday'); // Uppercase days of the week. content = content.replace( /(mon|tue|wed|thu|fri|sat|sun)/g, (x) => x[0].toUpperCase() + x.slice(1), ); // Match two two-number sequences, separated by a space (time) - e.g., 12 30. content.match(/\d{2}\W\d{2}/g).forEach((time, index) => { const newTime = time.replace(' ', ':'); content = content.replace(time, index === 0 ? newTime + ' to' : newTime); }); } } return content; } /** Split a string into an array of Vector Steps * @param String C1 string to be parsed */ export function parseVectorSteps(stepsStr: string): VectorStep[] { if (!stepsStr) return []; const vectorSteps: VectorStep[] = []; const steps = stepsStr.split(/\r?\n/); steps.forEach((step) => { // Match on first number, at the beginning of the string. const order = +(step.match(/^\d+/) ?? [''])[0]; const content = parseVectorStepContent(step); const type = parseVectorStepType(step); if (order < 1) return; vectorSteps.push({ order, type, content, }); }); return vectorSteps; }