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
|
Rev | Line | |
---|
[d565449] | 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');
|
---|
[79a0317] | 8 |
|
---|
| 9 | var isObject = require('../helpers/isObject');
|
---|
[d565449] | 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) {
|
---|
[79a0317] | 16 | if (!isObject(O)) {
|
---|
[d565449] | 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);
|
---|
[79a0317] | 30 | if (!isObject(result)) {
|
---|
[d565449] | 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.