mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
0.4.1 is out, with array methods proxied for wrapped objects, an _.breakLoop(), and an _.isEmpty()
This commit is contained in:
32
index.html
32
index.html
@@ -81,7 +81,7 @@
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Underscore provides 45-odd functions that support both the usual
|
||||
Underscore provides 50-odd functions that support both the usual
|
||||
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> —
|
||||
as well as more specialized helpers: function binding, javascript
|
||||
templating, deep equality testing, and so on. It delegates to built-in
|
||||
@@ -107,11 +107,11 @@
|
||||
<p>
|
||||
<table>
|
||||
<tr>
|
||||
<td><a href="underscore.js">Development Version (0.4.0)</a></td>
|
||||
<td><a href="underscore.js">Development Version (0.4.1)</a></td>
|
||||
<td><i>18kb, Uncompressed with Comments</i></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="underscore-min.js">Production Version (0.4.0)</a></td>
|
||||
<td><a href="underscore-min.js">Production Version (0.4.1)</a></td>
|
||||
<td><i>2kb, Packed and Gzipped</i></td>
|
||||
</tr>
|
||||
</table>
|
||||
@@ -156,6 +156,14 @@ _(lyrics).chain()
|
||||
|
||||
=> {lumberjack : 2, all : 4, night : 2 ... }</pre>
|
||||
|
||||
<p>
|
||||
In addition, the
|
||||
<a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array prototype's methods</a>
|
||||
are proxied through the chained Underscore object, so you can slip a
|
||||
<tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
|
||||
modify the array.
|
||||
</p>
|
||||
|
||||
<h2>Table of Contents</h2>
|
||||
|
||||
<p>
|
||||
@@ -860,8 +868,13 @@ _.template(list, {people : ['moe', 'curly', 'larry']});
|
||||
<a href="#styles">A more realistic example.</a>)
|
||||
</p>
|
||||
<pre>
|
||||
_({moe : false, curly : true}).chain().values().any().isEqual(true).get();
|
||||
=> true
|
||||
var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
|
||||
var youngest = _(stooges).chain()
|
||||
.sortBy(function(stooge){ return stooge.age; })
|
||||
.map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
|
||||
.first()
|
||||
.get();
|
||||
=> "moe is 21"
|
||||
</pre>
|
||||
|
||||
<p id="get">
|
||||
@@ -875,6 +888,15 @@ _([1, 2, 3]).get();
|
||||
</pre>
|
||||
|
||||
<h2>Change Log</h2>
|
||||
|
||||
<p>
|
||||
<b class="header">0.4.1</b><br />
|
||||
Chained Underscore objects now support the Array prototype methods, so
|
||||
that you can perform the full range of operations on a wrapped array
|
||||
without having to break your chain. Added a <tt>breakLoop</tt> method
|
||||
to <b>break</b> in the middle of any Underscore iteration. Added an
|
||||
<tt>isEmpty</tt> function that works on arrays and objects.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header">0.4.0</b><br />
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
$(document).ready(function() {
|
||||
|
||||
|
||||
module("Underscore chaining.");
|
||||
|
||||
|
||||
test("chaining: map/flatten/reduce", function() {
|
||||
var lyrics = [
|
||||
"I'm a lumberjack and I'm okay",
|
||||
@@ -12,14 +12,14 @@ $(document).ready(function() {
|
||||
var counts = _(lyrics).chain()
|
||||
.map(function(line) { return line.split(''); })
|
||||
.flatten()
|
||||
.reduce({}, function(hash, l) {
|
||||
.reduce({}, function(hash, l) {
|
||||
hash[l] = hash[l] || 0;
|
||||
hash[l]++;
|
||||
return hash;
|
||||
}).get();
|
||||
ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
|
||||
});
|
||||
|
||||
|
||||
test("chaining: select/reject/sortBy", function() {
|
||||
var numbers = [1,2,3,4,5,6,7,8,9,10];
|
||||
numbers = _(numbers).chain().select(function(n) {
|
||||
@@ -31,5 +31,17 @@ $(document).ready(function() {
|
||||
}).get();
|
||||
equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
||||
});
|
||||
|
||||
|
||||
test("chaining: reverse/concat/unshift/pop/map", function() {
|
||||
var numbers = [1,2,3,4,5];
|
||||
numbers = _(numbers).chain()
|
||||
.reverse()
|
||||
.concat([5, 5, 5])
|
||||
.unshift(17)
|
||||
.pop()
|
||||
.map(function(n){ return n * 2; })
|
||||
.get();
|
||||
equals(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
2
underscore-min.js
vendored
2
underscore-min.js
vendored
File diff suppressed because one or more lines are too long
@@ -28,7 +28,7 @@
|
||||
if (typeof exports !== 'undefined') _ = exports;
|
||||
|
||||
// Current version.
|
||||
_.VERSION = '0.4.0';
|
||||
_.VERSION = '0.4.1';
|
||||
|
||||
/*------------------------ Collection Functions: ---------------------------*/
|
||||
|
||||
@@ -496,12 +496,31 @@
|
||||
|
||||
/*------------------------ Setup the OOP Wrapper: --------------------------*/
|
||||
|
||||
// Helper function to continue chaining intermediate results.
|
||||
var result = function(obj, chain) {
|
||||
return chain ? _(obj).chain() : obj;
|
||||
};
|
||||
|
||||
// Add all of the Underscore functions to the wrapper object.
|
||||
_.each(_.functions(), function(name) {
|
||||
wrapper.prototype[name] = function() {
|
||||
Array.prototype.unshift.call(arguments, this._wrapped);
|
||||
var result = _[name].apply(_, arguments);
|
||||
return this._chain ? _(result).chain() : result;
|
||||
return result(_[name].apply(_, arguments), this._chain);
|
||||
};
|
||||
});
|
||||
|
||||
// Add all mutator Array functions to the wrapper.
|
||||
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
||||
wrapper.prototype[name] = function() {
|
||||
Array.prototype[name].apply(this._wrapped, arguments);
|
||||
return result(this._wrapped, this._chain);
|
||||
};
|
||||
});
|
||||
|
||||
// Add all accessor Array functions to the wrapper.
|
||||
_.each(['concat', 'join', 'slice'], function(name) {
|
||||
wrapper.prototype[name] = function() {
|
||||
return result(Array.prototype[name].apply(this._wrapped, arguments), this._chain);
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user