Skip to content

Commit

Permalink
version 0.4.2 -- quick patch to rename get() to value() for clarity, …
Browse files Browse the repository at this point in the history
…and adding jQuery comparisons in the speed tests
  • Loading branch information
jashkenas committed Nov 9, 2009
1 parent 7127d40 commit f6e67a5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
25 changes: 15 additions & 10 deletions index.html
Expand Up @@ -107,11 +107,11 @@ <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and u
<p>
<table>
<tr>
<td><a href="underscore.js">Development Version (0.4.1)</a></td>
<td><a href="underscore.js">Development Version (0.4.2)</a></td>
<td><i>18kb, Uncompressed with Comments</i></td>
</tr>
<tr>
<td><a href="underscore-min.js">Production Version (0.4.1)</a></td>
<td><a href="underscore-min.js">Production Version (0.4.2)</a></td>
<td><i>2kb, Packed and Gzipped</i></td>
</tr>
</table>
Expand All @@ -133,7 +133,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
Using the object-oriented style allows you to chain together methods. Calling
<tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
use <tt>get</tt> to retrieve the final value. Here's an example of chaining
use <tt>value</tt> to retrieve the final value. Here's an example of chaining
together a <b>map/flatten/reduce</b>, in order to get the word count of
every word in a song.
</p>
Expand All @@ -152,7 +152,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
.reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}).get();
}).value();

=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>

Expand Down Expand Up @@ -217,7 +217,7 @@ <h2>Table of Contents</h2>
<p>
<b>Chaining</b>
<br />
<span class="methods"><a href="#chain">chain</a>, <a href="#get">get</a>
<span class="methods"><a href="#chain">chain</a>, <a href="#value">value</a>
</p>

<div id="documentation">
Expand Down Expand Up @@ -864,7 +864,7 @@ <h2>Chaining</h2>
<b class="header">chain</b><code>_(obj).chain()</code>
<br />
Returns a wrapped object. Calling methods on this object will continue
to return wrapped objects until <tt>get</tt> is used. (
to return wrapped objects until <tt>value</tt> is used. (
<a href="#styles">A more realistic example.</a>)
</p>
<pre>
Expand All @@ -873,22 +873,27 @@ <h2>Chaining</h2>
.sortBy(function(stooge){ return stooge.age; })
.map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
.first()
.get();
.value();
=&gt; "moe is 21"
</pre>

<p id="get">
<b class="header">get</b><code>_(obj).get()</code>
<p id="value">
<b class="header">value</b><code>_(obj).value()</code>
<br />
Extracts the value of a wrapped object.
</p>
<pre>
_([1, 2, 3]).get();
_([1, 2, 3]).value();
=&gt; [1, 2, 3]
</pre>

<h2>Change Log</h2>

<p>
<b class="header">0.4.2</b><br />
Renamed the unwrapping function to <tt>value</tt>, for clarity.
</p>

<p>
<b class="header">0.4.1</b><br />
Chained Underscore objects now support the Array prototype methods, so
Expand Down
6 changes: 3 additions & 3 deletions test/chaining.js
Expand Up @@ -16,7 +16,7 @@ $(document).ready(function() {
hash[l] = hash[l] || 0;
hash[l]++;
return hash;
}).get();
}).value();
ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
});

Expand All @@ -28,7 +28,7 @@ $(document).ready(function() {
return n % 4 == 0;
}).sortBy(function(n) {
return -n;
}).get();
}).value();
equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
});

Expand All @@ -40,7 +40,7 @@ $(document).ready(function() {
.unshift(17)
.pop()
.map(function(n){ return n * 2; })
.get();
.value();
equals(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
});

Expand Down
34 changes: 22 additions & 12 deletions test/speed.js
@@ -1,54 +1,64 @@
(function() {

var numbers = [];
for (var i=0; i<1000; i++) numbers.push(i);
var objects = _.map(numbers, function(n){ return {num : n}; });
var randomized = _.sortBy(numbers, function(){ return Math.random(); });

JSLitmus.test('_.each()', function() {
var timesTwo = [];
_.each(numbers, function(num){ timesTwo.push(num * 2); });
return timesTwo;
});

JSLitmus.test('_(list).each()', function() {
var timesTwo = [];
_(numbers).each(function(num){ timesTwo.push(num * 2); });
return timesTwo;
});


JSLitmus.test('jQuery.each()', function() {
var timesTwo = [];
jQuery.each(numbers, function(){ timesTwo.push(this * 2); });
return timesTwo;
});

JSLitmus.test('_.map()', function() {
return _.map(objects, function(obj){ return obj.num; });
});


JSLitmus.test('jQuery.map()', function() {
return jQuery.map(objects, function(obj){ return obj.num; });
});

JSLitmus.test('_.pluck()', function() {
return _.pluck(objects, 'num');
});

JSLitmus.test('_.uniq()', function() {
return _.uniq(randomized);
});

JSLitmus.test('_.uniq() (sorted)', function() {
return _.uniq(numbers, true);
});

JSLitmus.test('_.sortBy()', function() {
return _.sortBy(numbers, function(num){ return -num; });
});

JSLitmus.test('_.isEqual()', function() {
return _.isEqual(numbers, randomized);
});

JSLitmus.test('_.keys()', function() {
return _.keys(objects);
});

JSLitmus.test('_.values()', function() {
return _.values(objects);
});

JSLitmus.test('_.intersect()', function() {
return _.intersect(numbers, randomized);
});
Expand Down
2 changes: 1 addition & 1 deletion underscore-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions underscore.js
Expand Up @@ -28,7 +28,7 @@
if (typeof exports !== 'undefined') _ = exports;

// Current version.
_.VERSION = '0.4.1';
_.VERSION = '0.4.2';

/*------------------------ Collection Functions: ---------------------------*/

Expand Down Expand Up @@ -531,7 +531,7 @@
};

// Extracts the result from a wrapped and chained object.
wrapper.prototype.get = function() {
wrapper.prototype.value = function() {
return this._wrapped;
};

Expand Down

0 comments on commit f6e67a5

Please sign in to comment.