public
Description: Framework-independent JavaScript collection methods.
Clone URL: git://github.com/osteele/collections-js.git
add Array.slice, global pluck
osteele (author)
Fri Apr 18 20:11:38 -0700 2008
commit  b1a67c4f8188e14eb4d6c0d89fdc09910ec0c7fd
tree    152e3eb0893df346a40645a497f93cfd73b46f68
parent  95224586cb58b259b6291cf9733e49fab62e13a4
0
...
11
12
13
 
 
 
14
15
16
...
11
12
13
14
15
16
17
18
19
0
@@ -11,6 +11,9 @@ those; the ones with the same name as prototype extensions have the
0
 same spec as those in the Prototype library; and there's a few odds
0
 and ends such as String#capitalize.
0
 
0
+The methods are documented in lib/collections.js, and speced (for
0
+further documentation, as well as testing) in spec/*.js.
0
+
0
 License
0
 =======
0
 Copyright 2007-2008 by Oliver Steele. Available under the MIT License.
...
220
221
222
 
223
224
 
225
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
228
229
...
220
221
222
223
224
 
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
0
@@ -220,10 +220,28 @@ define(Array, 'partitionBy', function(fn) {
0
 
0
 })();
0
 
0
+
0
 /*
0
- * Monadic Arrays
0
+ * Global array functions
0
  */
0
 
0
+if (!Array.slice) { // mozilla already defines this
0
+ Array.slice = (function() {
0
+ var slice = Array.prototype.slice;
0
+ return function(array) {
0
+ return slice.apply(array, slice.call(arguments, 1));
0
+ };
0
+ })();
0
+}
0
+
0
+/* Return a function that returns the value of the `propertyName`
0
+ * property of its first argument. */
0
+function pluck(propertyName) {
0
+ return function(object) {
0
+ return object[propertyName];
0
+ }
0
+}
0
+
0
 /** Return an array. Identity for arrays; same as monadic `return`
0
   * for other values.
0
   */
...
191
192
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
195
196
...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
0
@@ -191,6 +191,27 @@ describe('Array.partitionBy', {
0
     }
0
 });
0
 
0
+describe('Array.slice', {
0
+ 'should slice': function() {
0
+ var array = [1,2,10,11,22];
0
+ value_of(Array.slice(array, 0).join(',')).should_be([1,2,10,11,22].join(','));
0
+ value_of(Array.slice(array, 1).join(',')).should_be([2,10,11,22].join(','));
0
+ value_of(Array.slice(array, 1, 3).join(',')).should_be([2,10].join(','));
0
+ }
0
+});
0
+
0
+describe('pluck', {
0
+ 'should work with map': function() {
0
+ value_of([{x:1,y:2}, {x:3,y:4}].map(pluck('x'))).should_be([1,3]);
0
+ },
0
+ 'should work with select': function() {
0
+ var array = [{x:1,y:2}, {z:3,w:4}];
0
+ var result = array.select(pluck('x'));
0
+ value_of(result.length).should_be(1);
0
+ value_of(result[0]).should_be(array[0]);
0
+ }
0
+});
0
+
0
 describe('Array.toList', {
0
     'should be identity on arrays': function() {
0
         value_of(Array.toList([1,2])).should_be([1,2]);

Comments

    No one has commented yet.