UNPKG

ivr-tester

Version:

An automated testing framework for IVR call flows

53 lines (52 loc) 2.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UlawDtmfBufferGenerator = void 0; const path_1 = __importDefault(require("path")); const fs_1 = require("fs"); const dtmfSequenceUtils_1 = require("./dtmfSequenceUtils"); class UlawDtmfBufferGenerator { constructor(rawFilesBasePath = UlawDtmfBufferGenerator.DEFAULT_RAW_BASE_PATH) { this.paths = new Map(); this.rawCache = new Map(); this.initiatePathsToRawFiles(rawFilesBasePath); } initiatePathsToRawFiles(basePath) { this.paths.set("0", path_1.default.join(basePath, "0.raw")); this.paths.set("1", path_1.default.join(basePath, "1.raw")); this.paths.set("2", path_1.default.join(basePath, "2.raw")); this.paths.set("3", path_1.default.join(basePath, "3.raw")); this.paths.set("4", path_1.default.join(basePath, "4.raw")); this.paths.set("5", path_1.default.join(basePath, "5.raw")); this.paths.set("6", path_1.default.join(basePath, "6.raw")); this.paths.set("7", path_1.default.join(basePath, "7.raw")); this.paths.set("8", path_1.default.join(basePath, "8.raw")); this.paths.set("9", path_1.default.join(basePath, "9.raw")); this.paths.set("*", path_1.default.join(basePath, "asterisk.raw")); this.paths.set("#", path_1.default.join(basePath, "hash.raw")); this.paths.set("w", path_1.default.join(basePath, "w.raw")); } generate(digits) { if (typeof digits !== "string" || digits.length === 0) { throw new Error("At least one digit must be provided"); } const validationResults = dtmfSequenceUtils_1.dtmfSequenceValidator(digits); if (validationResults.valid === false) { throw new Error(validationResults.reason); } const separateDigits = dtmfSequenceUtils_1.convertToDtmfArray(digits); return Buffer.concat(separateDigits.map((d) => this.getRawBuffer(d))); } getRawBuffer(digit) { if (this.rawCache.has(digit)) { return this.rawCache.get(digit); } const file = fs_1.readFileSync(this.paths.get(digit)); this.rawCache.set(digit, file); return file; } } exports.UlawDtmfBufferGenerator = UlawDtmfBufferGenerator; UlawDtmfBufferGenerator.DEFAULT_RAW_BASE_PATH = path_1.default.join(__dirname, "./raw/");