-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathes6.number.constructor.js
52 lines (52 loc) · 1.93 KB
/
es6.number.constructor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var $ = require('./$')
, global = require('./$.global')
, has = require('./$.has')
, cof = require('./$.cof')
, isObject = require('./$.is-object')
, fails = require('./$.fails')
, NUMBER = 'Number'
, $Number = global[NUMBER]
, Base = $Number
, proto = $Number.prototype
// Opera ~12 has broken Object#toString
, BROKEN_COF = cof($.create(proto)) == NUMBER;
var toPrimitive = function(it){
var fn, val;
if(typeof (fn = it.valueOf) == 'function' && !isObject(val = fn.call(it)))return val;
if(typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val;
throw TypeError("Can't convert object to number");
};
var toNumber = function(it){
if(isObject(it))it = toPrimitive(it);
if(typeof it == 'string' && it.length > 2 && it.charCodeAt(0) == 48){
var binary = false;
switch(it.charCodeAt(1)){
case 66 : case 98 : binary = true;
case 79 : case 111 : return parseInt(it.slice(2), binary ? 2 : 8);
}
} return +it;
};
if(!($Number('0o1') && $Number('0b1'))){
$Number = function Number(it){
var that = this;
return that instanceof $Number
// check on 1..constructor(foo) case
&& (BROKEN_COF ? fails(function(){ proto.valueOf.call(that); }) : cof(that) != NUMBER)
? new Base(toNumber(it)) : toNumber(it);
};
$.each.call(require('./$.support-desc') ? $.getNames(Base) : (
// ES3:
'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
// ES6 (in case, if modules with ES6 Number statics required before):
'EPSILON,isFinite,isInteger,isNaN,isSafeInteger,MAX_SAFE_INTEGER,' +
'MIN_SAFE_INTEGER,parseFloat,parseInt,isInteger'
).split(','), function(key){
if(has(Base, key) && !has($Number, key)){
$.setDesc($Number, key, $.getDesc(Base, key));
}
});
$Number.prototype = proto;
proto.constructor = $Number;
require('./$.redef')(global, NUMBER, $Number);
}