Permalink
Browse files

version 0.3.2, with 'identity', and Rhino support

  • Loading branch information...
1 parent 4d09a85 commit d2d1285e26a206278ae9f711a589b5e49f54c60e @jashkenas jashkenas committed Oct 29, 2009
Showing with 43 additions and 20 deletions.
  1. +25 −4 index.html
  2. +1 −1 underscore-min.js
  3. +17 −15 underscore.js
View
@@ -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 @@
<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 />
@@ -737,6 +751,13 @@
<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
argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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) {

0 comments on commit d2d1285

Please sign in to comment.