lodash: Cleanup code. [jddalton, mathiasbynens]

Former-commit-id: 639655426b7b928ca756993227bc6f80484deae2
This commit is contained in:
John-David Dalton
2012-05-03 00:31:25 -04:00
parent 221f70e609
commit 04ca0ae309
2 changed files with 35 additions and 35 deletions

View File

@@ -31,13 +31,13 @@
'index', 'index',
'indexOf', 'indexOf',
'Infinity', 'Infinity',
'initial',
'isArray', 'isArray',
'isEmpty', 'isEmpty',
'isFunc', 'isFunc',
'length', 'length',
'object', 'object',
'Math', 'Math',
'noaccum',
'prop', 'prop',
'property', 'property',
'result', 'result',

View File

@@ -109,8 +109,8 @@
'init': '', 'init': '',
'exit': 'if (!collection) return []', 'exit': 'if (!collection) return []',
'beforeLoop': { 'beforeLoop': {
'array': 'result=Array(length)', 'array': 'result = Array(length)',
'object': 'result=[]' 'object': 'result = []'
}, },
'inLoop': { 'inLoop': {
'array': 'result[index] = callback(collection[index], index, collection)', 'array': 'result[index] = callback(collection[index], index, collection)',
@@ -179,12 +179,12 @@
* _.isArray([1, 2, 3]); * _.isArray([1, 2, 3]);
* // => true * // => true
*/ */
var isArray = nativeIsArray || function isArray(value) { var isArray = nativeIsArray || function(value) {
return toString.call(value) == arrayClass; return toString.call(value) == arrayClass;
}; };
/** /**
* Checks if a `value` is empty. Arrays or strings with a length of 0 and * Checks if a `value` is empty. Arrays or strings with a length of `0` and
* objects with no enumerable own properties are considered "empty". * objects with no enumerable own properties are considered "empty".
* *
* @static * @static
@@ -265,7 +265,7 @@
objectBranch = !(firstArg == 'array' || iterate == 'arrays'), objectBranch = !(firstArg == 'array' || iterate == 'arrays'),
useHas = options.useHas !== false; useHas = options.useHas !== false;
// stings used to compile methods are minified during the build process // all strings used to compile methods are minified during the build process
return Function('arrayClass, bind, concat, funcClass, hasOwnProperty, identity,' + return Function('arrayClass, bind, concat, funcClass, hasOwnProperty, identity,' +
'indexOf, Infinity, isArray, isEmpty, Math, slice, stringClass,' + 'indexOf, Infinity, isArray, isEmpty, Math, slice, stringClass,' +
'toString, undefined', 'toString, undefined',
@@ -275,7 +275,7 @@
'return function(' + args + ') {\n' + 'return function(' + args + ') {\n' +
// assign the `result` variable an initial value // assign the `result` variable an initial value
('var index, result' + (init ? '=' + init : '')) + ';\n' + ('var index, result' + (init ? '=' + init : '')) + ';\n' +
// add code to exit early or do so if the first argument is nullish // add code to exit early or do so if the first argument is falsey
(options.exit || 'if (!' + firstArg + ') return result') + ';\n' + (options.exit || 'if (!' + firstArg + ') return result') + ';\n' +
// add code after the exit snippet but before the iteration branches // add code after the exit snippet but before the iteration branches
(options.top || '') + ';\n' + (options.top || '') + ';\n' +
@@ -349,9 +349,9 @@
}); });
/** /**
* Checks if the `callback` returns truthy for **all** values of a `collection`. * Checks if the `callback` returns a truthy value for **all** elements of a
* The `callback` is invoked with 3 arguments; for arrays they are * `collection`. The `callback` is invoked with 3 arguments; for arrays they
* (value, index, array) and for objects they are (value, key, object). * are (value, index, array) and for objects they are (value, key, object).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -600,18 +600,18 @@
'args': 'collection, callback, accumulator, thisArg', 'args': 'collection, callback, accumulator, thisArg',
'init': 'accumulator', 'init': 'accumulator',
'top': 'top':
'var initial = arguments.length > 2;\n' + 'var noaccum = arguments.length < 3;\n' +
'if (thisArg) callback = bind(callback, thisArg)', 'if (thisArg) callback = bind(callback, thisArg)',
'beforeLoop': { 'beforeLoop': {
'array': 'if (!initial) result = collection[++index]' 'array': 'if (noaccum) result = collection[++index]'
}, },
'inLoop': { 'inLoop': {
'array': 'array':
'result = callback(result, collection[index], index, collection)', 'result = callback(result, collection[index], index, collection)',
'object': 'object':
'result = initial\n' + 'result = noaccum\n' +
'? callback(result, collection[index], index, collection)\n' + '? (noaccum = false, collection[index])\n' +
': (initial = true, collection[index])' ': callback(result, collection[index], index, collection)'
} }
}); });
@@ -636,39 +636,39 @@
* var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
* // => [4, 5, 2, 3, 0, 1] * // => [4, 5, 2, 3, 0, 1]
*/ */
function reduceRight(collection, callback, result, thisArg) { function reduceRight(collection, callback, accumulator, thisArg) {
if (!collection) { if (!collection) {
return result; return accumulator;
} }
var initial = arguments.length > 2, var length = collection.length,
length = collection.length; noaccum = arguments.length < 3;
if(thisArg) { if(thisArg) {
callback = bind(callback, thisArg); callback = bind(callback, thisArg);
} }
if (length === +length) { if (length === +length) {
if (length && !initial) { if (length && noaccum) {
result = collection[--length]; accumulator = collection[--length];
} }
while (length--) { while (length--) {
result = callback(result, collection[length], length, collection); accumulator = callback(accumulator, collection[length], length, collection);
} }
return result; return accumulator;
} }
var prop, var prop,
props = keys(collection); props = keys(collection);
length = props.length; length = props.length;
if (length && !initial) { if (length && noaccum) {
result = collection[props[--length]]; accumulator = collection[props[--length]];
} }
while (length--) { while (length--) {
prop = props[length]; prop = props[length];
result = callback(result, collection[prop], prop, collection); accumulator = callback(accumulator, collection[prop], prop, collection);
} }
return result; return accumulator;
} }
/** /**
@@ -760,11 +760,11 @@
} }
/** /**
* Checks if the `callback` returns truthy for **any** value of a `collection`. * Checks if the `callback` returns a truthy value for **any** element of a
* The function returns as soon as it finds passing value, and does not iterate * `collection`. The function returns as soon as it finds passing value, and
* over the entire `collection`. The `callback` is invoked with 3 arguments; for * does not iterate over the entire `collection`. The `callback` is invoked
* arrays they are (value, index, array) and for objects they are * with 3 arguments; for arrays they are (value, index, array) and for objects
* (value, key, object). * they are (value, key, object).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -1865,8 +1865,8 @@
* _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b'); * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
* // => true * // => true
*/ */
function has(object, prop) { function has(object, property) {
return hasOwnProperty.call(object, prop); return hasOwnProperty.call(object, property);
} }
/** /**
@@ -1890,7 +1890,7 @@
}; };
// fallback for browser like IE<9 which detect `arguments` as `[object Object]` // fallback for browser like IE<9 which detect `arguments` as `[object Object]`
if (!isArguments(arguments)) { if (!isArguments(arguments)) {
isArguments = function isArguments(value) { isArguments = function(value) {
return !!(value && hasOwnProperty.call(value, 'callee')); return !!(value && hasOwnProperty.call(value, 'callee'));
}; };
} }