src/util/Util.js
/**
 * Convert obj to its numerical form.
 *
 * <ul>
 *   <li>number -> number, e.g. 2 -> 2</li>
 *   <li>[number] -> [number], e.g. [1,2,3] -> [1,2,3]</li>
 *   <li>numeral string -> number, e.g. "8" -> 8</li>
 *   <li>[number | numeral string] -> [number], e.g. [1, 2, "3"] -> [1,2,3]</li>
 * </ul>
 *
 * @name module:util.toNumerical
 * @function
 * @public
 * @param {Object} obj - the input object
 * @return {number | number[]} the numerical form of the input object
 */
Convert obj to its numerical form.


Convert obj to its numerical form .
function toNumerical(obj)
{
	const response = {
		origin: "util.toNumerical",
		context: "when converting an object to its numerical form",
	};
	try
	{
		if (obj === null)
		{
			throw "unable to convert null to a number";
		}
		if (typeof obj === "undefined")
		{
			throw "unable to convert undefined to a number";
		}
		if (typeof obj === "number")
		{
			return obj;
		}
		const convertToNumber = (input) =>
		{
			const n = Number.parseFloat(input);
			if (Number.isNaN(n))
			{
				throw `unable to convert ${input} to a number`;
			}
			return n;
		};
		if (Array.isArray(obj))
		{
			return obj.map(convertToNumber);
		}
		const arrayMaybe = turnSquareBracketsIntoArrays(obj);
		if (Array.isArray(arrayMaybe))
		{
			return arrayMaybe.map(convertToNumber);
		}
		if (typeof obj === "string")
		{
			return convertToNumber(obj);
		}
		throw "unable to convert the object to a number";
	}
	catch (error)
	{
		throw Object.assign(response, { error });
	}
}
function toNumerical ( obj ) { const response = { origin : "util.toNumerical" , context : "when converting an object to its numerical form" , } ; try { if ( obj === null ) { throw "unable to convert null to a number" ; } if ( typeof obj === "undefined" ) { throw "unable to convert undefined to a number" ; } if ( typeof obj === "number" ) { return obj ; } const convertToNumber = ( input ) => { const n = Number . parseFloat ( input ) ; if ( Number . isNaN ( n ) ) { throw ` ${ input } ` ; } return n ; } ; if ( Array . isArray ( obj ) ) { return obj . map ( convertToNumber ) ; } const arrayMaybe = turnSquareBracketsIntoArrays ( obj ) ; if ( Array . isArray ( arrayMaybe ) ) { return arrayMaybe . map ( convertToNumber ) ; } if ( typeof obj === "string" ) { return convertToNumber ( obj ) ; } throw "unable to convert the object to a number" ; } catch ( error ) { throw Object . assign ( response , { error } ) ; } }
Convert obj to its numerical form.
Convert obj to its numerical form .