Reduce doc examples, simplify iteratorTemplate, remove deep argument support from _.clone, and ensure _.merge and _.pick exit early for non-objects.

Former-commit-id: d3fbd9a8ff9cd0c8201beb7180e3e7190e2a7cf8
This commit is contained in:
John-David Dalton
2013-01-26 20:37:26 -08:00
parent af234cbe54
commit 17fc3c2317
5 changed files with 294 additions and 278 deletions

View File

@@ -261,7 +261,8 @@
'var argsIndex = 0,\n' +
" argsLength = typeof guard == 'number' ? 2 : arguments.length;\n" +
'while (++argsIndex < argsLength) {\n' +
' if ((iterable = arguments[argsIndex])) {',
' iterable = arguments[argsIndex];\n' +
' if (iterable && objectTypes[typeof iterable]) {',
'loop': 'result[index] = iterable[index]',
'bottom': ' }\n}'
};
@@ -276,6 +277,7 @@
/** Reusable iterator options for `forIn` and `forOwn` */
var forOwnIteratorOptions = {
'top': 'if (!objectTypes[typeof iterable]) return result;\n' + eachIteratorOptions.top,
'arrays': false
};
@@ -476,8 +478,8 @@
* @returns {Array|Object|String} Returns `collection`.
*/
var each = function (collection, callback, thisArg) {
var index, iterable = collection, result = collection;
if (!collection) return result;
var index, iterable = collection, result = iterable;
if (!iterable) return result;
callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg);
var length = iterable.length; index = -1;
if (typeof length == 'number') {
@@ -635,8 +637,9 @@
* // => alerts 'name' and 'bark' (order is not guaranteed)
*/
var forIn = function (collection, callback) {
var index, iterable = collection, result = collection;
if (!collection) return result;
var index, iterable = collection, result = iterable;
if (!iterable) return result;
if (!objectTypes[typeof iterable]) return result;
callback || (callback = identity);
for (index in iterable) {
@@ -666,8 +669,9 @@
* // => alerts '0', '1', and 'length' (order is not guaranteed)
*/
var forOwn = function (collection, callback) {
var index, iterable = collection, result = collection;
if (!collection) return result;
var index, iterable = collection, result = iterable;
if (!iterable) return result;
if (!objectTypes[typeof iterable]) return result;
callback || (callback = identity);
for (index in iterable) {
@@ -819,16 +823,13 @@
}
/**
* Creates a clone of `value`. If `deep` is `true`, nested objects will also
* be cloned, otherwise they will be assigned by reference.
* Creates a shallow clone of `value`. Nested objects will be assigned by reference.
*
* @static
* @memberOf _
* @category Objects
* @param {Mixed} value The value to clone.
* @param {Boolean} deep A flag to indicate a deep clone.
* @param- {Object} [guard] Internally used to allow this method to work with
* others like `_.map` without using their callback `index` argument for `deep`.
* @param- {Object} [deep] Internally used to indicate performing a deep clone.
* @param- {Array} [stackA=[]] Internally used to track traversed source objects.
* @param- {Array} [stackB=[]] Internally used to associate clones with their
* source counterparts.
@@ -837,16 +838,14 @@
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 },
* { 'name': 'curly', 'age': 60 }
* { 'name': 'larry', 'age': 50 }
* ];
*
* var shallow = _.clone(stooges);
* shallow[0] === stooges[0];
* // => true
*
* var deep = _.clone(stooges, true);
* deep[0] === stooges[0];
* shallow == stooges;
* // => false
*/
function clone(value) {
@@ -946,8 +945,8 @@
* @returns {Object} Returns the created inverted object.
* @example
*
* _.invert({ 'first': 'Moe', 'second': 'Larry', 'third': 'Curly' });
* // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed)
* _.invert({ 'first': 'moe', 'second': 'larry' });
* // => { 'moe': 'first', 'larry': 'second' } (order is not guaranteed)
*/
function invert(object) {
var index = -1,
@@ -1439,8 +1438,8 @@
* @returns {Array} Returns new array of key-value pairs.
* @example
*
* _.pairs({ 'moe': 30, 'larry': 40, 'curly': 50 });
* // => [['moe', 30], ['larry', 40], ['curly', 50]] (order is not guaranteed)
* _.pairs({ 'moe': 30, 'larry': 40 });
* // => [['moe', 30], ['larry', 40]] (order is not guaranteed)
*/
function pairs(object) {
var index = -1,
@@ -1877,14 +1876,16 @@
* @returns {Mixed} Returns the maximum value.
* @example
*
* _.max([4, 2, 8, 6]);
* // => 8
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 },
* { 'name': 'curly', 'age': 60 }
* { 'name': 'larry', 'age': 50 }
* ];
*
* _.max(stooges, function(stooge) { return stooge.age; });
* // => { 'name': 'curly', 'age': 60 };
* // => { 'name': 'larry', 'age': 50 };
*/
function max(collection, callback, thisArg) {
var computed = -Infinity,
@@ -1929,8 +1930,16 @@
* @returns {Mixed} Returns the minimum value.
* @example
*
* _.min([10, 5, 100, 2, 1000]);
* _.min([4, 2, 8, 6]);
* // => 2
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 }
* ];
*
* _.min(stooges, function(stooge) { return stooge.age; });
* // => { 'name': 'moe', 'age': 40 };
*/
function min(collection, callback, thisArg) {
var computed = Infinity,
@@ -1974,12 +1983,11 @@
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 },
* { 'name': 'curly', 'age': 60 }
* { 'name': 'larry', 'age': 50 }
* ];
*
* _.pluck(stooges, 'name');
* // => ['moe', 'larry', 'curly']
* // => ['moe', 'larry']
*/
function pluck(collection, property) {
return map(collection, property + '');
@@ -2206,8 +2214,8 @@
* _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
* // => [3, 1, 2]
*
* _.sortBy(['larry', 'brendan', 'moe'], 'length');
* // => ['moe', 'larry', 'brendan']
* _.sortBy(['banana', 'strawberry', 'apple'], 'length');
* // => ['apple', 'banana', 'strawberry']
*/
function sortBy(collection, callback, thisArg) {
var index = -1,
@@ -2265,8 +2273,7 @@
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 },
* { 'name': 'curly', 'age': 60 }
* { 'name': 'larry', 'age': 50 }
* ];
*
* _.where(stooges, { 'age': 40 });
@@ -2644,8 +2651,8 @@
* corresponding values.
* @example
*
* _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
* // => { 'moe': 30, 'larry': 40, 'curly': 50 }
* _.object(['moe', 'larry'], [30, 40]);
* // => { 'moe': 30, 'larry': 40 }
*/
function object(keys, values) {
var index = -1,
@@ -2937,8 +2944,8 @@
* @returns {Array} Returns a new array of grouped elements.
* @example
*
* _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
* // => [['moe', 30, true], ['larry', 40, false], ['curly', 50, false]]
* _.zip(['moe', 'larry'], [30, 40], [true, false]);
* // => [['moe', 30, true], ['larry', 40, false]]
*/
function zip(array) {
var index = -1,
@@ -3366,11 +3373,11 @@
* }
* });
*
* _.capitalize('larry');
* // => 'Larry'
* _.capitalize('moe');
* // => 'Moe'
*
* _('curly').capitalize();
* // => 'Curly'
* _('moe').capitalize();
* // => 'Moe'
*/
function mixin(object) {
forEach(functions(object), function(methodName) {
@@ -3503,8 +3510,8 @@
* // => 'hello moe'
*
* var list = '<% _.forEach(people, function(name) { %><li><%= name %></li><% }); %>';
* _.template(list, { 'people': ['moe', 'larry', 'curly'] });
* // => '<li>moe</li><li>larry</li><li>curly</li>'
* _.template(list, { 'people': ['moe', 'larry'] });
* // => '<li>moe</li><li>larry</li>'
*
* // using the "escape" delimiter to escape HTML in data property values
* _.template('<b><%- value %></b>', { 'value': '<script>' });