diff --git a/README.md b/README.md index 39649cf..73af836 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,6 @@ Retrieves the options on the input ##### rawValue Retrieves the raw value of the input (numerical) ->>>>>>> 16f3800... feat: update README to reflect various changes to API and development workflows #### value Retrieves the formatted value of the input (string) diff --git a/src/helpers.ts b/src/helpers.ts index 54c8c26..72dbca8 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -144,11 +144,15 @@ export const allowedZero = (val: string, char: string, caretPos: number, options } }; -export const formattedToRaw = (formattedValue: string, options: IOptions): number => { +export const formattedToRaw = (formattedValue: string, options: IOptions): number | undefined => { if (is.not.string(formattedValue)) { return NaN; } + if (!formattedValue.length) { + return undefined; + } + // Number(...) accepts thousands ',' or '' and decimal '.' so we must: // 1. Remove thousands delimiter to cover case it is not ',' @@ -202,7 +206,7 @@ export const parseString = (str: string, options: IOptions): string => { // Need to ensure that delimiter is a '.' before parsing to number const normalisedNumber = formattedToRaw(parsed, options); // Then swap it back in - const adjusted = rawToFormatted(normalisedNumber * multiplier, options); + const adjusted = rawToFormatted((normalisedNumber || 0) * multiplier, options); const tooLarge = adjusted.indexOf("e") !== -1; if (tooLarge) {