Skip to content

Commit

Permalink
fix: regression when setting raw value to empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
oriondean committed Apr 25, 2019
1 parent 8df2a94 commit 931604b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ','
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 931604b

Please sign in to comment.