MCPcopy
hub / github.com/formatjs/formatjs / ToNumber

Function ToNumber

packages/ecma262-abstract/ToNumber.ts:19–47  ·  view source on GitHub ↗
(arg: any)

Source from the content-addressed store, hash-verified

17 * https://tc39.es/ecma262/#sec-tonumber
18 */
19export function ToNumber(arg: any): Decimal {
20 if (typeof arg === 'number') {
21 return new Decimal(arg)
22 }
23 if (typeof arg === 'bigint') {
24 return new Decimal(arg.toString())
25 }
26 invariant(typeof arg !== 'symbol', 'Symbol is not supported', TypeError)
27 if (arg === undefined) {
28 return new Decimal(NaN)
29 }
30 if (arg === null || arg === 0) {
31 return ZERO
32 }
33 if (arg === true) {
34 return new Decimal(1)
35 }
36 if (typeof arg === 'string') {
37 try {
38 return new Decimal(arg)
39 } catch {
40 return new Decimal(NaN)
41 }
42 }
43 invariant(typeof arg === 'object', 'object expected', TypeError)
44 let primValue = ToPrimitive(arg, 'number')
45 invariant(typeof primValue !== 'object', 'object expected', TypeError)
46 return ToNumber(primValue)
47}

Callers 5

ToIntegerIfIntegralFunction · 0.85
GetOperandsFunction · 0.85
ToIntegerFunction · 0.85
getFunction · 0.85
core.tsFile · 0.85

Calls 3

ToPrimitiveFunction · 0.85
invariantFunction · 0.70
toStringMethod · 0.45

Tested by

no test coverage detected