source: imaps-frontend/node_modules/es-abstract/2022/OrdinaryToPrimitive.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1018 bytes
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var Call = require('./Call');
6var Get = require('./Get');
7var IsCallable = require('./IsCallable');
8var Type = require('./Type');
9
10var inspect = require('object-inspect');
11
12// https://262.ecma-international.org/8.0/#sec-ordinarytoprimitive
13
14module.exports = function OrdinaryToPrimitive(O, hint) {
15 if (Type(O) !== 'Object') {
16 throw new $TypeError('Assertion failed: Type(O) is not Object');
17 }
18 if (/* typeof hint !== 'string' || */ hint !== 'string' && hint !== 'number') {
19 throw new $TypeError('Assertion failed: `hint` must be "string" or "number"');
20 }
21
22 var methodNames = hint === 'string' ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
23
24 for (var i = 0; i < methodNames.length; i += 1) {
25 var name = methodNames[i];
26 var method = Get(O, name);
27 if (IsCallable(method)) {
28 var result = Call(method, O);
29 if (Type(result) !== 'Object') {
30 return result;
31 }
32 }
33 }
34
35 throw new $TypeError('No primitive value for ' + inspect(O));
36};
Note: See TracBrowser for help on using the repository browser.