-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathnumber-property.ts
More file actions
34 lines (31 loc) · 1.21 KB
/
number-property.ts
File metadata and controls
34 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
/**
* Type describing the allowed values for a number input
* @docs-private
*/
export type NumberInput = string | number | null | undefined;
/** Coerces a data-bound value (typically a string) to a number. */
export function coerceNumberProperty(value: unknown): number;
export function coerceNumberProperty<D>(value: unknown, fallback: D): number | D;
export function coerceNumberProperty(value: unknown, fallbackValue = 0) {
if (_isNumberValue(value)) {
return Number(value);
}
return arguments.length === 2 ? fallbackValue : 0;
}
/**
* Whether the provided value is considered a number.
* @docs-private
*/
export function _isNumberValue(value: unknown): 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.
return !isNaN(parseFloat(String(value))) && !isNaN(Number(value));
}