| 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 |
|
|---|
| 9 | /** @type {{ [k in `[[${string}]]`]: (O: { toString?: unknown, valueOf?: unknown }, actualHint?: StringConstructor | NumberConstructor) => null | undefined | string | symbol | number | boolean | bigint; }} */
|
|---|
| 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) {
|
|---|
| 21 | /** @type {('toString' | 'valueOf')[]} */
|
|---|
| 22 | var methods = actualHint === String ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
|
|---|
| 23 | var value, i;
|
|---|
| 24 | for (i = 0; i < methods.length; ++i) {
|
|---|
| 25 | var method = methods[i];
|
|---|
| 26 | if (isCallable(O[method])) {
|
|---|
| 27 | // eslint-disable-next-line no-extra-parens
|
|---|
| 28 | value = /** @type {Function} */ (O[method])();
|
|---|
| 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 |
|
|---|
| 40 | /** @type {import('./es5')} */
|
|---|
| 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) {
|
|---|
| 47 | // eslint-disable-next-line no-extra-parens
|
|---|
| 48 | return ES5internalSlots['[[DefaultValue]]'](/** @type {object} */ (input), arguments[1]);
|
|---|
| 49 | }
|
|---|
| 50 | // eslint-disable-next-line no-extra-parens
|
|---|
| 51 | return ES5internalSlots['[[DefaultValue]]'](/** @type {object} */ (input));
|
|---|
| 52 | };
|
|---|