mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +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>
|
||||||
|
|
||||||
<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> —
|
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> —
|
||||||
as well as more specialized helpers: function binding, javascript
|
as well as more specialized helpers: function binding, javascript
|
||||||
templating, deep equality testing, and so on. It delegates to built-in
|
templating, deep equality testing, and so on. It delegates to built-in
|
||||||
@@ -107,11 +107,11 @@
|
|||||||
<p>
|
<p>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<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>
|
<td><i>18kb, Uncompressed with Comments</i></td>
|
||||||
</tr>
|
</tr>
|
||||||
<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>
|
<td><i>2kb, Packed and Gzipped</i></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
@@ -156,6 +156,14 @@ _(lyrics).chain()
|
|||||||
|
|
||||||
=> {lumberjack : 2, all : 4, night : 2 ... }</pre>
|
=> {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>
|
<h2>Table of Contents</h2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -860,8 +868,13 @@ _.template(list, {people : ['moe', 'curly', 'larry']});
|
|||||||
<a href="#styles">A more realistic example.</a>)
|
<a href="#styles">A more realistic example.</a>)
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
_({moe : false, curly : true}).chain().values().any().isEqual(true).get();
|
var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
|
||||||
=> true
|
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>
|
</pre>
|
||||||
|
|
||||||
<p id="get">
|
<p id="get">
|
||||||
@@ -875,6 +888,15 @@ _([1, 2, 3]).get();
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2>Change Log</h2>
|
<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>
|
<p>
|
||||||
<b class="header">0.4.0</b><br />
|
<b class="header">0.4.0</b><br />
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
|
||||||
module("Underscore chaining.");
|
module("Underscore chaining.");
|
||||||
|
|
||||||
test("chaining: map/flatten/reduce", function() {
|
test("chaining: map/flatten/reduce", function() {
|
||||||
var lyrics = [
|
var lyrics = [
|
||||||
"I'm a lumberjack and I'm okay",
|
"I'm a lumberjack and I'm okay",
|
||||||
@@ -12,14 +12,14 @@ $(document).ready(function() {
|
|||||||
var counts = _(lyrics).chain()
|
var counts = _(lyrics).chain()
|
||||||
.map(function(line) { return line.split(''); })
|
.map(function(line) { return line.split(''); })
|
||||||
.flatten()
|
.flatten()
|
||||||
.reduce({}, function(hash, l) {
|
.reduce({}, function(hash, l) {
|
||||||
hash[l] = hash[l] || 0;
|
hash[l] = hash[l] || 0;
|
||||||
hash[l]++;
|
hash[l]++;
|
||||||
return hash;
|
return hash;
|
||||||
}).get();
|
}).get();
|
||||||
ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
|
ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("chaining: select/reject/sortBy", function() {
|
test("chaining: select/reject/sortBy", function() {
|
||||||
var numbers = [1,2,3,4,5,6,7,8,9,10];
|
var numbers = [1,2,3,4,5,6,7,8,9,10];
|
||||||
numbers = _(numbers).chain().select(function(n) {
|
numbers = _(numbers).chain().select(function(n) {
|
||||||
@@ -31,5 +31,17 @@ $(document).ready(function() {
|
|||||||
}).get();
|
}).get();
|
||||||
equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
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;
|
if (typeof exports !== 'undefined') _ = exports;
|
||||||
|
|
||||||
// Current version.
|
// Current version.
|
||||||
_.VERSION = '0.4.0';
|
_.VERSION = '0.4.1';
|
||||||
|
|
||||||
/*------------------------ Collection Functions: ---------------------------*/
|
/*------------------------ Collection Functions: ---------------------------*/
|
||||||
|
|
||||||
@@ -496,12 +496,31 @@
|
|||||||
|
|
||||||
/*------------------------ Setup the OOP Wrapper: --------------------------*/
|
/*------------------------ 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.
|
// Add all of the Underscore functions to the wrapper object.
|
||||||
_.each(_.functions(), function(name) {
|
_.each(_.functions(), function(name) {
|
||||||
wrapper.prototype[name] = function() {
|
wrapper.prototype[name] = function() {
|
||||||
Array.prototype.unshift.call(arguments, this._wrapped);
|
Array.prototype.unshift.call(arguments, this._wrapped);
|
||||||
var result = _[name].apply(_, arguments);
|
return result(_[name].apply(_, arguments), this._chain);
|
||||||
return this._chain ? _(result).chain() : result;
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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