From 931604be728e511913379eae5afcb360620d0ab4 Mon Sep 17 00:00:00 2001 From: Dean Kerr Date: Thu, 25 Apr 2019 10:22:26 +0100 Subject: [PATCH] fix: regression when setting raw value to empty string --- README.md | 1 - src/helpers.ts | 8 ++++++-- 2 files changed, 6 insertions(+), 3 deletions(-) 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) {