MCPcopy Index your code
hub / github.com/formatjs/formatjs / OrdinaryToPrimitive

Function OrdinaryToPrimitive

packages/ecma262-abstract/ToPrimitive.ts:15–34  ·  view source on GitHub ↗
(O: object, hint: T)

Source from the content-addressed store, hash-verified

13}
14
15function OrdinaryToPrimitive<
16 T extends 'string' | 'number' = 'string' | 'number',
17>(O: object, hint: T): string | number | boolean | undefined | null {
18 let methodNames: Array<'toString' | 'valueOf'>
19 if (hint === 'string') {
20 methodNames = ['toString', 'valueOf']
21 } else {
22 methodNames = ['valueOf', 'toString']
23 }
24 for (const name of methodNames) {
25 const method = O[name]
26 if (IsCallable(method)) {
27 let result = method.call(O)
28 if (typeof result !== 'object') {
29 return result
30 }
31 }
32 }
33 throw new TypeError('Cannot convert object to primitive value')
34}
35
36/**
37 * https://tc39.es/ecma262/#sec-toprimitive

Callers 1

ToPrimitiveFunction · 0.85

Calls 1

IsCallableFunction · 0.70

Tested by

no test coverage detected