UNPKG

ivr-tester

Version:

An automated testing framework for IVR call flows

56 lines (55 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dtmfSequenceValidator = exports.convertToDtmfArray = void 0; const validDtmfDigits = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "#", "w", ]; function convertToDtmfArray(dtmfSequence) { if (typeof dtmfSequence === "string") { return dtmfSequence.split("").map((c) => c.toLocaleLowerCase()); } if (Array.isArray(dtmfSequence)) { const sequence = []; dtmfSequence .filter((d) => typeof d === "string") .forEach((e) => sequence.push(...e.split(""))); return sequence.map((c) => c.toLocaleLowerCase()); } return []; } exports.convertToDtmfArray = convertToDtmfArray; const isArrayOfString = (x) => Array.isArray(x) && x.every((e) => typeof e === "string"); function dtmfSequenceValidator(possibleDtmfSequence) { if (typeof possibleDtmfSequence !== "string" && !isArrayOfString(possibleDtmfSequence)) { return { valid: false, reason: `DTMF sequence '${possibleDtmfSequence}' must be a string or array of strings`, }; } const dtmfSequence = convertToDtmfArray(possibleDtmfSequence); if (dtmfSequence.length === 0) { return { valid: false, reason: "At least one digit must be provided" }; } const invalidDigits = dtmfSequence.filter((c) => !validDtmfDigits.includes(c.toLocaleLowerCase())); if (invalidDigits.length > 0) { return { valid: false, reason: `DTMF sequence '${possibleDtmfSequence}' contains invalid digits '${invalidDigits}'. The valid digits are '${validDtmfDigits}'`, }; } return { valid: true }; } exports.dtmfSequenceValidator = dtmfSequenceValidator;