main
Last change
on this file since d565449 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
|
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');
|
---|
| 8 | var Type = require('./Type');
|
---|
| 9 |
|
---|
| 10 | var inspect = require('object-inspect');
|
---|
| 11 |
|
---|
| 12 | // https://262.ecma-international.org/8.0/#sec-ordinarytoprimitive
|
---|
| 13 |
|
---|
| 14 | module.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.