From 4f783846de50e8de6d3001eb1ae23b9d041ccf23 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 28 Oct 2009 23:21:24 -0400 Subject: [PATCH] merged in kriskowal's CommonJS branch and Dmitry Baranovskiy's optimizations --- README | 4 ++-- index.html | 14 +++++++------- package.json | 19 +++++++++---------- test/collections.js | 7 +++++++ underscore.js | 17 ++++++++++------- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/README b/README index 1bf4faf29..e41015cd7 100644 --- a/README +++ b/README @@ -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/ diff --git a/index.html b/index.html index 11f706290..88c32d94f 100644 --- a/index.html +++ b/index.html @@ -72,11 +72,11 @@

Underscore 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 Prototype.js (or Ruby), - 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 jQuery's tux.

@@ -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 - Javascript 1.6 + JavaScript 1.6 compliant browsers will use the native implementations of forEach, map, filter, every, some and indexOf. @@ -171,7 +171,7 @@
Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is bound to the context object, if one is - passed. If list is a Javascript object, a pair with key + passed. If list is a JavaScript object, a pair with key and value properties will be yielded. If the list has an each method of its own, it will be used instead. Delegates to the native forEach function if it exists. @@ -403,7 +403,7 @@ _.last([3, 2, 1]); compact_.compact(array)
Returns a copy of the array with all falsy values removed. - In Javascript, false, null, 0, "", + In JavaScript, false, null, 0, "", undefined and NaN are all falsy.

@@ -713,10 +713,10 @@ _.uniqueId('contact_');
       

template_.template(templateString, [context])
- 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
- <%= … %>, as well as execute arbitrary Javascript code, with + <%= … %>, as well as execute arbitrary JavaScript code, with <% … %>. When you evaluate a template function, pass in a context object that has properties corresponding to the template's free variables. If you're writing a one-off, you can pass the context diff --git a/package.json b/package.json index 3e7ce81f3..2cbc8128d 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,11 @@ { - "!": "This is a Narwhal package descriptor.", - "name": "underscore", - "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 ", - "maintainer": "Kris Kowal ", - "contributors": [], - "dependencies": [], - "lib": "." + "name" : "underscore", + "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 ", + "maintainer" : "Kris Kowal ", + "contributors" : [], + "dependencies" : [], + "lib" : "." } diff --git a/test/collections.js b/test/collections.js index f97e6e520..8fb684a07 100644 --- a/test/collections.js +++ b/test/collections.js @@ -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() { diff --git a/underscore.js b/underscore.js index c89842dd9..f5b8610c2 100644 --- a/underscore.js +++ b/underscore.js @@ -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> 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