Created
February 3, 2013 05:44
-
-
Save ahomu/4700664 to your computer and use it in GitHub Desktop.
Difference of between 1.4.3 and 1.4.4 (Underscore.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/underscore1.4.3.js b/underscore1.4.4.js | |
index 4d83099..6642c09 100644 | |
--- a/underscore1.4.3.js | |
+++ b/underscore1.4.4.js | |
@@ -1,6 +1,6 @@ | |
-// Underscore.js 1.4.3 | |
+// Underscore.js 1.4.4 | |
// http://underscorejs.org | |
-// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. | |
+// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc. | |
// Underscore may be freely distributed under the MIT license. | |
(function() { | |
@@ -64,7 +64,7 @@ | |
} | |
// Current version. | |
- _.VERSION = '1.4.3'; | |
+ _.VERSION = '1.4.4'; | |
// Collection Functions | |
// -------------------- | |
@@ -224,8 +224,9 @@ | |
// Invoke a method (with arguments) on every item in a collection. | |
_.invoke = function(obj, method) { | |
var args = slice.call(arguments, 2); | |
+ var isFunc = _.isFunction(method); | |
return _.map(obj, function(value) { | |
- return (_.isFunction(method) ? method : value[method]).apply(value, args); | |
+ return (isFunc ? method : value[method]).apply(value, args); | |
}); | |
}; | |
@@ -235,10 +236,10 @@ | |
}; | |
// Convenience version of a common use case of `filter`: selecting only objects | |
- // with specific `key:value` pairs. | |
- _.where = function(obj, attrs) { | |
- if (_.isEmpty(attrs)) return []; | |
- return _.filter(obj, function(value) { | |
+ // containing specific `key:value` pairs. | |
+ _.where = function(obj, attrs, first) { | |
+ if (_.isEmpty(attrs)) return first ? void 0 : []; | |
+ return _[first ? 'find' : 'filter'](obj, function(value) { | |
for (var key in attrs) { | |
if (attrs[key] !== value[key]) return false; | |
} | |
@@ -246,6 +247,12 @@ | |
}); | |
}; | |
+ // Convenience version of a common use case of `find`: getting the first object | |
+ // containing specific `key:value` pairs. | |
+ _.findWhere = function(obj, attrs) { | |
+ return _.where(obj, attrs, true); | |
+ }; | |
+ | |
// Return the maximum element or (element-based computation). | |
// Can't optimize arrays of integers longer than 65,535 elements. | |
// See: https://bugs.webkit.org/show_bug.cgi?id=80797 | |
@@ -567,26 +574,23 @@ | |
// Function (ahem) Functions | |
// ------------------ | |
- // Reusable constructor function for prototype setting. | |
- var ctor = function(){}; | |
- | |
// Create a function bound to a given object (assigning `this`, and arguments, | |
- // optionally). Binding with arguments is also known as `curry`. | |
- // Delegates to **ECMAScript 5**'s native `Function.bind` if available. | |
- // We check for `func.bind` first, to fail fast when `func` is undefined. | |
+ // optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if | |
+ // available. | |
_.bind = function(func, context) { | |
- var args, bound; | |
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1)); | |
- if (!_.isFunction(func)) throw new TypeError; | |
- args = slice.call(arguments, 2); | |
- return bound = function() { | |
- if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments))); | |
- ctor.prototype = func.prototype; | |
- var self = new ctor; | |
- ctor.prototype = null; | |
- var result = func.apply(self, args.concat(slice.call(arguments))); | |
- if (Object(result) === result) return result; | |
- return self; | |
+ var args = slice.call(arguments, 2); | |
+ return function() { | |
+ return func.apply(context, args.concat(slice.call(arguments))); | |
+ }; | |
+ }; | |
+ | |
+ // Partially apply a function by creating a version that has had some of its | |
+ // arguments pre-filled, without changing its dynamic `this` context. | |
+ _.partial = function(func) { | |
+ var args = slice.call(arguments, 1); | |
+ return function() { | |
+ return func.apply(this, args.concat(slice.call(arguments))); | |
}; | |
}; | |
@@ -594,7 +598,7 @@ | |
// all callbacks defined on an object belong to it. | |
_.bindAll = function(obj) { | |
var funcs = slice.call(arguments, 1); | |
- if (funcs.length == 0) funcs = _.functions(obj); | |
+ if (funcs.length === 0) throw new Error("bindAll must be passed function names"); | |
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); }); | |
return obj; | |
}; | |
@@ -1019,7 +1023,7 @@ | |
max = min; | |
min = 0; | |
} | |
- return min + (0 | Math.random() * (max - min + 1)); | |
+ return min + Math.floor(Math.random() * (max - min + 1)); | |
}; | |
// List of HTML entities for escaping. | |
@@ -1054,7 +1058,7 @@ | |
// If the value of the named property is a function then invoke it; | |
// otherwise, return it. | |
_.result = function(object, property) { | |
- if (object == null) return null; | |
+ if (object == null) return void 0; | |
var value = object[property]; | |
return _.isFunction(value) ? value.call(object) : value; | |
}; | |
@@ -1075,7 +1079,7 @@ | |
// Useful for temporary DOM ids. | |
var idCounter = 0; | |
_.uniqueId = function(prefix) { | |
- var id = '' + ++idCounter; | |
+ var id = ++idCounter + ''; | |
return prefix ? prefix + id : id; | |
}; | |
@@ -1110,6 +1114,7 @@ | |
// Underscore templating handles arbitrary delimiters, preserves whitespace, | |
// and correctly escapes quotes within interpolated code. | |
_.template = function(text, data, settings) { | |
+ var render; | |
settings = _.defaults({}, settings, _.templateSettings); | |
// Combine delimiters into one regular expression via alternation. | |
@@ -1148,7 +1153,7 @@ | |
source + "return __p;\n"; | |
try { | |
- var render = new Function(settings.variable || 'obj', '_', source); | |
+ render = new Function(settings.variable || 'obj', '_', source); | |
} catch (e) { | |
e.source = source; | |
throw e; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment