underscore-source 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Underscore
2
2
  module Source
3
- VERSION = "0.4.4"
3
+ VERSION = "0.4.5"
4
4
  end
5
5
  end
@@ -31,7 +31,7 @@
31
31
  if (typeof exports !== 'undefined') exports._ = _;
32
32
 
33
33
  // Current version.
34
- _.VERSION = '0.4.4';
34
+ _.VERSION = '0.4.5';
35
35
 
36
36
  /*------------------------ Collection Functions: ---------------------------*/
37
37
 
@@ -156,7 +156,7 @@
156
156
 
157
157
  // Invoke a method with arguments on every item in a collection.
158
158
  _.invoke = function(obj, method) {
159
- var args = _.toArray(arguments).slice(2);
159
+ var args = _.rest(arguments, 2);
160
160
  return _.map(obj, function(value) {
161
161
  return (method ? value[method] : value).apply(value, args);
162
162
  });
@@ -228,9 +228,17 @@
228
228
 
229
229
  /*-------------------------- Array Functions: ------------------------------*/
230
230
 
231
- // Get the first element of an array.
232
- _.first = function(array) {
233
- return array[0];
231
+ // Get the first element of an array. Passing "n" will return the first N
232
+ // values in the array. Aliased as "head".
233
+ _.first = function(array, n) {
234
+ return n ? Array.prototype.slice.call(array, 0, n) : array[0];
235
+ };
236
+
237
+ // Returns everything but the first entry of the array. Aliased as "tail".
238
+ // Especially useful on the arguments object. Passing an "index" will return
239
+ // the rest of the values in the array from that index onward.
240
+ _.rest = function(array, index) {
241
+ return Array.prototype.slice.call(array, _.isUndefined(index) ? 1 : index);
234
242
  };
235
243
 
236
244
  // Get the last element of an array.
@@ -254,7 +262,7 @@
254
262
 
255
263
  // Return a version of the array that does not contain the specified value(s).
256
264
  _.without = function(array) {
257
- var values = array.slice.call(arguments, 0);
265
+ var values = _.rest(arguments);
258
266
  return _.select(array, function(value){ return !_.include(values, value); });
259
267
  };
260
268
 
@@ -270,7 +278,7 @@
270
278
  // Produce an array that contains every item shared between all the
271
279
  // passed-in arrays.
272
280
  _.intersect = function(array) {
273
- var rest = _.toArray(arguments).slice(1);
281
+ var rest = _.rest(arguments);
274
282
  return _.select(_.uniq(array), function(item) {
275
283
  return _.all(rest, function(other) {
276
284
  return _.indexOf(other, item) >= 0;
@@ -311,20 +319,17 @@
311
319
  // Create a function bound to a given object (assigning 'this', and arguments,
312
320
  // optionally). Binding with arguments is also known as 'curry'.
313
321
  _.bind = function(func, context) {
314
- context = context || root;
315
- var args = _.toArray(arguments).slice(2);
322
+ var args = _.rest(arguments, 2);
316
323
  return function() {
317
- var a = args.concat(_.toArray(arguments));
318
- return func.apply(context, a);
324
+ return func.apply(context || root, args.concat(_.toArray(arguments)));
319
325
  };
320
326
  };
321
327
 
322
328
  // Bind all of an object's methods to that object. Useful for ensuring that
323
329
  // all callbacks defined on an object belong to it.
324
330
  _.bindAll = function() {
325
- var args = _.toArray(arguments);
326
- var context = args.pop();
327
- _.each(args, function(methodName) {
331
+ var context = Array.prototype.pop.call(arguments);
332
+ _.each(arguments, function(methodName) {
328
333
  context[methodName] = _.bind(context[methodName], context);
329
334
  });
330
335
  };
@@ -332,14 +337,14 @@
332
337
  // Delays a function for the given number of milliseconds, and then calls
333
338
  // it with the arguments supplied.
334
339
  _.delay = function(func, wait) {
335
- var args = _.toArray(arguments).slice(2);
340
+ var args = _.rest(arguments, 2);
336
341
  return setTimeout(function(){ return func.apply(func, args); }, wait);
337
342
  };
338
343
 
339
344
  // Defers a function, scheduling it to run after the current call stack has
340
345
  // cleared.
341
346
  _.defer = function(func) {
342
- return _.delay.apply(_, [func, 1].concat(_.toArray(arguments).slice(1)));
347
+ return _.delay.apply(_, [func, 1].concat(_.rest(arguments)));
343
348
  };
344
349
 
345
350
  // Returns the first function passed as an argument to the second,
@@ -507,6 +512,8 @@
507
512
  _.filter = _.select;
508
513
  _.every = _.all;
509
514
  _.some = _.any;
515
+ _.head = _.first;
516
+ _.tail = _.rest;
510
517
  _.methods = _.functions;
511
518
 
512
519
  /*------------------------ Setup the OOP Wrapper: --------------------------*/
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: underscore-source
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.4
5
+ version: 0.4.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Daniel X. Moore