mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
docs done -- going back to code comments
This commit is contained in:
137
index.html
137
index.html
@@ -116,7 +116,7 @@
|
||||
<br />
|
||||
<span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
|
||||
<a href="#extend">extend</a>, <a href="#clone">clone</a>, <a href="#isEqual">isEqual</a>, <a href="#isElement">isElement</a>,
|
||||
<a href="#isArray">isArray</a>, <a href="#isFunction">isFunction</a>, <a href="#isUndefined">isUndefined</a>, <a href="#toString">toString</a>
|
||||
<a href="#isArray">isArray</a>, <a href="#isFunction">isFunction</a>, <a href="#isUndefined">isUndefined</a>
|
||||
</span>
|
||||
</p>
|
||||
|
||||
@@ -142,7 +142,7 @@
|
||||
</p>
|
||||
<pre>
|
||||
_.each([1, 2, 3], function(num){ alert(num); });
|
||||
=> alerts each number in turn...</pre>
|
||||
=> alerts each number in turn...</pre>
|
||||
|
||||
<p id="map">
|
||||
<b>map</b><code>_.map(list, iterator, [context])</code>
|
||||
@@ -153,7 +153,7 @@ _.each([1, 2, 3], function(num){ alert(num); });
|
||||
</p>
|
||||
<pre>
|
||||
_.map([1, 2, 3], function(num){ return num * 3 });
|
||||
=> [3, 6, 9]</pre>
|
||||
=> [3, 6, 9]</pre>
|
||||
|
||||
<p id="inject">
|
||||
<b>inject</b><code>_.inject(list, memo, iterator, [context])</code>
|
||||
@@ -165,7 +165,7 @@ _.map([1, 2, 3], function(num){ return num * 3 });
|
||||
</p>
|
||||
<pre>
|
||||
var sum = _.inject([1, 2, 3], 0, function(memo, num){ return memo + num });
|
||||
=> 6
|
||||
=> 6
|
||||
</pre>
|
||||
|
||||
<p id="detect">
|
||||
@@ -178,7 +178,7 @@ var sum = _.inject([1, 2, 3], 0, function(memo, num){ return memo + num });
|
||||
</p>
|
||||
<pre>
|
||||
var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
=> 2
|
||||
=> 2
|
||||
</pre>
|
||||
|
||||
<p id="select">
|
||||
@@ -190,7 +190,7 @@ var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
</p>
|
||||
<pre>
|
||||
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
=> [2, 4, 6]
|
||||
=> [2, 4, 6]
|
||||
</pre>
|
||||
|
||||
<p id="reject">
|
||||
@@ -201,7 +201,7 @@ var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
</p>
|
||||
<pre>
|
||||
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
=> [1, 3, 5]
|
||||
=> [1, 3, 5]
|
||||
</pre>
|
||||
|
||||
<p id="all">
|
||||
@@ -214,7 +214,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
</p>
|
||||
<pre>
|
||||
_.all([true, 1, null, 'yes']);
|
||||
=> false
|
||||
=> false
|
||||
</pre>
|
||||
|
||||
<p id="any">
|
||||
@@ -227,7 +227,7 @@ _.all([true, 1, null, 'yes']);
|
||||
</p>
|
||||
<pre>
|
||||
_.any([null, 0, 'yes', false]);
|
||||
=> true
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<p id="include">
|
||||
@@ -239,7 +239,7 @@ _.any([null, 0, 'yes', false]);
|
||||
</p>
|
||||
<pre>
|
||||
_.include([1, 2, 3], 3);
|
||||
=> true
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<p id="invoke">
|
||||
@@ -251,7 +251,7 @@ _.include([1, 2, 3], 3);
|
||||
</p>
|
||||
<pre>
|
||||
_.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
||||
=> [[1, 5, 7], [1, 2, 3]]
|
||||
=> [[1, 5, 7], [1, 2, 3]]
|
||||
</pre>
|
||||
|
||||
<p id="pluck">
|
||||
@@ -263,7 +263,7 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
||||
<pre>
|
||||
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
|
||||
_.pluck(stooges, 'name');
|
||||
=> ["moe", "larry", "curly"]
|
||||
=> ["moe", "larry", "curly"]
|
||||
</pre>
|
||||
|
||||
<p id="max">
|
||||
@@ -276,7 +276,7 @@ _.pluck(stooges, 'name');
|
||||
<pre>
|
||||
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
|
||||
_.max(stooges, function(stooge){ return stooge.age; });
|
||||
=> {name : 'curly', age : 60};
|
||||
=> {name : 'curly', age : 60};
|
||||
</pre>
|
||||
|
||||
<p id="min">
|
||||
@@ -289,7 +289,7 @@ _.max(stooges, function(stooge){ return stooge.age; });
|
||||
<pre>
|
||||
var numbers = [10, 5, 100, 2, 1000];
|
||||
_.min(numbers);
|
||||
=> 2
|
||||
=> 2
|
||||
</pre>
|
||||
|
||||
<p id="sortBy">
|
||||
@@ -300,7 +300,7 @@ _.min(numbers);
|
||||
</p>
|
||||
<pre>
|
||||
_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
|
||||
=> [5, 4, 6, 3, 1, 2]
|
||||
=> [5, 4, 6, 3, 1, 2]
|
||||
</pre>
|
||||
|
||||
<p id="sortedIndex">
|
||||
@@ -313,7 +313,7 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
|
||||
</p>
|
||||
<pre>
|
||||
_.sortedIndex([10, 20, 30, 40, 50], 35);
|
||||
=> 3
|
||||
=> 3
|
||||
</pre>
|
||||
|
||||
<p id="toArray">
|
||||
@@ -324,7 +324,7 @@ _.sortedIndex([10, 20, 30, 40, 50], 35);
|
||||
</p>
|
||||
<pre>
|
||||
(function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
|
||||
=> [1, 2, 3]
|
||||
=> [1, 2, 3]
|
||||
</pre>
|
||||
|
||||
<p id="size">
|
||||
@@ -334,7 +334,7 @@ _.sortedIndex([10, 20, 30, 40, 50], 35);
|
||||
</p>
|
||||
<pre>
|
||||
_.size({one : 1, two : 2, three : 3});
|
||||
=> 3
|
||||
=> 3
|
||||
</pre>
|
||||
|
||||
<h2>Array Functions</h2>
|
||||
@@ -346,7 +346,7 @@ _.size({one : 1, two : 2, three : 3});
|
||||
</p>
|
||||
<pre>
|
||||
_.first([3, 2, 1]);
|
||||
=> 3
|
||||
=> 3
|
||||
</pre>
|
||||
|
||||
<p id="last">
|
||||
@@ -356,7 +356,7 @@ _.first([3, 2, 1]);
|
||||
</p>
|
||||
<pre>
|
||||
_.last([3, 2, 1]);
|
||||
=> 1
|
||||
=> 1
|
||||
</pre>
|
||||
|
||||
<p id="compact">
|
||||
@@ -368,7 +368,7 @@ _.last([3, 2, 1]);
|
||||
</p>
|
||||
<pre>
|
||||
_.compact([0, 1, false, 2, '', 3]);
|
||||
=> [1, 2, 3]
|
||||
=> [1, 2, 3]
|
||||
</pre>
|
||||
|
||||
<p id="flatten">
|
||||
@@ -378,7 +378,7 @@ _.compact([0, 1, false, 2, '', 3]);
|
||||
</p>
|
||||
<pre>
|
||||
_.flatten([1, [2], [3, [[[4]]]]]);
|
||||
=> [1, 2, 3, 4];
|
||||
=> [1, 2, 3, 4];
|
||||
</pre>
|
||||
|
||||
<p id="without">
|
||||
@@ -389,7 +389,7 @@ _.flatten([1, [2], [3, [[[4]]]]]);
|
||||
</p>
|
||||
<pre>
|
||||
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
|
||||
=> [2, 3, 4]
|
||||
=> [2, 3, 4]
|
||||
</pre>
|
||||
|
||||
<p id="uniq">
|
||||
@@ -401,7 +401,7 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
|
||||
</p>
|
||||
<pre>
|
||||
_.uniq([1, 2, 1, 3, 1, 4]);
|
||||
=> [1, 2, 3, 4]
|
||||
=> [1, 2, 3, 4]
|
||||
</pre>
|
||||
|
||||
<p id="intersect">
|
||||
@@ -412,7 +412,7 @@ _.uniq([1, 2, 1, 3, 1, 4]);
|
||||
</p>
|
||||
<pre>
|
||||
_.intersect([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
||||
=> [1, 2]
|
||||
=> [1, 2]
|
||||
</pre>
|
||||
|
||||
<p id="zip">
|
||||
@@ -424,7 +424,7 @@ _.intersect([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
||||
</p>
|
||||
<pre>
|
||||
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
|
||||
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
|
||||
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
|
||||
</pre>
|
||||
|
||||
<p id="indexOf">
|
||||
@@ -436,7 +436,7 @@ _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
|
||||
</p>
|
||||
<pre>
|
||||
_.indexOf([1, 2, 3], 2);
|
||||
=> 1
|
||||
=> 1
|
||||
</pre>
|
||||
|
||||
<h2>Function (uh, ahem) Functions</h2>
|
||||
@@ -453,7 +453,7 @@ _.indexOf([1, 2, 3], 2);
|
||||
var func = function(greeting){ return greeting + ': ' + this.name };
|
||||
func = _.bind(func, {name : 'moe'}, 'hi');
|
||||
func();
|
||||
=> 'hi: moe'
|
||||
=> 'hi: moe'
|
||||
</pre>
|
||||
|
||||
<p id="bindAll">
|
||||
@@ -473,7 +473,7 @@ var buttonView = {
|
||||
};
|
||||
_.bindAll('onClick', 'onHover', buttonView);
|
||||
jQuery('#underscore_button').bind('click', buttonView.onClick);
|
||||
=> When the button is clicked, this.label will have the correct value...
|
||||
=> When the button is clicked, this.label will have the correct value...
|
||||
</pre>
|
||||
|
||||
<p id="delay">
|
||||
@@ -486,7 +486,7 @@ jQuery('#underscore_button').bind('click', buttonView.onClick);
|
||||
<pre>
|
||||
var log = _.bind(console.log, console);
|
||||
_.delay(log, 1000, 'logged later');
|
||||
=> 'logged later' // Appears after one second.
|
||||
=> 'logged later' // Appears after one second.
|
||||
</pre>
|
||||
|
||||
<p id="defer">
|
||||
@@ -516,7 +516,7 @@ hello = _.wrap(hello, function(func) {
|
||||
return "before, " + func("moe") + ", after";
|
||||
});
|
||||
hello();
|
||||
=> before, hello: moe, after
|
||||
=> before, hello: moe, after
|
||||
</pre>
|
||||
|
||||
<h2>Object Functions</h2>
|
||||
@@ -528,7 +528,7 @@ hello();
|
||||
</p>
|
||||
<pre>
|
||||
_.keys({one : 1, two : 2, three : 3});
|
||||
=> ["one", "two", "three"]
|
||||
=> ["one", "two", "three"]
|
||||
</pre>
|
||||
|
||||
<p id="values">
|
||||
@@ -538,7 +538,7 @@ _.keys({one : 1, two : 2, three : 3});
|
||||
</p>
|
||||
<pre>
|
||||
_.values({one : 1, two : 2, three : 3});
|
||||
=> [1, 2, 3]
|
||||
=> [1, 2, 3]
|
||||
</pre>
|
||||
|
||||
<p id="extend">
|
||||
@@ -549,7 +549,7 @@ _.values({one : 1, two : 2, three : 3});
|
||||
</p>
|
||||
<pre>
|
||||
_.extend({name : 'moe'}, {age : 50});
|
||||
=> {name : 'moe', age : 50}
|
||||
=> {name : 'moe', age : 50}
|
||||
</pre>
|
||||
|
||||
<p id="clone">
|
||||
@@ -560,7 +560,7 @@ _.extend({name : 'moe'}, {age : 50});
|
||||
</p>
|
||||
<pre>
|
||||
_.clone({name : 'moe'});
|
||||
=> {name : 'moe'};
|
||||
=> {name : 'moe'};
|
||||
</pre>
|
||||
|
||||
<p id="isEqual">
|
||||
@@ -573,60 +573,85 @@ _.clone({name : 'moe'});
|
||||
var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
|
||||
var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
|
||||
moe == clone;
|
||||
=> false
|
||||
=> false
|
||||
_.isEqual(moe, clone);
|
||||
=> true
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<p id="">
|
||||
<b>isElement</b><code>_.()</code>
|
||||
<p id="isElement">
|
||||
<b>isElement</b><code>_.isElement(object)</code>
|
||||
<br />
|
||||
Returns <i>true</i> if <b>object</b> is a DOM element.
|
||||
</p>
|
||||
<pre>
|
||||
_.isElement(jQuery('body')[0]);
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<p id="">
|
||||
<b>isArray</b><code>_.()</code>
|
||||
<p id="isArray">
|
||||
<b>isArray</b><code>_.isArray(object)</code>
|
||||
<br />
|
||||
Returns <i>true</i> if <b>object</b> is an Array.
|
||||
</p>
|
||||
<pre>
|
||||
(function(){ return _.isArray(arguments); })();
|
||||
=> false
|
||||
_.isArray([1,2,3]);
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<p id="">
|
||||
<b>isFunction</b><code>_.()</code>
|
||||
<p id="isFunction">
|
||||
<b>isFunction</b><code>_.isFunction(object)</code>
|
||||
<br />
|
||||
Returns <i>true</i> if <b>object</b> is a Function.
|
||||
</p>
|
||||
<pre>
|
||||
_.isFunction(alert);
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<p id="">
|
||||
<b>isUndefined</b><code>_.()</code>
|
||||
<br />
|
||||
</p>
|
||||
<pre>
|
||||
</pre>
|
||||
|
||||
<p id="">
|
||||
<b>toString</b><code>_.()</code>
|
||||
<p id="isUndefined">
|
||||
<b>isUndefined</b><code>_.isUndefined(variable)</code>
|
||||
<br />
|
||||
Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
|
||||
</p>
|
||||
<pre>
|
||||
_.isUndefined(window.missingVariable);
|
||||
=> true
|
||||
</pre>
|
||||
|
||||
<h2>Utility Functions</h2>
|
||||
|
||||
<p id="">
|
||||
<b>uniqueId</b><code>_.()</code>
|
||||
<p id="uniqueId">
|
||||
<b>uniqueId</b><code>_.uniqueId([prefix])</code>
|
||||
<br />
|
||||
Generate a globally-unique id for client-side models or DOM elements
|
||||
that need one. If <b>prefix</b> is passed, the id will be appended to it.
|
||||
</p>
|
||||
<pre>
|
||||
_.uniqueId('contact_');
|
||||
=> 'contact_104'
|
||||
</pre>
|
||||
|
||||
<p id="">
|
||||
<b>template</b><code>_.()</code>
|
||||
<p id="template">
|
||||
<b>template</b><code>_.template(templateString)</code>
|
||||
<br />
|
||||
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><%= … %></i>, as well as execute arbitrary Javascript code, with
|
||||
<i><% … %></i>. When you evaluate a template, pass in a
|
||||
context object that has properties corresponding to the template's free
|
||||
variables.
|
||||
</p>
|
||||
<pre>
|
||||
var compiled = _.template("hello: <%= name %>");
|
||||
compiled({name : 'moe'});
|
||||
=> "hello: moe"
|
||||
|
||||
var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
|
||||
_.template(list, {people : ['moe', 'curly', 'larry']});
|
||||
=> "<li>moe</li><li>curly</li><li>larry</li>"
|
||||
</pre>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -40,7 +40,7 @@ $(document).ready(function() {
|
||||
test('arrays: zip', function() {
|
||||
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
|
||||
var stooges = _.zip(names, ages, leaders);
|
||||
equals(_.toString(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
|
||||
equals(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
|
||||
});
|
||||
|
||||
test("arrays: indexOf", function() {
|
||||
|
||||
@@ -59,8 +59,4 @@ $(document).ready(function() {
|
||||
ok(_.isUndefined(undefined), 'undefined is undefined');
|
||||
});
|
||||
|
||||
test("objects: toString", function() {
|
||||
equals(_.toString([1, 2, 3]), '1,2,3', 'object can be converted to printable strings');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
// Javascript can be so much more pleasant when it's functional -- re-implement
|
||||
// a bunch of utility methods from Prototype and Steele's Functional...
|
||||
// Underscore.js
|
||||
// (c) 2009 Jeremy Ashkenas, DocumentCloud Inc.
|
||||
// Underscore is freely distributable under the terms of the MIT license.
|
||||
// For all details and documentation:
|
||||
// http://fdjklsafjsdalk
|
||||
window._ = {
|
||||
|
||||
VERSION : '0.1.0',
|
||||
|
||||
// The cornerstone, an each implementation.
|
||||
// Handles objects implementing forEach, _each, arrays, and raw objects.
|
||||
each : function(obj, iterator, context) {
|
||||
@@ -345,15 +350,13 @@ window._ = {
|
||||
if (a == b) return true;
|
||||
// One of them implements an isEqual()?
|
||||
if (a.isEqual) return a.isEqual(b);
|
||||
// If a is not an object by this point, we can't handle it.
|
||||
if (atype !== 'object') return false;
|
||||
// Nothing else worked, deep compare the contents.
|
||||
return atype === 'object' && _._isEqualContents(a, b);
|
||||
},
|
||||
|
||||
// Objects have equal contents if they have the same keys, and all the values
|
||||
// are equal (as defined by _.isEqual).
|
||||
_isEqualContents : function(a, b) {
|
||||
var aKeys = _.keys(a), bKeys = _.keys(b);
|
||||
// Different object sizes?
|
||||
if (aKeys.length != bKeys.length) return false;
|
||||
// Recursive comparison of contents.
|
||||
for (var key in a) if (!_.isEqual(a[key], b[key])) return false;
|
||||
return true;
|
||||
},
|
||||
@@ -378,11 +381,6 @@ window._ = {
|
||||
return typeof obj == 'undefined';
|
||||
},
|
||||
|
||||
// Convert any value into printable string form.
|
||||
toString : function(obj) {
|
||||
return obj == null ? '' : String(obj);
|
||||
},
|
||||
|
||||
/* -------------- The following methods are utility methods --------------- */
|
||||
|
||||
// Generate a unique integer id (unique within the entire client session).
|
||||
|
||||
Reference in New Issue
Block a user