underscore-rails 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 121c2f4878563957cac04e83f26f8f007b3a3152
4
- data.tar.gz: 0c1082f042569bf4d16f3813894bd41bce82dc88
3
+ metadata.gz: f08947a5b7d61a65c0dedc3e83c378102ac3ba38
4
+ data.tar.gz: f8ecfea15083768f5ebb388aaefdfaf96931f58a
5
5
  SHA512:
6
- metadata.gz: 0f217295aec0b04bc1d6df709a13a4864530d778febc168b96bd9ac23658977efd0c47d7eaeea68a12aa6fdd6fc1a60c9d637d9a206cd647a91096e6a26717fb
7
- data.tar.gz: 5ec01cbf3d18fa12addb51e516ed25f9d329518997bc9cfdbbce11d82e4128b8224cfbe4e87a779be99535343b75d54132b6d62da58e414067b4b2171c62e2e9
6
+ metadata.gz: 2fff345ea535304206346082950a729e918c567e2cddb917f38d4ea448f426a953c34d0df67528dce878313fa8bc0a1716e3eb3ca04a8aa6343671a16bbd78f6
7
+ data.tar.gz: 23dd626e89ec0831a9a84b8fd7a4949816420a2e0c0cb978949f26efe7e0c611c72515ca599056b476f7a09f171bc10a49635858f5ac255dbcf1abc954c88c7e
@@ -1,5 +1,5 @@
1
1
  module Underscore
2
2
  module Rails
3
- VERSION = "1.5.2"
3
+ VERSION = "1.6.0"
4
4
  end
5
5
  end
@@ -1,6 +1,6 @@
1
- // Underscore.js 1.5.2
1
+ // Underscore.js 1.6.0
2
2
  // http://underscorejs.org
3
- // (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
3
+ // (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
4
4
  // Underscore may be freely distributed under the MIT license.
5
5
 
6
6
  (function() {
@@ -65,7 +65,7 @@
65
65
  }
66
66
 
67
67
  // Current version.
68
- _.VERSION = '1.5.2';
68
+ _.VERSION = '1.6.0';
69
69
 
70
70
  // Collection Functions
71
71
  // --------------------
@@ -74,7 +74,7 @@
74
74
  // Handles objects with the built-in `forEach`, arrays, and raw objects.
75
75
  // Delegates to **ECMAScript 5**'s native `forEach` if available.
76
76
  var each = _.each = _.forEach = function(obj, iterator, context) {
77
- if (obj == null) return;
77
+ if (obj == null) return obj;
78
78
  if (nativeForEach && obj.forEach === nativeForEach) {
79
79
  obj.forEach(iterator, context);
80
80
  } else if (obj.length === +obj.length) {
@@ -87,6 +87,7 @@
87
87
  if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
88
88
  }
89
89
  }
90
+ return obj;
90
91
  };
91
92
 
92
93
  // Return the results of applying the iterator to each element.
@@ -152,10 +153,10 @@
152
153
  };
153
154
 
154
155
  // Return the first value which passes a truth test. Aliased as `detect`.
