-
Notifications
You must be signed in to change notification settings - Fork 3
/
normalize.js
131 lines (113 loc) · 3.16 KB
/
normalize.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
if(typeof Array.prototype.add == "undefined") {
Array.prototype.add = function(item) {
this[this.length] = item;
return this;
}
}
if(typeof Array.prototype.last == "undefined") {
Array.prototype.last = function() {
return (this.length > 0) ? this[this.length - 1] : -1;
};
}
if(typeof Array.prototype.first == "undefined") {
Array.prototype.first = function() {
return this[0];
};
}
if(typeof Array.prototype.each == "undefined") {
Array.prototype.each = function(fn) {
for(var index = 0; index < this.length; index++) {
fn(this[index], index);
}
return this;
}
}
/*
Implementação da Mozilla
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
Navegadores defasados e antigos como o IE8/9 não possuem
Production steps of ECMA-262, Edition 5, 15.4.4.19
Reference: http://es5.github.com/#x15.4.4.19
*/
if (!Array.prototype.map) {
Array.prototype.map = function(callback, thisArg) {
var T, A, k;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0;
if ({}.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
A = new Array(len);
k = 0;
while(k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[ k ];
mappedValue = callback.call(T, kValue, k, O);
A[ k ] = mappedValue;
}
k++;
}
return A;
};
}
/*
Implementação da Mozilla
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
*/
if ( !Array.prototype.reduce ) {
Array.prototype.reduce = function reduce(accumlator){
var i, l = this.length, curr;
if(typeof accumlator !== "function")
throw new TypeError("First argument is not callable");
if((l == 0 || l === null) && (arguments.length <= 1))
throw new TypeError("Array length is 0 and no second argument");
if(arguments.length <= 1){
curr = this[0];
i = 1;
}
else{
curr = arguments[1];
}
for(i = i || 0 ; i < l ; ++i){
if(i in this)
curr = accumlator.call(undefined, curr, this[i], i, this);
}
return curr;
};
}
/*
Implementação da Mozilla
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
Navegadores defasados e antigos como o IE8/9 não possuem
*/
if(typeof Array.prototype.filter == "undefined") {
Array.prototype.filter = function(fun)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}