Skip to content

Commit 23d9612

Browse files
committed
1 parent d9e0244 commit 23d9612

26 files changed

+881
-0
lines changed

Array/every.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
3+
*/
4+
if (!Array.prototype.every) {
5+
Array.prototype.every = function(callback, thisArg) {
6+
var T, k;
7+
8+
if (this === void 0 || this === null) {
9+
throw new TypeError('Array.prototype.every called on null or undefined');
10+
}
11+
12+
// 1. Let O be the result of calling ToObject passing the this
13+
// value as the argument.
14+
var O = Object(this);
15+
16+
// 2. Let lenValue be the result of calling the Get internal method
17+
// of O with the argument "length".
18+
// 3. Let len be ToUint32(lenValue).
19+
var len = O.length >>> 0;
20+
21+
// 4. If IsCallable(callback) is false, throw a TypeError exception.
22+
if (callback.__class__ !== 'Function') {
23+
throw new TypeError(callback + ' is not a function');
24+
}
25+
26+
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
27+
T = (arguments.length > 1) ? thisArg : void 0;
28+
29+
// 6. Let k be 0.
30+
k = 0;
31+
32+
// 7. Repeat, while k < len
33+
while (k < len) {
34+
35+
var kValue;
36+
37+
// a. Let Pk be ToString(k).
38+
// This is implicit for LHS operands of the in operator
39+
// b. Let kPresent be the result of calling the HasProperty internal
40+
// method of O with argument Pk.
41+
// This step can be combined with c
42+
// c. If kPresent is true, then
43+
if (k in O) {
44+
45+
// i. Let kValue be the result of calling the Get internal method
46+
// of O with argument Pk.
47+
kValue = O[k];
48+
49+
// ii. Let testResult be the result of calling the Call internal method
50+
// of callback with T as the this value and argument list
51+
// containing kValue, k, and O.
52+
var testResult = callback.call(T, kValue, k, O);
53+
54+
// iii. If ToBoolean(testResult) is false, return false.
55+
if (!testResult) {
56+
return false;
57+
}
58+
}
59+
k++;
60+
}
61+
return true;
62+
};
63+
}

Array/filter.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
3+
*/
4+
if (!Array.prototype.filter) {
5+
Array.prototype.filter = function(callback, thisArg) {
6+
7+
if (this === void 0 || this === null) {
8+
throw new TypeError('Array.prototype.filter called on null or undefined');
9+
}
10+
11+
var t = Object(this);
12+
var len = t.length >>> 0;
13+
14+
if (callback.__class__ !== 'Function') {
15+
throw new TypeError(callback + ' is not a function');
16+
}
17+
18+
var res = [];
19+
20+
var T = (arguments.length > 1) ? thisArg : void 0;
21+
22+
for (var i = 0; i < len; i++) {
23+
if (i in t) {
24+
var val = t[i];
25+
26+
// NOTE: Technically this should Object.defineProperty at
27+
// the next index, as push can be affected by
28+
// properties on Object.prototype and Array.prototype.
29+
// But that method's new, and collisions should be
30+
// rare, so use the more-compatible alternative.
31+
if (callback.call(T, val, i, t)) {
32+
res.push(val);
33+
}
34+
}
35+
}
36+
37+
return res;
38+
};
39+
}