155
- _.find = _.detect = function(obj, iterator, context) {
156
+ _.find = _.detect = function(obj, predicate, context) {
156
157
  var result;
157
158
  any(obj, function(value, index, list) {
158
- if (iterator.call(context, value, index, list)) {
159
+ if (predicate.call(context, value, index, list)) {
159
160
  result = value;
160
161
  return true;
161
162
  }
@@ -166,33 +167,33 @@
166
167
  // Return all the elements that pass a truth test.
167
168
  // Delegates to **ECMAScript 5**'s native `filter` if available.
168
169
  // Aliased as `select`.
169
- _.filter = _.select = function(obj, iterator, context) {
170
+ _.filter = _.select = function(obj, predicate, context) {
170
171
  var results = [];
171
172
  if (obj == null) return results;
172
- if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
173
+ if (nativeFilter && obj.filter === nativeFilter) return obj.filter(predicate, context);
173
174
  each(obj, function(value, index, list) {
174
- if (iterator.call(context, value, index, list)) results.push(value);
175
+ if (predicate.call(context, value, index, list)) results.push(value);
175
176
  });
176
177
  return results;
177
178
  };
178
179
 
179
180
  // Return all the elements for which a truth test fails.
180
- _.reject = function(obj, iterator, context) {
181
+ _.reject = function(obj, predicate, context) {
181
182
  return _.filter(obj, function(value, index, list) {
182
- return !iterator.call(context, value, index, list);
183
+ return !predicate.call(context, value, index, list);
183
184
  }, context);
184
185
  };
185
186
 
186
187
  // Determine whether all of the elements match a truth test.
187
188
  // Delegates to **ECMAScript 5**'s native `every` if available.
188
189
  // Aliased as `all`.
189
- _.every = _.all = function(obj, iterator, context) {
190
- iterator || (iterator = _.identity);
190
+ _.every = _.all = function(obj, predicate, context) {
191
+ predicate || (predicate = _.identity);
191
192
  var result = true;
192
193
  if (obj == null) return result;
193
- if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
194
+ if (nativeEvery && obj.every === nativeEvery) return obj.every(predicate, context);
194
195
  each(obj, function(value, index, list) {
195
- if (!(result = result && iterator.call(context, value, index, list))) return breaker;
196
+ if (!(result = result && predicate.call(context, value, index, list))) return breaker;
196
197
  });
197
198
  return !!result;
198
199
  };
@@ -200,13 +201,13 @@
200
201
  // Determine if at least one element in the object matches a truth test.
201
202
  // Delegates to **ECMAScript 5**'s native `some` if available.
202
203
  // Aliased as `any`.
203
- var any = _.some = _.any = function(obj, iterator, context) {
204
- iterator || (iterator = _.identity);
204
+ var any = _.some = _.any = function(obj, predicate, context) {
205
+ predicate || (predicate = _.identity);
205
206
  var result = false;
206
207
  if (obj == null) return result;
207
- if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
208
+ if (nativeSome && obj.some === nativeSome) return obj.some(predicate, context);
208
209
  each(obj, function(value, index, list) {
209
- if (result || (result = iterator.call(context, value, index, list))) return breaker;
210
+ if (result || (result = predicate.call(context, value, index, list))) return breaker;
210
211
  });
211
212
  return !!result;
212
213
  };
@@ -232,25 +233,19 @@
232
233
 
233
234
  // Convenience version of a common use case of `map`: fetching a property.
234
235
  _.pluck = function(obj, key) {
235
- return _.map(obj, function(value){ return value[key]; });
236
+ return _.map(obj, _.property(key));
236
237
  };
237
238
 
238
239
  // Convenience version of a common use case of `filter`: selecting only objects
239
240
  // containing specific `key:value` pairs.
240
- _.where = function(obj, attrs, first) {
241
- if (_.isEmpty(attrs)) return first ? void 0 : [];
242
- return _[first ? 'find' : 'filter'](obj, function(value) {
243
- for (var key in attrs) {
244
- if (attrs[key] !== value[key]) return false;
245
- }
246
- return true;
247
- });
241
+ _.where = function(obj, attrs) {
242
+ return _.filter(obj, _.matches(attrs));
248
243
  };
249
244
 
250
245
  // Convenience version of a common use case of `find`: getting the first object
251
246
  // containing specific `key:value` pairs.
252
247
  _.findWhere = function(obj, attrs) {
253
- return _.where(obj, attrs, true);
248
+ return _.find(obj, _.matches(attrs));
254
249
  };
255
250
 
256
251
  // Return the maximum element or (element-based computation).
@@ -260,13 +255,15 @@
260
255
  if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
261
256
  return Math.max.apply(Math, obj);
262
257
  }
263
- if (!iterator && _.isEmpty(obj)) return -Infinity;
264
- var result = {computed : -Infinity, value: -Infinity};
258
+ var result = -Infinity, lastComputed = -Infinity;
265
259
  each(obj, function(value, index, list) {
266
260
  var computed = iterator ? iterator.call(context, value, index, list) : value;
267
- computed > result.computed && (result = {value : value, computed : computed});
261
+ if (computed > lastComputed) {
262
+ result = value;
263
+ lastComputed = computed;
264
+ }
268
265
  });
269
- return result.value;
266
+ return result;
270
267
  };
271
268
 
272
269
  // Return the minimum element (or element-based computation).
@@ -274,17 +271,19 @@
274
271
  if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
275
272
  return Math.min.apply(Math, obj);
276
273
  }
277
- if (!iterator && _.isEmpty(obj)) return Infinity;
278
- var result = {computed : Infinity, value: Infinity};
274
+ var result = Infinity, lastComputed = Infinity;
279
275
  each(obj, function(value, index, list) {
280
276
  var computed = iterator ? iterator.call(context, value, index, list) : value;
281
- computed < result.computed && (result = {value : value, computed : computed});
277
+ if (computed < lastComputed) {
278
+ result = value;
279
+ lastComputed = computed;
280
+ }
282
281
  });
283
- return result.value;
282
+ return result;
284
283
  };
285
284
 
286
285
  // Shuffle an array, using the modern version of the
287
- // [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
286
+ // [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/FisherYates_shuffle).
288
287
  _.shuffle = function(obj) {
289
288
  var rand;
290
289
  var index = 0;
@@ -297,11 +296,12 @@
297
296
  return shuffled;
298
297
  };
299
298
 
300
- // Sample **n** random values from an array.
301
- // If **n** is not specified, returns a single random element from the array.
299
+ // Sample **n** random values from a collection.
300
+ // If **n** is not specified, returns a single random element.
302
301
  // The internal `guard` argument allows it to work with `map`.
303
302
  _.sample = function(obj, n, guard) {
304
- if (arguments.length < 2 || guard) {
303
+ if (n == null || guard) {
304
+ if (obj.length !== +obj.length) obj = _.values(obj);
305
305
  return obj[_.random(obj.length - 1)];
306
306
  }
307
307
  return _.shuffle(obj).slice(0, Math.max(0, n));
@@ -309,12 +309,14 @@
309
309
 
310
310
  // An internal function to generate lookup iterators.
311
311
  var lookupIterator = function(value) {
312
- return _.isFunction(value) ? value : function(obj){ return obj[value]; };
312
+ if (value == null) return _.identity;
313
+ if (_.isFunction(value)) return value;
314
+ return _.property(value);
313
315
  };
314
316
 
315
317
  // Sort the object's values by a criterion produced by an iterator.
316
- _.sortBy = function(obj, value, context) {
317
- var iterator = lookupIterator(value);
318
+ _.sortBy = function(obj, iterator, context) {
319
+ iterator = lookupIterator(iterator);
318
320
  return _.pluck(_.map(obj, function(value, index, list) {
319
321
  return {
320
322
  value: value,
@@ -334,9 +336,9 @@
334
336
 
335
337
  // An internal function used for aggregate "group by" operations.
336
338
  var group = function(behavior) {
337
- return function(obj, value, context) {
339
+ return function(obj, iterator, context) {
338
340
  var result = {};
339
- var iterator = value == null ? _.identity : lookupIterator(value);
341
+ iterator = lookupIterator(iterator);
340
342
  each(obj, function(value, index) {
341
343
  var key = iterator.call(context, value, index, obj);
342
344
  behavior(result, key, value);
@@ -348,7 +350,7 @@
348
350
  // Groups the object's values by a criterion. Pass either a string attribute
349
351
  // to group by, or a function that returns the criterion.
350
352
  _.groupBy = group(function(result, key, value) {
351
- (_.has(result, key) ? result[key] : (result[key] = [])).push(value);
353
+ _.has(result, key) ? result[key].push(value) : result[key] = [value];
352
354
  });
353
355
 
354
356
  // Indexes the object's values by a criterion, similar to `groupBy`, but for
@@ -367,7 +369,7 @@
367
369
  // Use a comparator function to figure out the smallest index at which
368
370
  // an object should be inserted so as to maintain order. Uses binary search.
369
371
  _.sortedIndex = function(array, obj, iterator, context) {
370
- iterator = iterator == null ? _.identity : lookupIterator(iterator);
372
+ iterator = lookupIterator(iterator);
371
373
  var value = iterator.call(context, obj);
372
374
  var low = 0, high = array.length;
373
375
  while (low < high) {
@@ -399,7 +401,9 @@
399
401
  // allows it to work with `_.map`.
400
402
  _.first = _.head = _.take = function(array, n, guard) {
401
403
  if (array == null) return void 0;
402
- return (n == null) || guard ? array[0] : slice.call(array, 0, n);
404
+ if ((n == null) || guard) return array[0];
405
+ if (n < 0) return [];
406
+ return slice.call(array, 0, n);
403
407
  };
404
408
 
405
409
  // Returns everything but the last entry of the array. Especially useful on
@@ -414,11 +418,8 @@
414
418
  // values in the array. The **guard** check allows it to work with `_.map`.
415
419
  _.last = function(array, n, guard) {
416
420
  if (array == null) return void 0;
417
- if ((n == null) || guard) {
418
- return array[array.length - 1];
419
- } else {
420
- return slice.call(array, Math.max(array.length - n, 0));
421
- }
421
+ if ((n == null) || guard) return array[array.length - 1];
422
+ return slice.call(array, Math.max(array.length - n, 0));
422
423
  };
423
424
 
424
425
  // Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
@@ -459,6 +460,16 @@
459
460
  return _.difference(array, slice.call(arguments, 1));
460
461
  };
461
462
 
463
+ // Split an array into two arrays: one whose elements all satisfy the given
464
+ // predicate, and one whose elements all do not satisfy the predicate.
465
+ _.partition = function(array, predicate) {
466
+ var pass = [], fail = [];
467
+ each(array, function(elem) {
468
+ (predicate(elem) ? pass : fail).push(elem);
469
+ });
470
+ return [pass, fail];
471
+ };
472
+
462
473
  // Produce a duplicate-free version of the array. If the array has already
463
474
  // been sorted, you have the option of using a faster algorithm.
464
475
  // Aliased as `unique`.
@@ -492,7 +503,7 @@
492
503
  var rest = slice.call(arguments, 1);
493
504
  return _.filter(_.uniq(array), function(item) {
494
505
  return _.every(rest, function(other) {
495
- return _.indexOf(other, item) >= 0;
506
+ return _.contains(other, item);
496
507
  });
497
508
  });
498
509
  };
@@ -507,7 +518,7 @@
507
518
  // Zip together multiple lists into a single array -- elements that share
508
519
  // an index go together.
509
520
  _.zip = function() {
510
- var length = _.max(_.pluck(arguments, "length").concat(0));
521
+ var length = _.max(_.pluck(arguments, 'length').concat(0));
511
522
  var results = new Array(length);
512
523
  for (var i = 0; i < length; i++) {
513
524
  results[i] = _.pluck(arguments, '' + i);
@@ -613,19 +624,27 @@
613
624
  };
614
625
 
615
626
  // Partially apply a function by creating a version that has had some of its
616
- // arguments pre-filled, without changing its dynamic `this` context.
627
+ // arguments pre-filled, without changing its dynamic `this` context. _ acts
628
+ // as a placeholder, allowing any combination of arguments to be pre-filled.
617
629
  _.partial = function(func) {
618
- var args = slice.call(arguments, 1);
630
+ var boundArgs = slice.call(arguments, 1);
619
631
  return function() {
620
- return func.apply(this, args.concat(slice.call(arguments)));
632
+ var position = 0;
633
+ var args = boundArgs.slice();
634
+ for (var i = 0, length = args.length; i < length; i++) {
635
+ if (args[i] === _) args[i] = arguments[position++];
636
+ }
637
+ while (position < arguments.length) args.push(arguments[position++]);
638
+ return func.apply(this, args);
621
639
  };
622
640
  };
623
641
 
624
- // Bind all of an object's methods to that object. Useful for ensuring that
625
- // all callbacks defined on an object belong to it.
642
+ // Bind a number of an object's methods to that object. Remaining arguments
643
+ // are the method names to be bound. Useful for ensuring that all callbacks
644
+ // defined on an object belong to it.
626
645
  _.bindAll = function(obj) {
627
646
  var funcs = slice.call(arguments, 1);
628
- if (funcs.length === 0) throw new Error("bindAll must be passed function names");
647
+ if (funcs.length === 0) throw new Error('bindAll must be passed function names');
629
648
  each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
630
649
  return obj;
631
650
  };
@@ -664,12 +683,13 @@
664
683
  var previous = 0;
665
684
  options || (options = {});
666
685
  var later = function() {
667
- previous = options.leading === false ? 0 : new Date;
686
+ previous = options.leading === false ? 0 : _.now();
668
687
  timeout = null;
669
688
  result = func.apply(context, args);
689
+ context = args = null;
670
690
  };
671
691
  return function() {
672
- var now = new Date;
692
+ var now = _.now();
673
693
  if (!previous && options.leading === false) previous = now;
674
694
  var remaining = wait - (now - previous);
675
695
  context = this;
@@ -679,6 +699,7 @@
679
699
  timeout = null;
680
700
  previous = now;
681
701
  result = func.apply(context, args);
702
+ context = args = null;
682
703
  } else if (!timeout && options.trailing !== false) {
683
704
  timeout = setTimeout(later, remaining);
684
705
  }
@@ -692,24 +713,33 @@
692
713
  // leading edge, instead of the trailing.
693
714
  _.debounce = function(func, wait, immediate) {
694
715
  var timeout, args, context, timestamp, result;
716
+
717
+ var later = function() {
718
+ var last = _.now() - timestamp;
719
+ if (last < wait) {
720
+ timeout = setTimeout(later, wait - last);
721
+ } else {
722
+ timeout = null;
723
+ if (!immediate) {
724
+ result = func.apply(context, args);
725
+ context = args = null;
726
+ }
727
+ }
728
+ };
729
+
695
730
  return function() {
696
731
  context = this;
697
732
  args = arguments;
698
- timestamp = new Date();
699
- var later = function() {
700
- var last = (new Date()) - timestamp;
701
- if (last < wait) {
702
- timeout = setTimeout(later, wait - last);
703
- } else {
704
- timeout = null;
705
- if (!immediate) result = func.apply(context, args);
706
- }
707
- };
733
+ timestamp = _.now();
708
734
  var callNow = immediate && !timeout;
709
735
  if (!timeout) {
710
736
  timeout = setTimeout(later, wait);
711
737
  }
712
- if (callNow) result = func.apply(context, args);
738
+ if (callNow) {
739
+ result = func.apply(context, args);
740
+ context = args = null;
741
+ }
742
+
713
743
  return result;
714
744
  };
715
745
  };
@@ -731,11 +761,7 @@
731
761
  // allowing you to adjust arguments, run code before and after, and
732
762
  // conditionally execute the original function.
733
763
  _.wrap = function(func, wrapper) {
734
- return function() {
735
- var args = [func];
736
- push.apply(args, arguments);
737
- return wrapper.apply(this, args);
738
- };
764
+ return _.partial(wrapper, func);
739
765
  };
740
766
 
741
767
  // Returns a function that is the composition of a list of functions, each
@@ -765,8 +791,9 @@
765
791
 
766
792
  // Retrieve the names of an object's properties.
767
793
  // Delegates to **ECMAScript 5**'s native `Object.keys`
768
- _.keys = nativeKeys || function(obj) {
769
- if (obj !== Object(obj)) throw new TypeError('Invalid object');
794
+ _.keys = function(obj) {
795
+ if (!_.isObject(obj)) return [];
796
+ if (nativeKeys) return nativeKeys(obj);
770
797
  var keys = [];
771
798
  for (var key in obj) if (_.has(obj, key)) keys.push(key);
772
799
  return keys;
@@ -921,7 +948,8 @@
921
948
  // from different frames are.
922
949
  var aCtor = a.constructor, bCtor = b.constructor;
923
950
  if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
924
- _.isFunction(bCtor) && (bCtor instanceof bCtor))) {
951
+ _.isFunction(bCtor) && (bCtor instanceof bCtor))
952
+ && ('constructor' in a && 'constructor' in b)) {
925
953
  return false;
926
954
  }
927
955
  // Add the first object to the stack of traversed objects.
@@ -1061,6 +1089,30 @@
1061
1089
  return value;
1062
1090
  };
1063
1091
 
1092
+ _.constant = function(value) {
1093
+ return function () {
1094
+ return value;
1095
+ };
1096
+ };
1097
+
1098
+ _.property = function(key) {
1099
+ return function(obj) {
1100
+ return obj[key];
1101
+ };
1102
+ };
1103
+
1104
+ // Returns a predicate for checking whether an object has a given set of `key:value` pairs.
1105
+ _.matches = function(attrs) {
1106
+ return function(obj) {
1107
+ if (obj === attrs) return true; //avoid comparing an object to itself.
1108
+ for (var key in attrs) {
1109
+ if (attrs[key] !== obj[key])
1110
+ return false;
1111
+ }
1112
+ return true;
1113
+ }
1114
+ };
1115
+
1064
1116
  // Run a function **n** times.
1065
1117
  _.times = function(n, iterator, context) {
1066
1118
  var accum = Array(Math.max(0, n));
@@ -1077,6 +1129,9 @@
1077
1129
  return min + Math.floor(Math.random() * (max - min + 1));
1078
1130
  };
1079
1131
 
1132
+ // A (possibly faster) way to get the current timestamp as an integer.
1133
+ _.now = Date.now || function() { return new Date().getTime(); };
1134
+
1080
1135
  // List of HTML entities for escaping.
1081
1136
  var entityMap = {
1082
1137
  escape: {
@@ -1273,4 +1328,16 @@
1273
1328
 
1274
1329
  });
1275
1330
 
1276
- }).call(this);
1331
+ // AMD registration happens at the end for compatibility with AMD loaders
1332
+ // that may not enforce next-turn semantics on modules. Even though general
1333
+ // practice for AMD registration is to be anonymous, underscore registers
1334
+ // as a named module because, like jQuery, it is a base library that is
1335
+ // popular enough to be bundled in a third party lib, but not be part of
1336
+ // an AMD load request. Those cases could generate an error when an
1337
+ // anonymous define() is called outside of a loader request.
1338
+ if (typeof define === 'function' && define.amd) {
1339
+ define('underscore', [], function() {
1340
+ return _;
1341
+ });
1342
+ }
1343
+ }).call(this);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: underscore-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Wenglewski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-04 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  version: '0'
47
47
  requirements: []
48
48
  rubyforge_project: underscore-rails
49
- rubygems_version: 2.0.3
49
+ rubygems_version: 2.1.11
50
50
  signing_key:
51
51
  specification_version: 4
52
52
  summary: underscore.js asset pipeline provider/wrapper