UNPKG

read-vietnamese-number

Version:
78 lines (77 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trimLeft = trimLeft; exports.trimRight = trimRight; exports.splitToDigits = splitToDigits; exports.validateNumber = validateNumber; /** * Remove the given character from the left side of the string. * * @param str the string * @param char the character to remove * @returns the string with the given character removed from the start */ function trimLeft(str, char) { if (str === '') { return ''; } let pos = 0; while (str[pos] === char[0]) { pos++; } return str.substring(pos); } /** * Remove the given character from the right side of the string. * * @param str the string * @param char the character to remove * @returns the string with the given character removed from the end */ function trimRight(str, char) { if (str === '') { return ''; } let lastPos = str.length - 1; while (str[lastPos] === char[0]) { lastPos--; } return str.substring(0, lastPos + 1); } /** * Split the given string into an array of digits. * * @param str the string * @returns an array of digits, or null if the string contains invalid characters */ function splitToDigits(str) { const digits = str.split('').map((digit) => { const value = parseInt(digit, 10); return isNaN(value) || value < 0 || value > 9 ? null : value; }); return digits.includes(null) ? null : digits; } /** * Validate the given input number and convert it to a string. * * @param number the input number * @returns a string representation of the number * @throws TypeError if the input number is not in a valid type */ function validateNumber(number) { switch (typeof number) { case 'string': { return number; } case 'bigint': { return number.toString(); } default: { // Throw error on number, object, undefined, boolean, symbol, function // Some numbers may cause errors when in parsing process: // - Loss precision: Number.isInteger(value) && !Number.isSafeInteger(value) // - Cannot parse scientific notation: value.toString().includes('e') throw new TypeError('The input number must be of type string or bigint'); } } }