merged in kriskowal's CommonJS branch and Dmitry Baranovskiy's optimizations

This commit is contained in:
Jeremy Ashkenas
2009-10-28 23:21:24 -04:00
parent 8c15ae153a
commit 4f783846de
5 changed files with 35 additions and 26 deletions

4
README
View File

@@ -8,10 +8,10 @@
\ \____/
\/___/
Underscore is a utility-belt library for Javascript that provides
Underscore is a utility-belt library for JavaScript that provides
a lot of the functional programming support that you would expect
in Prototype.js (or Ruby), but without extending any of the built-
in Javascript objects. It's the tie to go along with jQuery's tux.
in JavaScript objects. It's the tie to go along with jQuery's tux.
For Docs, License, Tests, and pre-packed downloads, see:
http://documentcloud.github.com/underscore/

View File

@@ -72,11 +72,11 @@
<p>
<a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
utility-belt library for Javascript that provides a lot of the
utility-belt library for JavaScript that provides a lot of the
functional programming support that you would expect in
<a href="http://prototypejs.org/api">Prototype.js</a>
(or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
but without extending any of the built-in Javascript objects. It's the
but without extending any of the built-in JavaScript objects. It's the
tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
</p>
@@ -86,7 +86,7 @@
as well as more specialized helpers: function binding, javascript
templating, deep equality testing, and so on. It delegates to built-in
functions, if present, so
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">Javascript 1.6</a>
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">JavaScript 1.6</a>
compliant browsers will use the
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
<b>every</b>, <b>some</b> and <b>indexOf</b>.
@@ -171,7 +171,7 @@
<br />
Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
passed. If <b>list</b> is a Javascript object, a pair with <b>key</b>
passed. If <b>list</b> is a JavaScript object, a pair with <b>key</b>
and <b>value</b> properties will be yielded. If the list has an <b>each</b>
method of its own, it will be used instead. Delegates to the native
<b>forEach</b> function if it exists.
@@ -403,7 +403,7 @@ _.last([3, 2, 1]);
<b class="header">compact</b><code>_.compact(array)</code>
<br />
Returns a copy of the <b>array</b> with all falsy values removed.
In Javascript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
<i>undefined</i> and <i>NaN</i> are all falsy.
</p>
<pre>
@@ -713,10 +713,10 @@ _.uniqueId('contact_');
<p id="template">
<b class="header">template</b><code>_.template(templateString, [context])</code>
<br />
Compiles Javascript templates into functions that can be evaluated
Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON
data sources. Template functions can both interpolate variables, using<br />
<i>&lt;%= &hellip; %&gt;</i>, as well as execute arbitrary Javascript code, with
<i>&lt;%= &hellip; %&gt;</i>, as well as execute arbitrary JavaScript code, with
<i>&lt;% &hellip; %&gt;</i>. When you evaluate a template function, pass in a
<b>context</b> object that has properties corresponding to the template's free
variables. If you're writing a one-off, you can pass the <b>context</b>

View File

@@ -1,7 +1,6 @@
{
"!": "This is a Narwhal package descriptor.",
"name" : "underscore",
"description": "Functional programming aid for Javascript. Works well with jQuery.",
"description" : "Functional programming aid for JavaScript. Works well with jQuery.",
"url" : "http://documentcloud.github.com/underscore/",
"keywords" : ["util", "functional", "server", "client", "browser"],
"author" : "Jeremy Ashkenas <jeremy@documentcloud.org>",

View File

@@ -18,6 +18,13 @@ $(document).ready(function() {
answers = [];
_.forEach([1, 2, 3], function(num){ answers.push(num); });
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
answers = [];
var obj = {one : 1, two : 2, three : 3};
obj.constructor.prototype.four = 4;
_.each(obj, function(pair){ answers.push(pair.key); });
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
delete obj.constructor.prototype.four;
});
test('collections: map', function() {

View File

@@ -12,6 +12,8 @@
var previousUnderscore = root._;
var identity = function(value) { return value; };
var _ = root._ = {};
_.VERSION = '0.2.0';
@@ -26,7 +28,7 @@
if (obj.forEach) {
obj.forEach(iterator, context);
} else if (obj.length) {
for (var i=0, ii = obj.length; i<ii; i++) iterator.call(context, obj[i], i);
for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i);
} else if (obj.each) {
obj.each(function(value) { iterator.call(context, value, index++); });
} else {
@@ -99,10 +101,11 @@
// 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;
if (obj.every) return obj.every(iterator, context);
var result = true;
_.each(obj, function(value, index) {
if (!(result = result && iterator ? iterator.call(context, value, index) : value)) throw '__break__';
if (!(result = result && iterator.call(context, value, index))) throw '__break__';
});
return result;
};
@@ -110,11 +113,11 @@
// 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 || function(v) { return v; };
iterator = iterator || identity;
if (obj.some) return obj.some(iterator, context);
var result = false;
_.each(obj, function(value, index) {
if (result = iterator ? iterator.call(context, value, index) : value) throw '__break__';
if (result = iterator.call(context, value, index)) throw '__break__';
});
return result;
};
@@ -185,7 +188,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 || function(val) { return val; };
iterator = iterator || identity;
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >> 1;
@@ -273,7 +276,7 @@
// item in an array, or -1 if the item is not included in the array.
_.indexOf = function(array, item) {
if (array.indexOf) return array.indexOf(item);
for (i=0, ii=array.length; i<ii; i++) if (array[i] === item) return i;
for (i=0, l=array.length; i<l; i++) if (array[i] === item) return i;
return -1;
};
@@ -453,6 +456,6 @@
/*------------------------- Export for ServerJS ----------------------------*/
if (!_.isUndefined(exports)) exports = _;
if (typeof exports != 'undefined') exports = _;
})();