underscore-rails 1.4.2.1 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  module Underscore
2
2
  module Rails
3
- VERSION = "1.4.2.1"
3
+ VERSION = "1.4.3"
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
- // Underscore.js 1.4.2
1
+ // Underscore.js 1.4.3
2
2
  // http://underscorejs.org
3
3
  // (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
4
4
  // Underscore may be freely distributed under the MIT license.
@@ -24,7 +24,6 @@
24
24
  var push = ArrayProto.push,
25
25
  slice = ArrayProto.slice,
26
26
  concat = ArrayProto.concat,
27
- unshift = ArrayProto.unshift,
28
27
  toString = ObjProto.toString,
29
28
  hasOwnProperty = ObjProto.hasOwnProperty;
30
29
 
@@ -61,11 +60,11 @@
61
60
  }
62
61
  exports._ = _;
63
62
  } else {
64
- root['_'] = _;
63
+ root._ = _;
65
64
  }
66
65
 
67
66
  // Current version.
68
- _.VERSION = '1.4.2';
67
+ _.VERSION = '1.4.3';
69
68
 
70
69
  // Collection Functions
71
70
  // --------------------
@@ -102,6 +101,8 @@
102
101
  return results;
103
102
  };
104
103
 
104
+ var reduceError = 'Reduce of empty array with no initial value';
105
+
105
106
  // **Reduce** builds up a single result from a list of values, aka `inject`,
106
107
  // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
107
108
  _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
@@ -119,7 +120,7 @@
119
120
  memo = iterator.call(context, memo, value, index, list);
120
121
  }
121
122
  });
122
- if (!initial) throw new TypeError('Reduce of empty array with no initial value');
123
+ if (!initial) throw new TypeError(reduceError);
123
124
  return memo;
124
125
  };
125
126
 
@@ -130,7 +131,7 @@
130
131
  if (obj == null) obj = [];
131
132
  if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
132
133
  if (context) iterator = _.bind(iterator, context);
133
- return arguments.length > 2 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
134
+ return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
134
135
  }
135
136
  var length = obj.length;
136
137
  if (length !== +length) {
@@ -146,7 +147,7 @@
146
147
  memo = iterator.call(context, memo, obj[index], index, list);
147
148
  }
148
149
  });
149
- if (!initial) throw new TypeError('Reduce of empty array with no initial value');
150
+ if (!initial) throw new TypeError(reduceError);
150
151
  return memo;
151
152
  };
152
153
 
@@ -177,12 +178,9 @@
177
178
 
178
179
  // Return all the elements for which a truth test fails.
179
180
  _.reject = function(obj, iterator, context) {
180
- var results = [];
181
- if (obj == null) return results;
182
- each(obj, function(value, index, list) {
183
- if (!iterator.call(context, value, index, list)) results[results.length] = value;
184
- });
185
- return results;
181
+ return _.filter(obj, function(value, index, list) {
182
+ return !iterator.call(context, value, index, list);
183
+ }, context);
186
184
  };
187
185
 
188
186
  // Determine whether all of the elements match a truth test.
@@ -216,13 +214,11 @@
216
214
  // Determine if the array or object contains a given value (using `===`).
217
215
  // Aliased as `include`.