Array/forEach.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
3+
*/
4+
// Production steps of ECMA-262, Edition 5, 15.4.4.18
5+
// Reference: http://es5.github.io/#x15.4.4.18
6+
if (!Array.prototype.forEach) {
7+
Array.prototype.forEach = function(callback, thisArg) {
8+
9+
10+
if (this === void 0 || this === null) {
11+
throw new TypeError('Array.prototype.forEach called on null or undefined');
12+
}
13+
14+
// 1. Let O be the result of calling toObject() passing the
15+
// |this| value as the argument.
16+
var O = Object(this);
17+
18+
// 2. Let lenValue be the result of calling the Get() internal
19+
// method of O with the argument "length".
20+
// 3. Let len be toUint32(lenValue).
21+
var len = O.length >>> 0;
22+
23+
// 4. If isCallable(callback) is false, throw a TypeError exception.
24+
// See: http://es5.github.com/#x9.11
25+
if (callback.__class__ !== 'Function') {
26+
throw new TypeError(callback + ' is not a function');
27+
}
28+
29+
// 5. If thisArg was supplied, let T be thisArg; else let
30+
// T be undefined.
31+
var T = (arguments.length > 1) ? thisArg : void 0;
32+
33+
34+
// 6. Let k be 0
35+
//k = 0;
36+
37+
// 7. Repeat, while k < len
38+
for (var k = 0; k < len; k++) {
39+
var kValue;
40+
// a. Let Pk be ToString(k).
41+
// This is implicit for LHS operands of the in operator
42+
// b. Let kPresent be the result of calling the HasProperty
43+
// internal method of O with argument Pk.
44+
// This step can be combined with c
45+
// c. If kPresent is true, then
46+
if (k in O) {
47+
// i. Let kValue be the result of calling the Get internal
48+
// method of O with argument Pk.
49+
kValue = O[k];
50+
// ii. Call the Call internal method of callback with T as
51+
// the this value and argument list containing kValue, k, and O.
52+
callback.call(T, kValue, k, O);
53+
}
54+
}
55+
// 8. return undefined
56+
}
57+
}

Array/indexOf.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill
3+
*/
4+
// Production steps of ECMA-262, Edition 5, 15.4.4.14
5+
// Reference: http://es5.github.io/#x15.4.4.14
6+
if (!Array.prototype.indexOf) {
7+
Array.prototype.indexOf = function(searchElement, fromIndex) {
8+
9+
10+
// 1. Let o be the result of calling ToObject passing
11+
// the this value as the argument.
12+
if (this === void 0 || this === null) {
13+
throw new TypeError('Array.prototype.indexOf called on null or undefined');
14+
}
15+
16+
var k;
17+
var o = Object(this);
18+
19+
// 2. Let lenValue be the result of calling the Get
20+
// internal method of o with the argument "length".
21+
// 3. Let len be ToUint32(lenValue).
22+
var len = o.length >>> 0;
23+
24+
// 4. If len is 0, return -1.
25+
if (len === 0) {
26+
return -1;
27+
}
28+
29+
// 5. If argument fromIndex was passed let n be
30+
// ToInteger(fromIndex); else let n be 0.
31+
var n = +fromIndex || 0;
32+
33+
if (Math.abs(n) === Infinity) {
34+
n = 0;
35+
}
36+
37+
// 6. If n >= len, return -1.
38+
if (n >= len) {
39+
return -1;
40+
}
41+
42+
// 7. If n >= 0, then Let k be n.
43+
// 8. Else, n<0, Let k be len - abs(n).
44+
// If k is less than 0, then let k be 0.
45+
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
46+
47+
// 9. Repeat, while k < len
48+
while (k < len) {
49+
// a. Let Pk be ToString(k).
50+
// This is implicit for LHS operands of the in operator
51+
// b. Let kPresent be the result of calling the
52+
// HasProperty internal method of o with argument Pk.
53+
// This step can be combined with c
54+
// c. If kPresent is true, then
55+
// i. Let elementK be the result of calling the Get
56+
// internal method of o with the argument ToString(k).
57+
// ii. Let same be the result of applying the
58+
// Strict Equality Comparison Algorithm to
59+
// searchElement and elementK.
60+
// iii. If same is true, return k.
61+
if (k in o && o[k] === searchElement) {
62+
return k;
63+
}
64+
k++;
65+
}
66+
return -1;
67+
};
68+
}

Array/isArray.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
3+
*/
4+
if (!Array.isArray) {
5+
Array.isArray = function(arg) {
6+
7+
if (arg === void 0 || arg === null) {
8+
return false;
9+
}
10+
return (arg.__class__ === 'Array');
11+
};
12+
}

Array/lastIndexOf.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
3+
*/
4+
// Production steps of ECMA-262, Edition 5, 15.4.4.15
5+
// Reference: http://es5.github.io/#x15.4.4.15
6+
if (!Array.prototype.lastIndexOf) {
7+
Array.prototype.lastIndexOf = function(searchElement, fromIndex) {
8+
9+
if (this === void 0 || this === null) {
10+
throw new TypeError('Array.prototype.lastIndexOf called on null or undefined');
11+
}
12+
13+
var n, k,
14+
t = Object(this),
15+
len = t.length >>> 0;
16+
if (len === 0) {
17+
return -1;
18+
}
19+
20+
n = len - 1;
21+
if (arguments.length > 1) {
22+
n = Number(arguments[1]);
23+
if (n != n) {
24+
n = 0;
25+
}
26+
else if (n != 0 && n != Infinity && n != -Infinity) {
27+
n = (n > 0 || -1) * Math.floor(Math.abs(n));
28+
}
29+
}
30+
31+
for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) {
32+
if (k in t && t[k] === searchElement) {
33+
return k;
34+
}
35+
}
36+
return -1;
37+
};
38+
}

Array/map.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
3+
*/
4+
// Production steps of ECMA-262, Edition 5, 15.4.4.19
5+
// Reference: http://es5.github.io/#x15.4.4.19
6+
if (!Array.prototype.map) {
7+
8+
Array.prototype.map = function(callback, thisArg) {
9+
10+
var T, A, k;
11+
12+
if (this === void 0 || this === null) {
13+
throw new TypeError('Array.prototype.map called on null or undefined');
14+
}
15+
16+
// 1. Let O be the result of calling ToObject passing the |this|
17+
// value as the argument.
18+
var O = Object(this);
19+
20+
// 2. Let lenValue be the result of calling the Get internal
21+
// method of O with the argument "length".
22+
// 3. Let len be ToUint32(lenValue).
23+
var len = O.length >>> 0;
24+
25+
// 4. If IsCallable(callback) is false, throw a TypeError exception.
26+
// See: http://es5.github.com/#x9.11
27+
if (callback.__class__ !== 'Function') {
28+
throw new TypeError(callback + ' is not a function');
29+
}
30+
31+
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
32+
T = (arguments.length > 1) ? thisArg : void 0;
33+
34+
// 6. Let A be a new array created as if by the expression new Array(len)
35+
// where Array is the standard built-in constructor with that name and
36+
// len is the value of len.
37+
A = new Array(len);
38+
39+
for (var k = 0; k < len; k++) {
40+
41+
var kValue, mappedValue;
42+
43+
// a. Let Pk be ToString(k).
44+
// This is implicit for LHS operands of the in operator
45+
// b. Let kPresent be the result of calling the HasProperty internal
46+
// method of O with argument Pk.
47+
// This step can be combined with c
48+
// c. If kPresent is true, then
49+
if (k in O) {
50+
51+
// i. Let kValue be the result of calling the Get internal
52+
// method of O with argument Pk.
53+
kValue = O[k];
54+
55+
// ii. Let mappedValue be the result of calling the Call internal
56+
// method of callback with T as the this value and argument
57+
// list containing kValue, k, and O.
58+
mappedValue = callback.call(T, kValue, k, O);
59+
60+
// iii. Call the DefineOwnProperty internal method of A with arguments
61+
// Pk, Property Descriptor
62+
// { Value: mappedValue,
63+
// Writable: true,
64+
// Enumerable: true,
65+
// Configurable: true },
66+
// and false.
67+
68+
// In browsers that support Object.defineProperty, use the following:
69+
// Object.defineProperty(A, k, {
70+
// value: mappedValue,
71+
// writable: true,
72+
// enumerable: true,
73+
// configurable: true
74+
// });
75+
76+
// For best browser support, use the following:
77+
A[k] = mappedValue;
78+
}
79+
}
80+
// 9. return A
81+
return A;
82+
};
83+
}

0 commit comments

Comments
 (0)