forked from ulic75/power-flow-card
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify state retrieval (ulic75#10)
- Loading branch information
Showing
2 changed files
with
40 additions
and
20 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 |
---|---|---|
@@ -1,4 +1,19 @@ | ||
/* eslint-disable no-redeclare */ | ||
export function coerceNumber(value: any): number; | ||
export function coerceNumber<D>(value: any, fallback: D): number | D; | ||
export function coerceNumber(value: any, fallbackValue = 0) { | ||
return _isNumberValue(value) ? Number(value) : fallbackValue; | ||
} | ||
|
||
export const roundValue = (value: number, decimalPlaces: number): number => { | ||
const factorOfTen = 10 ** decimalPlaces; | ||
return Math.round(value * factorOfTen) / factorOfTen; | ||
}; | ||
|
||
export function _isNumberValue(value: any): boolean { | ||
// parseFloat(value) handles most of the cases we're interested in (it treats null, empty string, | ||
// and other non-number values as NaN, where Number just uses 0) but it considers the string | ||
// '123hello' to be a valid number. Therefore we also check if Number(value) is NaN. | ||
// eslint-disable-next-line no-restricted-globals | ||
return !isNaN(parseFloat(value as any)) && !isNaN(Number(value)); | ||
} |