source: imaps-frontend/node_modules/es-to-primitive/es5.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 1.7 KB
Line 
1'use strict';
2
3var toStr = Object.prototype.toString;
4
5var isPrimitive = require('./helpers/isPrimitive');
6
7var 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
11var 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
42module.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};
Note: See TracBrowser for help on using the repository browser.