|
1 | | -// Underscore.js 1.4.3 |
| 1 | +// Underscore.js 1.4.4 |
2 | 2 | // http://underscorejs.org |
3 | | -// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. |
| 3 | +// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc. |
4 | 4 | // Underscore may be freely distributed under the MIT license. |
5 | 5 |
|
6 | 6 | (function() { |
|
64 | 64 | } |
65 | 65 |
|
66 | 66 | // Current version. |
67 | | - _.VERSION = '1.4.3'; |
| 67 | + _.VERSION = '1.4.4'; |
68 | 68 |
|
69 | 69 | // Collection Functions |
70 | 70 | // -------------------- |
|
224 | 224 | // Invoke a method (with arguments) on every item in a collection. |
225 | 225 | _.invoke = function(obj, method) { |
226 | 226 | var args = slice.call(arguments, 2); |
| 227 | + var isFunc = _.isFunction(method); |
227 | 228 | return _.map(obj, function(value) { |
228 | | - return (_.isFunction(method) ? method : value[method]).apply(value, args); |
| 229 | + return (isFunc ? method : value[method]).apply(value, args); |
229 | 230 | }); |
230 | 231 | }; |
231 | 232 |
|
|
235 | 236 | }; |
236 | 237 |
|
237 | 238 | // Convenience version of a common use case of `filter`: selecting only objects |
238 | | - // with specific `key:value` pairs. |
239 | | - _.where = function(obj, attrs) { |
240 | | - if (_.isEmpty(attrs)) return []; |
241 | | - return _.filter(obj, function(value) { |
| 239 | + // containing specific `key:value` pairs. |
| 240 | + _.where = function(obj, attrs, first) { |
| 241 | + if (_.isEmpty(attrs)) return first ? null : []; |
| 242 | + return _[first ? 'find' : 'filter'](obj, function(value) { |
242 | 243 | for (var key in attrs) { |
243 | 244 | if (attrs[key] !== value[key]) return false; |
244 | 245 | } |
245 | 246 | return true; |
246 | 247 | }); |
247 | 248 | }; |
248 | 249 |
|
| 250 | + // Convenience version of a common use case of `find`: getting the first object |
| 251 | + // containing specific `key:value` pairs. |
| 252 | + _.findWhere = function(obj, attrs) { |
| 253 | + return _.where(obj, attrs, true); |
| 254 | + }; |
| 255 | + |
249 | 256 | // Return the maximum element or (element-based computation). |
250 | 257 | // Can't optimize arrays of integers longer than 65,535 elements. |
251 | 258 | // See: https://bugs.webkit.org/show_bug.cgi?id=80797 |
|
567 | 574 | // Function (ahem) Functions |
568 | 575 | // ------------------ |
569 | 576 |
|
570 | | - // Reusable constructor function for prototype setting. |
571 | | - var ctor = function(){}; |
572 | | - |
573 | 577 | // Create a function bound to a given object (assigning `this`, and arguments, |
574 | | - // optionally). Binding with arguments is also known as `curry`. |
575 | | - // Delegates to **ECMAScript 5**'s native `Function.bind` if available. |
576 | | - // We check for `func.bind` first, to fail fast when `func` is undefined. |
| 578 | + // optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if |
| 579 | + // available. |
577 | 580 | _.bind = function(func, context) { |
578 | | - var args, bound; |
579 | 581 | if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1)); |
580 | | - if (!_.isFunction(func)) throw new TypeError; |
581 | | - args = slice.call(arguments, 2); |
582 | | - return bound = function() { |
583 | | - if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments))); |
584 | | - ctor.prototype = func.prototype; |
585 | | - var self = new ctor; |
586 | | - ctor.prototype = null; |
587 | | - var result = func.apply(self, args.concat(slice.call(arguments))); |
588 | | - if (Object(result) === result) return result; |
589 | | - return self; |
| 582 | + var args = slice.call(arguments, 2); |
| 583 | + return function() { |
| 584 | + return func.apply(context, args.concat(slice.call(arguments))); |
| 585 | + }; |
| 586 | + }; |
| 587 | + |
| 588 | + // Partially apply a function by creating a version that has had some of its |
| 589 | + // arguments pre-filled, without changing its dynamic `this` context. |
| 590 | + _.partial = function(func) { |
| 591 | + var args = slice.call(arguments, 1); |
| 592 | + return function() { |
| 593 | + return func.apply(this, args.concat(slice.call(arguments))); |
590 | 594 | }; |
591 | 595 | }; |
592 | 596 |
|
593 | 597 | // Bind all of an object's methods to that object. Useful for ensuring that |
594 | 598 | // all callbacks defined on an object belong to it. |
595 | 599 | _.bindAll = function(obj) { |
596 | 600 | var funcs = slice.call(arguments, 1); |
597 | | - if (funcs.length == 0) funcs = _.functions(obj); |
| 601 | + if (funcs.length === 0) funcs = _.functions(obj); |
598 | 602 | each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); }); |
599 | 603 | return obj; |
600 | 604 | }; |
|
1019 | 1023 | max = min; |
1020 | 1024 | min = 0; |
1021 | 1025 | } |
1022 | | - return min + (0 | Math.random() * (max - min + 1)); |
| 1026 | + return min + Math.floor(Math.random() * (max - min + 1)); |
1023 | 1027 | }; |
1024 | 1028 |
|
1025 | 1029 | // List of HTML entities for escaping. |
|
1075 | 1079 | // Useful for temporary DOM ids. |
1076 | 1080 | var idCounter = 0; |
1077 | 1081 | _.uniqueId = function(prefix) { |
1078 | | - var id = '' + ++idCounter; |
| 1082 | + var id = ++idCounter + ''; |
1079 | 1083 | return prefix ? prefix + id : id; |
1080 | 1084 | }; |
1081 | 1085 |
|
|
1110 | 1114 | // Underscore templating handles arbitrary delimiters, preserves whitespace, |
1111 | 1115 | // and correctly escapes quotes within interpolated code. |
1112 | 1116 | _.template = function(text, data, settings) { |
| 1117 | + var render; |
1113 | 1118 | settings = _.defaults({}, settings, _.templateSettings); |
1114 | 1119 |
|
1115 | 1120 | // Combine delimiters into one regular expression via alternation. |
|
1148 | 1153 | source + "return __p;\n"; |
1149 | 1154 |
|
1150 | 1155 | try { |
1151 | | - var render = new Function(settings.variable || 'obj', '_', source); |
| 1156 | + render = new Function(settings.variable || 'obj', '_', source); |
1152 | 1157 | } catch (e) { |
1153 | 1158 | e.source = source; |
1154 | 1159 | throw e; |
|
0 commit comments