-
Notifications
You must be signed in to change notification settings - Fork 24
/
array.js
197 lines (187 loc) · 5.09 KB
/
array.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Array prototype extensions. Doesn't depend on any
* other code. Doens't overwrite existing methods.
*
* Adds forEach, every, some, map, filter, indexOf and unique.
*
* Copyright (c) 2006 Jörn Zaefferer
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function() {
/**
* Adds a given method under the given name
* to the Array prototype if it doesn't
* currently exist.
*
* @private
*/
function add(name, method) {
if( !Array.prototype[name] ) {
Array.prototype[name] = method;
}
};
/**
* Executes a provided function once per array element.
*
* @example var stuff = "";
* ["foo", "bar"].forEach(function(element, index, array) {
* stuff += element;
* });
* @result "foobar";
*
* @param Function handler Function to execute for each element.
* @param Object scope (optional) Object to use as 'this' when executing handler.
* @name forEach
* @type undefined
* @cat Plugins/Methods/Array
*/
add("forEach", function(handler, scope) {
scope = scope || window;
for( var i = 0; i < this.length; i++)
handler.call(scope, this[i], i, this);
});
/**
* Tests whether all elements in the array pass the test
* implemented by the provided function.
*
* @example [12, 54, 18, 130, 44].every(function(element, index, array) {
* return element >= 10;
* });
* @result true;
*
* @example [12, 5, 8, 130, 44].every(function(element, index, array) {
* return element >= 10;
* });
* @result false;
*
* @param Function handler Function to execute for each element.
* @param Object scope (optional) Object to use as 'this' when executing handler.
* @name every
* @type Boolean
* @cat Plugins/Methods/Array
*/
add("every", function(handler, scope) {
scope = scope || window;
for( var i = 0; i < this.length; i++)
if( !handler.call(scope, this[i], i, this) )
return false;
return true;
});
/**
* Tests whether at least one element in the array passes the test
* implemented by the provided function.
*
* @example [12, 5, 8, 1, 44].some(function(element, index, array) {
* return element >= 10;
* });
* @result true;
*
* @example [2, 5, 8, 1, 4].some(function(element, index, array) {
* return element >= 10;
* });
* @result false;
*
* @param Function handler Function to execute for each element.
* @param Object scope (optional) Object to use as 'this' when executing handler.
* @name some
* @type Boolean
* @cat Plugins/Methods/Array
*/
add("some", function(handler, scope) {
scope = scope || window;
for( var i = 0; i < this.length; i++)
if( handler.call(scope, this[i], i, this) )
return true;
return false;
});
/**
* Creates a new array with the results of
* calling a provided function on every element in this array.
*
* @example ["hello", "Array", "WORLD"].map(function(element, index, array) {
* return element.toUpperCase();
* });
* @result ["HELLO", "ARRAY", "WORLD"];
*
* @example [1, 4, 9].map(Math.sqrt);
* @result [1, 2, 3];
*
* @param Function handler Function to execute for each element.
* @param Object scope (optional) Object to use as 'this' when executing handler.
* @name map
* @type Array
* @cat Plugins/Methods/Array
*/
add("map", function(handler, scope) {
scope = scope || window;
var r = [];
for( var i = 0; i < this.length; i++)
r[r.length] = handler.call(scope, this[i], i, this);
return r;
});
/**
* Creates a new array with all elements that pass
* the test implemented by the provided function.
*
* @example [12, 5, 8, 1, 44].filter(function(element, index, array) {
* return element >= 10;
* });
* @result [12, 44];
*
* @param Function handler Function to execute for each element.
* @param Object scope (optional) Object to use as 'this' when executing handler.
* @name filter
* @type Array
* @cat Plugins/Methods/Array
*/
add("filter", function(handler, scope) {
scope = scope || window;
var r = [];
for( var i = 0; i < this.length; i++)
if( handler.call(scope, this[i], i, this) )
r[r.length] = this[i];
return r;
});
/**
* Returns the first index at which a given element can
* be found in the array, or -1 if it is not present.
*
* @example [12, 5, 8, 5, 44].indexOf(5);
* @result 1;
*
* @example [12, 5, 8, 5, 44].indexOf(5, 2);
* @result 3;
*
* @param Object subject Object to search for
* @param Number offset (optional) Index at which to start searching
* @name filter
* @type Array
* @cat Plugins/Methods/Array
*/
add("indexOf", function(subject, offset) {
for( var i = offset || 0; i < this.length; i++)
if ( this[i] === subject )
return i;
return -1;
});
/**
* Returns a new array that contains all unique elements
* of this array.
*
* @example [1, 2, 1, 4, 5, 4].unique();
* @result [1, 2, 4, 5]
*
* @name unique
* @type Array
* @cat Plugins/Methods/Array
*/
add("unique", function() {
return this.filter(function(element, index, array) {
return array.indexOf(element) >= index;
});
});
})();