UNPKG

docxml

Version:

TypeScript (component) library for building and parsing a DOCX file

58 lines (57 loc) 2.65 kB
"use strict"; /** * A function that can be used to test that all the parameters of an object pass * a particular test. This can be used to check that no values in an object are NaN, * or could be used to check that values are inside a particular range. * * @param objectToCheck An object whose parameters we want to validate. * * @param callback A callback function that we use to check the values of our object. * The callback will be used recursively if objectToCheck has nested values. * * @param callbackFailureValue The boolean value that will indicate a failure of the callback function. * * @returns Returns `true` if all the object's values pass the check between the callback function and * the value that determines a failure. Otherwise, throws an error. * * @todo Add a better means of validating that the values used to generate ooxml are, in fact, * constrained in the ways we think they should be. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidNumber = exports.checkForForbiddenParameters = void 0; function checkForForbiddenParameters(objectToCheck, callback, callbackFailureValue) { const values = []; // Recurse through an object and flatten it to key-value pairs. const flattenedObject = (deepObject, accumulator) => { if (typeof deepObject === 'object') { for (const key in deepObject) { if (typeof deepObject[key] === 'object') { flattenedObject(deepObject[key], accumulator); } else { accumulator.push({ prop: key, value: deepObject[key] }); } } } return accumulator; }; // Iterate over an object's values until we hit one that causes the callback // function to equal the failure value. const flattenedObjectArray = flattenedObject(objectToCheck, values); flattenedObjectArray.forEach(entry => { const { prop, value } = entry; if (callback(value) === callbackFailureValue) { throw new Error(`Error when checking parameters.\nCallback for { ${prop}: ${value} } returned ${callback(value)}.`); } }); return true; } exports.checkForForbiddenParameters = checkForForbiddenParameters; /** * A function to check if any arbitrary property of an object is `NaN`. Intended to be used as a * possible callback for the `checkForForbiddenParameters` function in case we want to detect `NaN`. */ function isValidNumber(objectProperty) { return typeof objectProperty === 'number' && Number.isNaN(objectProperty); } exports.isValidNumber = isValidNumber;