-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do string to float casting with some safety nets
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { numericStringToFloat } from './numeric-string-to-float.utils'; | ||
|
||
describe('parseOptionalFloat', () => { | ||
it('should return undefined if the value is undefined', () => { | ||
expect(numericStringToFloat(undefined)).toBeUndefined(); | ||
}); | ||
|
||
it('should throw an exception if the value is not numeric', () => { | ||
expect(() => numericStringToFloat('foo')).toThrow('Invalid number: foo'); | ||
}); | ||
|
||
it('should return a numeric representation of the value if the value is numeric', () => { | ||
expect(numericStringToFloat('123.456')).toBe(123.456); | ||
}); | ||
|
||
it('should silently round a float to the maximum precision supported by javascript', () => { | ||
expect(numericStringToFloat('123.456789012345678901234567890')).toBe( | ||
123.45678901234568, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isNil } from 'lodash'; | ||
|
||
/** | ||
* Kind of like parseFloat(), but passing through undefined values, and handling | ||
* values that don't cast to a number. | ||
* | ||
* @debt It silently rounds to the maximum precision supported by Javascript any | ||
* input values that are numeric but beyond what can be represented in a | ||
* Javascript number (not BigInt). Infinity and -Infinity are also passed | ||
* through as corresponding Javascript Infinity numeric values. | ||
*/ | ||
export function numericStringToFloat( | ||
value: string | undefined, | ||
): number | undefined { | ||
// +(null) === 0, so we only cast if input is neither undefined nor null. | ||
if (!isNil(value)) { | ||
const floatValue = +value; | ||
if (!isNaN(floatValue)) { | ||
return floatValue; | ||
} | ||
throw new Error(`Invalid number: ${value}`); | ||
} | ||
return; | ||
} |