(
value: string,
options: ParseSafeIntegerStringOptions = {}
)
| 7 | const FINITE_NUMBER_STRING_PATTERN = /^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:[eE][+-]?\d+)?$/; |
| 8 | |
| 9 | export function parseSafeIntegerString( |
| 10 | value: string, |
| 11 | options: ParseSafeIntegerStringOptions = {} |
| 12 | ): number | null { |
| 13 | const trimmedValue = value.trim(); |
| 14 | const pattern = options.allowSign === true ? /^-?\d+$/ : /^\d+$/; |
| 15 | |
| 16 | if (!pattern.test(trimmedValue)) return null; |
| 17 | |
| 18 | const parsed = Number(trimmedValue); |
| 19 | if (!Number.isSafeInteger(parsed)) return null; |
| 20 | if (options.min !== undefined && parsed < options.min) return null; |
| 21 | if (options.max !== undefined && parsed > options.max) return null; |
| 22 | |
| 23 | return parsed; |
| 24 | } |
| 25 | |
| 26 | export function parseFiniteNumberString(value: string): number | null { |
| 27 | const trimmedValue = value.trim(); |
no outgoing calls
no test coverage detected