[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var toStr = Object.prototype.toString;
|
---|
| 4 |
|
---|
| 5 | var isPrimitive = require('./helpers/isPrimitive');
|
---|
| 6 |
|
---|
| 7 | var isCallable = require('is-callable');
|
---|
| 8 |
|
---|
[0c6b92a] | 9 | /** @type {{ [k in `[[${string}]]`]: (O: { toString?: unknown, valueOf?: unknown }, actualHint?: StringConstructor | NumberConstructor) => null | undefined | string | symbol | number | boolean | bigint; }} */
|
---|
[d565449] | 10 | // http://ecma-international.org/ecma-262/5.1/#sec-8.12.8
|
---|
| 11 | var ES5internalSlots = {
|
---|
| 12 | '[[DefaultValue]]': function (O) {
|
---|
| 13 | var actualHint;
|
---|
| 14 | if (arguments.length > 1) {
|
---|
| 15 | actualHint = arguments[1];
|
---|
| 16 | } else {
|
---|
| 17 | actualHint = toStr.call(O) === '[object Date]' ? String : Number;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | if (actualHint === String || actualHint === Number) {
|
---|
[0c6b92a] | 21 | /** @type {('toString' | 'valueOf')[]} */
|
---|
[d565449] | 22 | var methods = actualHint === String ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
|
---|
| 23 | var value, i;
|
---|
| 24 | for (i = 0; i < methods.length; ++i) {
|
---|
[0c6b92a] | 25 | var method = methods[i];
|
---|
| 26 | if (isCallable(O[method])) {
|
---|
| 27 | // eslint-disable-next-line no-extra-parens
|
---|
| 28 | value = /** @type {Function} */ (O[method])();
|
---|
[d565449] | 29 | if (isPrimitive(value)) {
|
---|
| 30 | return value;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 | throw new TypeError('No default value');
|
---|
| 35 | }
|
---|
| 36 | throw new TypeError('invalid [[DefaultValue]] hint supplied');
|
---|
| 37 | }
|
---|
| 38 | };
|
---|
| 39 |
|
---|
[0c6b92a] | 40 | /** @type {import('./es5')} */
|
---|
[d565449] | 41 | // http://ecma-international.org/ecma-262/5.1/#sec-9.1
|
---|
| 42 | module.exports = function ToPrimitive(input) {
|
---|
| 43 | if (isPrimitive(input)) {
|
---|
| 44 | return input;
|
---|
| 45 | }
|
---|
| 46 | if (arguments.length > 1) {
|
---|
[0c6b92a] | 47 | // eslint-disable-next-line no-extra-parens
|
---|
| 48 | return ES5internalSlots['[[DefaultValue]]'](/** @type {object} */ (input), arguments[1]);
|
---|
[d565449] | 49 | }
|
---|
[0c6b92a] | 50 | // eslint-disable-next-line no-extra-parens
|
---|
| 51 | return ES5internalSlots['[[DefaultValue]]'](/** @type {object} */ (input));
|
---|
[d565449] | 52 | };
|
---|