Underscore.js 1.3.1
(c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
Underscore is freely distributable under the MIT license.
Portions of Underscore are inspired or borrowed from Prototype,
@@ -31,7 +31,7 @@ for Closure Compiler "advanced" mode.
The cornerstone, an each implementation, aka forEach.
Handles objects with the built-in forEach, arrays, and raw objects.
Delegates to ECMAScript 5's native forEach if available.
vareach=_.each=_.forEach=function(obj,iterator,context){if(obj==null)return;
@@ -43,13 +43,13 @@ Delegates to ECMAScript 5's native forEach if avai
}}else{for(varkeyinobj){
- if(hasOwnProperty.call(obj,key)){
+ if(_.has(obj,key)){if(iterator.call(context,obj[key],key,obj)===breaker)return;}}}};
Return the results of applying the iterator to each element.
-Delegates to ECMAScript 5's native map if available.
_.map=function(obj,iterator,context){
+Delegates to ECMAScript 5's native map if available.
_.map=_.collect=function(obj,iterator,context){varresults=[];if(obj==null)returnresults;if(nativeMap&&obj.map===nativeMap)returnobj.map(iterator,context);
@@ -349,7 +349,7 @@ all callbacks defined on an object belong to it.
When customizing templateSettings, if you don't want to define an
interpolation, evaluation or escaping regex, we need one that is
-guaranteed not to match.
JavaScript micro-templating, similar to John Resig's implementation.
Underscore templating handles arbitrary delimiters, preserves whitespace,
and correctly escapes quotes within interpolated code.
_.template=function(str,data){varc=_.templateSettings;
@@ -571,15 +576,13 @@ and correctly escapes quotes within interpolated code.
If Underscore is called as a function, it returns a wrapped object that
can be used OO-style. This wrapper holds altered versions of all the
-underscore functions. Wrapped objects may be chained.
@@ -350,6 +351,7 @@ _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
map_.map(list, iterator, [context])
+ Alias: collect
Produces a new array of values by mapping each value in list
through a transformation function (iterator). If the native map method
@@ -521,8 +523,8 @@ _.min(numbers);
sortBy_.sortBy(list, iterator, [context])
- Returns a sorted copy of list, ranked by the results of running
- each value through iterator.
+ Returns a sorted copy of list, ranked in ascending order by the
+ results of running each value through iterator.
- flatten_.flatten(array)
+ flatten_.flatten(array, [shallow])
- Flattens a nested array (the nesting can be to any depth).
+ Flattens a nested array (the nesting can be to any depth). If you
+ pass shallow, the array will only be flattened a single level.
+ has_.has(object, key)
+
+ Does the object contain the given key? Identical to
+ object.hasOwnProperty(key), but uses a safe reference to the
+ hasOwnProperty function, in case it's been
+ overridden accidentally.
+
+
+_.has({a: 1, b: 2, c: 3}, "b");
+=> true
@@ -1465,6 +1484,25 @@ _([1, 2, 3]).value();
Change Log
+
+ 1.3.1 — Jan. 23, 2012
+
+
+ Added an _.has function, as a safer way to use hasOwnProperty.
+
+
+ Added _.collect as an alias for _.map. Smalltalkers, rejoice.
+
+
+ Reverted an old change so that _.extend will correctly copy
+ over keys with undefined values again.
+
+
+ Bugfix to stop escaping slashes within interpolations in _.template.
+
+
+
+
1.3.0 — Jan. 11, 2012
diff --git a/package.json b/package.json
index 0749177b4..4a2e3ca72 100644
--- a/package.json
+++ b/package.json
@@ -4,8 +4,7 @@
"homepage" : "http://documentcloud.github.com/underscore/",
"keywords" : ["util", "functional", "server", "client", "browser"],
"author" : "Jeremy Ashkenas ",
- "contributors" : [],
"repository" : {"type": "git", "url": "git://github.com/documentcloud/underscore.git"},
"main" : "underscore.js",
- "version" : "1.3.0"
+ "version" : "1.3.1"
}
diff --git a/test/collections.js b/test/collections.js
index 46bcdf2af..cff9763ce 100644
--- a/test/collections.js
+++ b/test/collections.js
@@ -35,6 +35,9 @@ $(document).ready(function() {
var doubled = _.map([1, 2, 3], function(num){ return num * 2; });
equals(doubled.join(', '), '2, 4, 6', 'doubled numbers');
+ doubled = _.collect([1, 2, 3], function(num){ return num * 2; });
+ equals(doubled.join(', '), '2, 4, 6', 'aliased as "collect"');
+
var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
diff --git a/test/objects.js b/test/objects.js
index 6d432033a..0105d608a 100644
--- a/test/objects.js
+++ b/test/objects.js
@@ -38,7 +38,7 @@ $(document).ready(function() {
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
result = _.extend({}, {a: void 0, b: null});
- equals(_.keys(result).join(''), 'b', 'extend does not copy undefined values');
+ equals(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
});
test("objects: defaults", function() {
diff --git a/test/utility.js b/test/utility.js
index 30ba3a940..7bc5cb44b 100644
--- a/test/utility.js
+++ b/test/utility.js
@@ -57,6 +57,9 @@ $(document).ready(function() {
var backslashTemplate = _.template("<%= thing %> is \\ridanculous");
equals(backslashTemplate({thing: 'This'}), "This is \\ridanculous");
+ var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>');
+ equals(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');
+
var fancyTemplate = _.template("