mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 03:47:50 +00:00
merged in kriskowal's CommonJS branch and Dmitry Baranovskiy's optimizations
This commit is contained in:
4
README
4
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
|
a lot of the functional programming support that you would expect
|
||||||
in Prototype.js (or Ruby), but without extending any of the built-
|
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:
|
For Docs, License, Tests, and pre-packed downloads, see:
|
||||||
http://documentcloud.github.com/underscore/
|
http://documentcloud.github.com/underscore/
|
||||||
|
|||||||
14
index.html
14
index.html
@@ -72,11 +72,11 @@
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
<a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
|
<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
|
functional programming support that you would expect in
|
||||||
<a href="http://prototypejs.org/api">Prototype.js</a>
|
<a href="http://prototypejs.org/api">Prototype.js</a>
|
||||||
(or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</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.
|
tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@
|
|||||||
as well as more specialized helpers: function binding, javascript
|
as well as more specialized helpers: function binding, javascript
|
||||||
templating, deep equality testing, and so on. It delegates to built-in
|
templating, deep equality testing, and so on. It delegates to built-in
|
||||||
functions, if present, so
|
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
|
compliant browsers will use the
|
||||||
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
|
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
|
||||||
<b>every</b>, <b>some</b> and <b>indexOf</b>.
|
<b>every</b>, <b>some</b> and <b>indexOf</b>.
|
||||||
@@ -171,7 +171,7 @@
|
|||||||
<br />
|
<br />
|
||||||
Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
|
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
|
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>
|
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
|
method of its own, it will be used instead. Delegates to the native
|
||||||
<b>forEach</b> function if it exists.
|
<b>forEach</b> function if it exists.
|
||||||
@@ -403,7 +403,7 @@ _.last([3, 2, 1]);
|
|||||||
<b class="header">compact</b><code>_.compact(array)</code>
|
<b class="header">compact</b><code>_.compact(array)</code>
|
||||||
<br />
|
<br />
|
||||||
Returns a copy of the <b>array</b> with all falsy values removed.
|
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.
|
<i>undefined</i> and <i>NaN</i> are all falsy.
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
@@ -713,10 +713,10 @@ _.uniqueId('contact_');
|
|||||||
<p id="template">
|
<p id="template">
|
||||||
<b class="header">template</b><code>_.template(templateString, [context])</code>
|
<b class="header">template</b><code>_.template(templateString, [context])</code>
|
||||||
<br />
|
<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
|
for rendering. Useful for rendering complicated bits of HTML from JSON
|
||||||
data sources. Template functions can both interpolate variables, using<br />
|
data sources. Template functions can both interpolate variables, using<br />
|
||||||
<i><%= … %></i>, as well as execute arbitrary Javascript code, with
|
<i><%= … %></i>, as well as execute arbitrary JavaScript code, with
|
||||||
<i><% … %></i>. When you evaluate a template function, pass in a
|
<i><% … %></i>. When you evaluate a template function, pass in a
|
||||||
<b>context</b> object that has properties corresponding to the template's free
|
<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>
|
variables. If you're writing a one-off, you can pass the <b>context</b>
|
||||||
|
|||||||
19
package.json
19
package.json
@@ -1,12 +1,11 @@
|
|||||||
{
|
{
|
||||||
"!": "This is a Narwhal package descriptor.",
|
"name" : "underscore",
|
||||||
"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/",
|
||||||
"url": "http://documentcloud.github.com/underscore/",
|
"keywords" : ["util", "functional", "server", "client", "browser"],
|
||||||
"keywords": ["util", "functional", "server", "client", "browser"],
|
"author" : "Jeremy Ashkenas <jeremy@documentcloud.org>",
|
||||||
"author": "Jeremy Ashkenas <jeremy@documentcloud.org>",
|
"maintainer" : "Kris Kowal <kris.kowal@cixar.com>",
|
||||||
"maintainer": "Kris Kowal <kris.kowal@cixar.com>",
|
"contributors" : [],
|
||||||
"contributors": [],
|
"dependencies" : [],
|
||||||
"dependencies": [],
|
"lib" : "."
|
||||||
"lib": "."
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ $(document).ready(function() {
|
|||||||
answers = [];
|
answers = [];
|
||||||
_.forEach([1, 2, 3], function(num){ answers.push(num); });
|
_.forEach([1, 2, 3], function(num){ answers.push(num); });
|
||||||
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
|
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() {
|
test('collections: map', function() {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
var previousUnderscore = root._;
|
var previousUnderscore = root._;
|
||||||
|
|
||||||
|
var identity = function(value) { return value; };
|
||||||
|
|
||||||
var _ = root._ = {};
|
var _ = root._ = {};
|
||||||
|
|
||||||
_.VERSION = '0.2.0';
|
_.VERSION = '0.2.0';
|
||||||
@@ -26,7 +28,7 @@
|
|||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (obj.length) {
|
} 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) {
|
} else if (obj.each) {
|
||||||
obj.each(function(value) { iterator.call(context, value, index++); });
|
obj.each(function(value) { iterator.call(context, value, index++); });
|
||||||
} else {
|
} else {
|
||||||
@@ -99,10 +101,11 @@
|
|||||||
// Determine whether all of the elements match a truth test. Delegate to
|
// Determine whether all of the elements match a truth test. Delegate to
|
||||||
// JavaScript 1.6's every(), if it is present.
|
// JavaScript 1.6's every(), if it is present.
|
||||||
_.all = function(obj, iterator, context) {
|
_.all = function(obj, iterator, context) {
|
||||||
|
iterator = iterator || identity;
|
||||||
if (obj.every) return obj.every(iterator, context);
|
if (obj.every) return obj.every(iterator, context);
|
||||||
var result = true;
|
var result = true;
|
||||||
_.each(obj, function(value, index) {
|
_.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;
|
return result;
|
||||||
};
|
};
|
||||||
@@ -110,11 +113,11 @@
|
|||||||
// Determine if at least one element in the object matches a truth test. Use
|
// Determine if at least one element in the object matches a truth test. Use
|
||||||
// JavaScript 1.6's some(), if it exists.
|
// JavaScript 1.6's some(), if it exists.
|
||||||
_.any = function(obj, iterator, context) {
|
_.any = function(obj, iterator, context) {
|
||||||
iterator = iterator || function(v) { return v; };
|
iterator = iterator || identity;
|
||||||
if (obj.some) return obj.some(iterator, context);
|
if (obj.some) return obj.some(iterator, context);
|
||||||
var result = false;
|
var result = false;
|
||||||
_.each(obj, function(value, index) {
|
_.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;
|
return result;
|
||||||
};
|
};
|
||||||
@@ -185,7 +188,7 @@
|
|||||||
// Use a comparator function to figure out at what index an object should
|
// Use a comparator function to figure out at what index an object should
|
||||||
// be inserted so as to maintain order. Uses binary search.
|
// be inserted so as to maintain order. Uses binary search.
|
||||||
_.sortedIndex = function(array, obj, iterator) {
|
_.sortedIndex = function(array, obj, iterator) {
|
||||||
iterator = iterator || function(val) { return val; };
|
iterator = iterator || identity;
|
||||||
var low = 0, high = array.length;
|
var low = 0, high = array.length;
|
||||||
while (low < high) {
|
while (low < high) {
|
||||||
var mid = (low + high) >> 1;
|
var mid = (low + high) >> 1;
|
||||||
@@ -273,7 +276,7 @@
|
|||||||
// item in an array, or -1 if the item is not included in the array.
|
// item in an array, or -1 if the item is not included in the array.
|
||||||
_.indexOf = function(array, item) {
|
_.indexOf = function(array, item) {
|
||||||
if (array.indexOf) return array.indexOf(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;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -453,6 +456,6 @@
|
|||||||
|
|
||||||
/*------------------------- Export for ServerJS ----------------------------*/
|
/*------------------------- Export for ServerJS ----------------------------*/
|
||||||
|
|
||||||
if (!_.isUndefined(exports)) exports = _;
|
if (typeof exports != 'undefined') exports = _;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user