38 lines
1003 B
JavaScript
38 lines
1003 B
JavaScript
'use strict';
|
|
|
|
var toStr = Object.prototype.toString;
|
|
|
|
var isPrimitive = require('./helpers/isPrimitive');
|
|
|
|
var isCallable = require('is-callable');
|
|
|
|
// https://es5.github.io/#x8.12
|
|
var ES5internalSlots = {
|
|
'[[DefaultValue]]': function (O, hint) {
|
|
var actualHint = hint || (toStr.call(O) === '[object Date]' ? String : Number);
|
|
|
|
if (actualHint === String || actualHint === Number) {
|
|
var methods = actualHint === String ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
|
|
var value, i;
|
|
for (i = 0; i < methods.length; ++i) {
|
|
if (isCallable(O[methods[i]])) {
|
|
value = O[methods[i]]();
|
|
if (isPrimitive(value)) {
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
throw new TypeError('No default value');
|
|
}
|
|
throw new TypeError('invalid [[DefaultValue]] hint supplied');
|
|
}
|
|
};
|
|
|
|
// https://es5.github.io/#x9
|
|
module.exports = function ToPrimitive(input, PreferredType) {
|
|
if (isPrimitive(input)) {
|
|
return input;
|
|
}
|
|
return ES5internalSlots['[[DefaultValue]]'](input, PreferredType);
|
|
};
|