version 0.3.2, with 'identity', and Rhino support

This commit is contained in:
Jeremy Ashkenas
2009-10-29 14:45:56 -04:00
parent 4d09a85bae
commit d2d1285e26
3 changed files with 43 additions and 20 deletions

View File

@@ -81,7 +81,7 @@
</p>
<p>
Underscore provides 44-odd functions that support both the usual
Underscore provides 45-odd functions that support both the usual
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
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.3.1)</a></td>
<td><a href="underscore.js">Development Version (0.3.2)</a></td>
<td><i>16kb, Uncompressed with Comments</i></td>
</tr>
<tr>
<td><a href="underscore-min.js">Production Version (0.3.1)</a></td>
<td><a href="underscore-min.js">Production Version (0.3.2)</a></td>
<td><i>4kb, Packed and Gzipped</i></td>
</tr>
</table>
@@ -158,7 +158,8 @@
<b>Utility</b>
<br />
<span class="methods"><a href="#noConflict">noConflict</a>,
<a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
<a href="#identity">identity</a>, <a href="#uniqueId">uniqueId</a>,
<a href="#template">template</a></span>
</p>
<div id="documentation">
@@ -700,6 +701,19 @@ _.isUndefined(window.missingVariable);
<pre>
var underscore = _.noConflict();</pre>
<p id="identity">
<b class="header">identity</b><code>_.identity(value)</code>
<br />
Returns the same value that is used as the argument. In math:
<tt>f(x) = x</tt><br />
This function looks useless, but is used throughout Underscore as
a default iterator.
</p>
<pre>
var moe = {name : 'moe'};
moe === _.identity(moe);
=> true</pre>
<p id="uniqueId">
<b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
<br />
@@ -736,6 +750,13 @@ _.template(list, {people : ['moe', 'curly', 'larry']});
<h2>Change Log</h2>
<p>
<b class="header">0.3.2</b><br />
Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
interpreters with: <tt>load("underscore.js")</tt>.
Added <a href="#identity"><tt>identity</tt></a> as a utility function.
</p>
<p>
<b class="header">0.3.1</b><br />
All iterators are now passed in the original collection as their third

2
underscore-min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -10,23 +10,20 @@
/*------------------------- Baseline setup ---------------------------------*/
// Are we running in CommonJS or in the browser?
var commonJS = (typeof window === 'undefined' && typeof exports !== 'undefined');
// Establish the root object, "window" in the browser, or "global" on the server.
var root = this;
// Save the previous value of the "_" variable.
var previousUnderscore = commonJS ? null : window._;
// Keep the identity function around for default iterators.
var identity = function(value) { return value; };
var previousUnderscore = root._;
// Create a safe reference to the Underscore object for the functions below.
var _ = {};
var _ = root._ = {};
// Export the Underscore object for CommonJS, assign it globally otherwise.
commonJS ? _ = exports : window._ = _;
// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') _ = exports;
// Current version.
_.VERSION = '0.3.1';
_.VERSION = '0.3.2';
/*------------------------ Collection Functions: ---------------------------*/
@@ -107,7 +104,7 @@
// Determine whether all of the elements match a truth test. Delegate to
// JavaScript 1.6's every(), if it is present.
_.all = function(obj, iterator, context) {
iterator = iterator || identity;
iterator = iterator || _.identity;
if (obj.every) return obj.every(iterator, context);
var result = true;
_.each(obj, function(value, index, list) {
@@ -119,7 +116,7 @@
// Determine if at least one element in the object matches a truth test. Use
// JavaScript 1.6's some(), if it exists.
_.any = function(obj, iterator, context) {
iterator = iterator || identity;
iterator = iterator || _.identity;
if (obj.some) return obj.some(iterator, context);
var result = false;
_.each(obj, function(value, index, list) {
@@ -192,7 +189,7 @@
// Use a comparator function to figure out at what index an object should
// be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator) {
iterator = iterator || identity;
iterator = iterator || _.identity;
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >> 1;
@@ -360,7 +357,7 @@
// Retrieve the values of an object's properties.
_.values = function(obj) {
return _.map(obj, identity);
return _.map(obj, _.identity);
};
// Extend a given object with all of the properties in a source object.
@@ -421,10 +418,15 @@
// Run Underscore.js in noConflict mode, returning the '_' variable to its
// previous owner. Returns a reference to the Underscore object.
_.noConflict = function() {
if (!commonJS) window._ = previousUnderscore;
root._ = previousUnderscore;
return this;
};
// Keep the identity function around for default iterators.
_.identity = function(value) {
return value;
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
_.uniqueId = function(prefix) {