1 | 'use strict';
|
---|
2 |
|
---|
3 | var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
|
---|
4 |
|
---|
5 | var isPrimitive = require('./helpers/isPrimitive');
|
---|
6 | var isCallable = require('is-callable');
|
---|
7 | var isDate = require('is-date-object');
|
---|
8 | var isSymbol = require('is-symbol');
|
---|
9 |
|
---|
10 | /** @type {(O: { valueOf?: () => unknown, toString?: () => unknown }, hint: 'number' | 'string' | 'default') => null | undefined | string | symbol | number | boolean | bigint} */
|
---|
11 | var ordinaryToPrimitive = function OrdinaryToPrimitive(O, hint) {
|
---|
12 | if (typeof O === 'undefined' || O === null) {
|
---|
13 | throw new TypeError('Cannot call method on ' + O);
|
---|
14 | }
|
---|
15 | if (typeof hint !== 'string' || (hint !== 'number' && hint !== 'string')) {
|
---|
16 | throw new TypeError('hint must be "string" or "number"');
|
---|
17 | }
|
---|
18 | /** @type {('toString' | 'valueOf')[]} */
|
---|
19 | var methodNames = hint === 'string' ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
|
---|
20 | var method, result, i;
|
---|
21 | for (i = 0; i < methodNames.length; ++i) {
|
---|
22 | method = O[methodNames[i]];
|
---|
23 | if (isCallable(method)) {
|
---|
24 | result = method.call(O);
|
---|
25 | if (isPrimitive(result)) {
|
---|
26 | return result;
|
---|
27 | }
|
---|
28 | }
|
---|
29 | }
|
---|
30 | throw new TypeError('No default value');
|
---|
31 | };
|
---|
32 |
|
---|
33 | /** @type {<K extends PropertyKey>(O: Record<K, unknown>, P: K) => Function | undefined} */
|
---|
34 | var GetMethod = function GetMethod(O, P) {
|
---|
35 | var func = O[P];
|
---|
36 | if (func !== null && typeof func !== 'undefined') {
|
---|
37 | if (!isCallable(func)) {
|
---|
38 | throw new TypeError(func + ' returned for property ' + String(P) + ' of object ' + O + ' is not a function');
|
---|
39 | }
|
---|
40 | return func;
|
---|
41 | }
|
---|
42 | return void 0;
|
---|
43 | };
|
---|
44 |
|
---|
45 | /** @type {import('./es2015')} */
|
---|
46 | // http://www.ecma-international.org/ecma-262/6.0/#sec-toprimitive
|
---|
47 | module.exports = function ToPrimitive(input) {
|
---|
48 | if (isPrimitive(input)) {
|
---|
49 | return input;
|
---|
50 | }
|
---|
51 | /** @type {'default' | 'string' | 'number'} */
|
---|
52 | var hint = 'default';
|
---|
53 | if (arguments.length > 1) {
|
---|
54 | if (arguments[1] === String) {
|
---|
55 | hint = 'string';
|
---|
56 | } else if (arguments[1] === Number) {
|
---|
57 | hint = 'number';
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | var exoticToPrim;
|
---|
62 | if (hasSymbols) {
|
---|
63 | if (Symbol.toPrimitive) {
|
---|
64 | // eslint-disable-next-line no-extra-parens
|
---|
65 | exoticToPrim = GetMethod(/** @type {Record<PropertyKey, unknown>} */ (input), Symbol.toPrimitive);
|
---|
66 | } else if (isSymbol(input)) {
|
---|
67 | exoticToPrim = Symbol.prototype.valueOf;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | if (typeof exoticToPrim !== 'undefined') {
|
---|
71 | var result = exoticToPrim.call(input, hint);
|
---|
72 | if (isPrimitive(result)) {
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 | throw new TypeError('unable to convert exotic object to primitive');
|
---|
76 | }
|
---|
77 | if (hint === 'default' && (isDate(input) || isSymbol(input))) {
|
---|
78 | hint = 'string';
|
---|
79 | }
|
---|
80 | // eslint-disable-next-line no-extra-parens
|
---|
81 | return ordinaryToPrimitive(/** @type {object} */ (input), hint === 'default' ? 'number' : hint);
|
---|
82 | };
|
---|