-
Notifications
You must be signed in to change notification settings - Fork 19
/
$es.js
137 lines (134 loc) · 3.57 KB
/
$es.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @author xiaojue[desingsor@gmail.com]
* @fileoverview $es for core
* @date 20150209
*/
(function(global, undef) {
var Obj = Object,
Fun = Function,
Arr = Array,
FP = Fun.prototype,
OP = Obj.prototype,
AP = Arr.prototype,
toString = OP.toString,
is = function(type,val) {
return toString.call(val) === '[object ' + type + ']';
},
$ES = {
isNum: function(val){
return is('Number',val);
},
isObj:function(val){
return is('Object',val);
},
isArr: function(val){
return is('Array',val);
},
isFun: function(val){
return is('Function',val);
},
isStr:function(val){
return is('String',val);
},
isReg:function(val){
return is('RegExp',val);
},
SameValue:function(a,b){
if (a === b) {
// 0 === -0, but they are not identical.
if (a === 0) return 1 / a === 1 / b;
return true;
}
return isNaN(a) && isNaN(b);
},
forEach: Arr.forEach ? function(arr, fn) {
arr.forEach(fn);
}: function(arr, fn) {
for (var i = 0; i < arr.length; i++) fn(arr[i], i, arr);
},
toArray: Arr.of ? Arr.of: function() {
return AP.slice.call(arguments);
},
bind: FP.bind ? function(fun, thisP) {
return fun.bind(thisP);
}: function(fun, thisP) {
if (!isFun(fun)) throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
var args = $ES.toArray(arguments).slice(1),
fToBind = fun,
fNOP = $ES.noop,
fBound = function() {
return fToBind.apply(this instanceof fNOP && thisP ? this: thisP, args.concat($ES.toArray(arguments)));
};
fNOP.prototype = fun.prototype;
fBound.prototype = new fNOP();
return fBound;
},
indexOf: AP.indexOf ? function(arr, selector) {
return arr.indexOf(selector);
}: function(arr, selector) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === selector) return i;
}
return - 1;
},
pad: function(number) {
if (number < 10) return '0' + number;
return number;
},
_toJSON: function() {
return this.valueOf();
},
noop: function() {},
toInteger: function(num) {
var n = Num(num);
if (n != num || n === Infinity) { //isNaN,expect string
n = 0;
} else if (n !== 0 && n != (1 / 0) && n != - (1 / 0)) {
n = (n > 0 || - 1) * M.floor(M.abs(n));
}
return n;
},
toLength: function(num) {
var len = toInteger(num);
return M.min(M.max(len, 0), Num6.maxSafeInteger);
},
keys: Obj.keys ? Obj.keys: function(o) {
var ret = [];
for (var p in o) {
if (o.hasOwnProperty(p)) ret.push(p);
}
return ret;
},
defineProperty: function() {
var supDefinePro = !! Obj.defineProperty && (function() {
try {
Obj.defineProperty({},
'x', {});
return true;
} catch(e) {
/*ie8 bug*/
return false;
}
} ());
return function(object, name, value, force) {
if (!force && name in object) return;
if (supDefinePro) {
Obj.defineProperty(object, name, {
configurable: true,
enumerable: false,
writable: true,
value: value
});
} else {
object[name] = valuel;
}
};
} (),
defineProperties: function(object,map,force) {
$ES.forEach($ES.keys(map),function(name){
$ES.defineProperty(object,name,map[name],force);
});
}
};
global.$ES = $ES;
})(this);