Permalink
codeql-action/node_modules/es-abstract/2023/OrdinaryToPrimitive.js
Newer
100644
38 lines (29 sloc)
1.04 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
5
var $TypeError = GetIntrinsic('%TypeError%');
6
7
var Call = require('./Call');
8
var Get = require('./Get');
9
var IsCallable = require('./IsCallable');
10
var Type = require('./Type');
11
12
var inspect = require('object-inspect');
13
14
// https://262.ecma-international.org/8.0/#sec-ordinarytoprimitive
15
16
module.exports = function OrdinaryToPrimitive(O, hint) {
17
if (Type(O) !== 'Object') {
18
throw new $TypeError('Assertion failed: Type(O) is not Object');
19
}
20
if (/* Type(hint) !== 'String' || */ hint !== 'string' && hint !== 'number') {
21
throw new $TypeError('Assertion failed: `hint` must be "string" or "number"');
22
}
23
24
var methodNames = hint === 'string' ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
25
26
for (var i = 0; i < methodNames.length; i += 1) {
27
var name = methodNames[i];
28
var method = Get(O, name);
29
if (IsCallable(method)) {
30
var result = Call(method, O);
31
if (Type(result) !== 'Object') {
32
return result;
33
}
34
}
35
}
36
37
throw new $TypeError('No primitive value for ' + inspect(O));
38
};