forked from tarao/LambdaJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compatibility.js
130 lines (130 loc) · 3.87 KB
/
compatibility.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
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt, from) {
var len = this.length;
if (typeof from == 'undefined') from = 0;
from = Number(from);
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0) from += len;
for (; from < len; from++) {
if (from in this && this[from] === elt) return from;
}
return -1;
};
}
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun, thisp) {
var len = this.length;
if (typeof fun != "function") {
throw new TypeError('filter: not a function');
}
var rv = new Array();
for (var i = 0; i < len; i++) {
if (i in this) {
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this)) rv.push(val);
}
}
return rv;
};
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun, thisp) {
var len = this.length;
if (typeof fun != 'function') {
throw new TypeError('forEach: not a function');
}
for (var i=0; i < len; i++) {
if (i in this) fun.call(thisp, this[i], i, this);
}
};
}
if (!Array.prototype.every) {
Array.prototype.every = function(fun, thisp) {
var len = this.length;
if (typeof fun != 'function') {
throw new TypeError('every: not a function');
}
for (var i = 0; i < len; i++) {
if (i in this && !fun.call(thisp, this[i], i, this)) {
return false;
}
}
return true;
};
}
if (!Array.prototype.map) {
Array.prototype.map = function(fun, thisp) {
var len = this.length;
if (typeof fun != 'function') {
throw new TypeError('map: not a function');
}
var rv = new Array(len);
for (var i = 0; i < len; i++) {
if (i in this) rv[i] = fun.call(thisp, this[i], i, this);
}
return rv;
};
}
if (!Array.prototype.some) {
Array.prototype.some = function(fun, thisp) {
var len = this.length;
if (typeof fun != "function") {
throw new TypeError('some: not a function');
}
for (var i = 0; i < len; i++) {
if (i in this && fun.call(thisp, this[i], i, this)) return true;
}
return false;
};
}
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(fun, initial) {
var len = this.length;
if (typeof fun != 'function') {
throw TypeError('reduce: not a function ');
}
var i = 0;
var prev;
var rv;
if (typeof initial != 'undefined') {
rv = initial;
} else {
do {
if (i in this) {
rv = this[i++];
break;
}
if (++i >= len) throw new TypeError('reduce: empty array');
} while (true);
}
for (; i < len; i++) {
if (i in this) rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
}
if (!Array.prototype.reduceRight) {
Array.prototype.reduceRight = function(fun, initial) {
var len = this.length;
if (typeof fun != "function") {
throw new TypeError('reduceRight: not a function');
}
var i = len - 1;
var rv;
if (typeof initial != 'undefined') {
rv = initial;
} else {
do {
if (i in this) {
rv = this[i--];
break;
}
if (--i < 0) throw new TypeError('reduceRight: empty array');
} while (true);
}
for (; i >= 0; i--) {
if (i in this) rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
}