218
216
  _.contains = _.include = function(obj, target) {
219
- var found = false;
220
- if (obj == null) return found;
217
+ if (obj == null) return false;
221
218
  if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
222
- found = any(obj, function(value) {
219
+ return any(obj, function(value) {
223
220
  return value === target;
224
221
  });
225
- return found;
226
222
  };
227
223
 
228
224
  // Invoke a method (with arguments) on every item in a collection.
@@ -258,7 +254,7 @@
258
254
  return Math.max.apply(Math, obj);
259
255
  }
260
256
  if (!iterator && _.isEmpty(obj)) return -Infinity;
261
- var result = {computed : -Infinity};
257
+ var result = {computed : -Infinity, value: -Infinity};
262
258
  each(obj, function(value, index, list) {
263
259
  var computed = iterator ? iterator.call(context, value, index, list) : value;
264
260
  computed >= result.computed && (result = {value : value, computed : computed});
@@ -272,7 +268,7 @@
272
268
  return Math.min.apply(Math, obj);
273
269
  }
274
270
  if (!iterator && _.isEmpty(obj)) return Infinity;
275
- var result = {computed : Infinity};
271
+ var result = {computed : Infinity, value: Infinity};
276
272
  each(obj, function(value, index, list) {
277
273
  var computed = iterator ? iterator.call(context, value, index, list) : value;
278
274
  computed < result.computed && (result = {value : value, computed : computed});
@@ -321,7 +317,7 @@
321
317
  // An internal function used for aggregate "group by" operations.
322
318
  var group = function(obj, value, context, behavior) {
323
319
  var result = {};
324
- var iterator = lookupIterator(value);
320
+ var iterator = lookupIterator(value || _.identity);
325
321
  each(obj, function(value, index) {
326
322
  var key = iterator.call(context, value, index, obj);
327
323
  behavior(result, key, value);
@@ -341,7 +337,7 @@
341
337
  // either a string attribute to count by, or a function that returns the
342
338
  // criterion.
343
339
  _.countBy = function(obj, value, context) {
344
- return group(obj, value, context, function(result, key, value) {
340
+ return group(obj, value, context, function(result, key) {
345
341
  if (!_.has(result, key)) result[key] = 0;
346
342
  result[key]++;
347
343
  });
@@ -363,12 +359,14 @@
363
359
  // Safely convert anything iterable into a real, live array.
364
360
  _.toArray = function(obj) {
365
361
  if (!obj) return [];
366
- if (obj.length === +obj.length) return slice.call(obj);
362
+ if (_.isArray(obj)) return slice.call(obj);
363
+ if (obj.length === +obj.length) return _.map(obj, _.identity);
367
364
  return _.values(obj);
368
365
  };
369
366
 
370
367
  // Return the number of elements in an object.
371
368
  _.size = function(obj) {
369
+ if (obj == null) return 0;
372
370
  return (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
373
371
  };
374
372
 
@@ -379,6 +377,7 @@
379
377
  // values in the array. Aliased as `head` and `take`. The **guard** check
380
378
  // allows it to work with `_.map`.
381
379
  _.first = _.head = _.take = function(array, n, guard) {
380
+ if (array == null) return void 0;
382
381
  return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
383
382
  };
384
383
 
@@ -393,6 +392,7 @@
393
392
  // Get the last element of an array. Passing **n** will return the last N
394
393
  // values in the array. The **guard** check allows it to work with `_.map`.
395
394
  _.last = function(array, n, guard) {
395
+ if (array == null) return void 0;
396
396
  if ((n != null) && !guard) {
397
397
  return slice.call(array, Math.max(array.length - n, 0));
398
398
  } else {
@@ -410,7 +410,7 @@
410
410
 
411
411
  // Trim out all falsy values from an array.
412
412
  _.compact = function(array) {
413
- return _.filter(array, function(value){ return !!value; });
413
+ return _.filter(array, _.identity);
414
414
  };
415
415
 
416
416
  // Internal implementation of a recursive `flatten` function.
@@ -439,6 +439,11 @@
439
439
  // been sorted, you have the option of using a faster algorithm.
440
440
  // Aliased as `unique`.
441
441
  _.uniq = _.unique = function(array, isSorted, iterator, context) {
442
+ if (_.isFunction(isSorted)) {
443
+ context = iterator;
444
+ iterator = isSorted;
445
+ isSorted = false;
446
+ }
442
447
  var initial = iterator ? _.map(array, iterator, context) : array;
443
448
  var results = [];
444
449
  var seen = [];
@@ -491,6 +496,7 @@
491
496
  // pairs, or two parallel arrays of the same length -- one of keys, and one of
492
497
  // the corresponding values.
493
498
  _.object = function(list, values) {
499
+ if (list == null) return {};
494
500
  var result = {};
495
501
  for (var i = 0, l = list.length; i < l; i++) {
496
502
  if (values) {
@@ -568,8 +574,8 @@
568
574
  // optionally). Binding with arguments is also known as `curry`.
569
575
  // Delegates to **ECMAScript 5**'s native `Function.bind` if available.
570
576
  // We check for `func.bind` first, to fail fast when `func` is undefined.
571
- _.bind = function bind(func, context) {
572
- var bound, args;
577
+ _.bind = function(func, context) {
578
+ var args, bound;
573
579
  if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
574
580
  if (!_.isFunction(func)) throw new TypeError;
575
581
  args = slice.call(arguments, 2);
@@ -577,6 +583,7 @@
577
583
  if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
578
584
  ctor.prototype = func.prototype;
579
585
  var self = new ctor;
586
+ ctor.prototype = null;
580
587
  var result = func.apply(self, args.concat(slice.call(arguments)));
581
588
  if (Object(result) === result) return result;
582
589
  return self;
@@ -618,25 +625,26 @@
618
625
  // Returns a function, that, when invoked, will only be triggered at most once
619
626
  // during a given window of time.
620
627
  _.throttle = function(func, wait) {
621
- var context, args, timeout, throttling, more, result;
622
- var whenDone = _.debounce(function(){ more = throttling = false; }, wait);
628
+ var context, args, timeout, result;
629
+ var previous = 0;
630
+ var later = function() {
631
+ previous = new Date;
632
+ timeout = null;
633
+ result = func.apply(context, args);
634
+ };
623
635
  return function() {
624
- context = this; args = arguments;
625
- var later = function() {
636
+ var now = new Date;
637
+ var remaining = wait - (now - previous);
638
+ context = this;
639
+ args = arguments;
640
+ if (remaining <= 0) {
641
+ clearTimeout(timeout);
626
642
  timeout = null;
627
- if (more) {
628
- result = func.apply(context, args);
629
- }
630
- whenDone();
631
- };
632
- if (!timeout) timeout = setTimeout(later, wait);
633
- if (throttling) {
634
- more = true;
635
- } else {
636
- throttling = true;
643
+ previous = now;
637
644
  result = func.apply(context, args);
645
+ } else if (!timeout) {
646
+ timeout = setTimeout(later, remaining);
638
647
  }
639
- whenDone();
640
648
  return result;
641
649
  };
642
650
  };
@@ -754,8 +762,10 @@
754
762
  // Extend a given object with all the properties in passed-in object(s).
755
763
  _.extend = function(obj) {
756
764
  each(slice.call(arguments, 1), function(source) {
757
- for (var prop in source) {
758
- obj[prop] = source[prop];
765
+ if (source) {
766
+ for (var prop in source) {
767
+ obj[prop] = source[prop];
768
+ }
759
769
  }
760
770
  });
761
771
  return obj;
@@ -784,8 +794,10 @@
784
794
  // Fill in a given object with default properties.
785
795
  _.defaults = function(obj) {
786
796
  each(slice.call(arguments, 1), function(source) {
787
- for (var prop in source) {
788
- if (obj[prop] == null) obj[prop] = source[prop];
797
+ if (source) {
798
+ for (var prop in source) {
799
+ if (obj[prop] == null) obj[prop] = source[prop];
800
+ }
789
801
  }
790
802
  });
791
803
  return obj;
@@ -950,7 +962,7 @@
950
962
 
951
963
  // Is a given object a finite number?
952
964
  _.isFinite = function(obj) {
953
- return _.isNumber(obj) && isFinite(obj);
965
+ return isFinite(obj) && !isNaN(parseFloat(obj));
954
966
  };
955
967
 
956
968
  // Is the given value `NaN`? (NaN is the only number which does not equal itself).
@@ -996,7 +1008,9 @@
996
1008
 
997
1009
  // Run a function **n** times.
998
1010
  _.times = function(n, iterator, context) {
999
- for (var i = 0; i < n; i++) iterator.call(context, i);
1011
+ var accum = Array(n);
1012
+ for (var i = 0; i < n; i++) accum[i] = iterator.call(context, i);
1013
+ return accum;
1000
1014
  };
1001
1015
 
1002
1016
  // Return a random integer between min and max (inclusive).
@@ -1061,7 +1075,7 @@
1061
1075
  // Useful for temporary DOM ids.
1062
1076
  var idCounter = 0;
1063
1077
  _.uniqueId = function(prefix) {
1064
- var id = idCounter++;
1078
+ var id = '' + ++idCounter;
1065
1079
  return prefix ? prefix + id : id;
1066
1080
  };
1067
1081
 
@@ -1111,11 +1125,18 @@
1111
1125
  text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
1112
1126
  source += text.slice(index, offset)
1113
1127
  .replace(escaper, function(match) { return '\\' + escapes[match]; });
1114
- source +=
1115
- escape ? "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'" :
1116
- interpolate ? "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'" :
1117
- evaluate ? "';\n" + evaluate + "\n__p+='" : '';
1128
+
1129
+ if (escape) {
1130
+ source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
1131
+ }
1132
+ if (interpolate) {
1133
+ source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
1134
+ }
1135
+ if (evaluate) {
1136
+ source += "';\n" + evaluate + "\n__p+='";
1137
+ }
1118
1138
  index = offset + match.length;
1139
+ return match;
1119
1140
  });
1120
1141
  source += "';\n";
1121
1142
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: underscore-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2.1
4
+ version: 1.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-06 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: