Add utility belt example
This commit is contained in:
14
examples/utility-belt/src/string-to-number.js
Normal file
14
examples/utility-belt/src/string-to-number.js
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
Reference in New Issue
Block a user