Files
introduction-to-testing/examples/utility-belt/src/string-to-number.js
2024-09-29 16:55:08 -06:00

15 lines
338 B
JavaScript

/**
* Converts a string to a number or throws an error if the string is not a number.
* @param {string} value
* @returns {number} The number.
*/
export const stringToNumber = (value) => {
const number = Number(value);
if (isNaN(number)) {
throw new Error(`'${value}' cannot be parsed as a number.`);
}
return number;
};