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