mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
lodash: Add comments to iterationFactory. [jddalton]
Former-commit-id: 20e591a9f18a4433d3974694698445667677394e
This commit is contained in:
31
lodash.js
31
lodash.js
@@ -245,12 +245,13 @@
|
|||||||
options = {},
|
options = {},
|
||||||
props = ['beforeLoop', 'loopExp', 'inLoop', 'afterLoop'];
|
props = ['beforeLoop', 'loopExp', 'inLoop', 'afterLoop'];
|
||||||
|
|
||||||
|
// use a while-loop to merge options objects because `extend` isn't defined yet
|
||||||
while (++index < arguments.length) {
|
while (++index < arguments.length) {
|
||||||
for (prop in arguments[index]) {
|
for (prop in arguments[index]) {
|
||||||
options[prop] = arguments[index][prop];
|
options[prop] = arguments[index][prop];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// assign the `array` and `object` branch options
|
||||||
while ((prop = props.pop())) {
|
while ((prop = props.pop())) {
|
||||||
if (typeof options[prop] == 'object') {
|
if (typeof options[prop] == 'object') {
|
||||||
array[prop] = options[prop].array;
|
array[prop] = options[prop].array;
|
||||||
@@ -268,36 +269,62 @@
|
|||||||
objectBranch = !(args == 'array' || iterate == 'arrays'),
|
objectBranch = !(args == 'array' || iterate == 'arrays'),
|
||||||
useHas = options.useHas !== false;
|
useHas = options.useHas !== false;
|
||||||
|
|
||||||
|
// stings 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',
|
||||||
|
// compile the function in strict mode
|
||||||
'"use strict";' +
|
'"use strict";' +
|
||||||
|
// compile the arguments the function accepts
|
||||||
'return function(' + args + '){\n' +
|
'return function(' + args + '){\n' +
|
||||||
|
// add code that goes at the top of the iteration method
|
||||||
(options.top || '') + ';\n' +
|
(options.top || '') + ';\n' +
|
||||||
|
// assign the `result` variable an initial value
|
||||||
('var index, result' + (init ? '=' + init : '')) + ';\n' +
|
('var index, result' + (init ? '=' + init : '')) + ';\n' +
|
||||||
|
// if the first argument, e.g. `collection`, is nullish then exit early
|
||||||
'if(' + firstArg + '==undefined)return ' + (options.exits || 'result') + ';\n' +
|
'if(' + firstArg + '==undefined)return ' + (options.exits || 'result') + ';\n' +
|
||||||
|
// the following branch is for iterating arrays and array-like objects
|
||||||
(arrayBranch
|
(arrayBranch
|
||||||
|
// initialize `length` and `index` values
|
||||||
? 'var length=' + firstArg + '.length;\nindex=-1;\n' +
|
? 'var length=' + firstArg + '.length;\nindex=-1;\n' +
|
||||||
|
// check if the `collection` is array-like when also supporting object iteration
|
||||||
((objectBranch ? 'if(length===+length){\n' : '') +
|
((objectBranch ? 'if(length===+length){\n' : '') +
|
||||||
|
// add code before the while-loop
|
||||||
(array.beforeLoop || '') + ';\n' +
|
(array.beforeLoop || '') + ';\n' +
|
||||||
'while(' + (array.loopExp || '++index<length') + '){\n' + array.inLoop + '\n}' +
|
// add a custom loop expression
|
||||||
|
'while(' + (array.loopExp || '++index<length') + '){\n' +
|
||||||
|
// add code inside the while-loop
|
||||||
|
array.inLoop +
|
||||||
|
'\n}' +
|
||||||
|
// add code after the while-loop
|
||||||
(array.afterLoop || '') + ';\n' +
|
(array.afterLoop || '') + ';\n' +
|
||||||
|
// end the array-like if-statement
|
||||||
(objectBranch ? '\n}\n' : ''))
|
(objectBranch ? '\n}\n' : ''))
|
||||||
: ''
|
: ''
|
||||||
) +
|
) +
|
||||||
|
// the following branch is for iterating an object's own or inherited keys (configurable)
|
||||||
(objectBranch
|
(objectBranch
|
||||||
|
// begin the else-statement when also supporting array-like iteration
|
||||||
? ((arrayBranch ? 'else{\n' : '') +
|
? ((arrayBranch ? 'else{\n' : '') +
|
||||||
|
// add code before the for-in loop
|
||||||
(object.beforeLoop || '') + ';\n' +
|
(object.beforeLoop || '') + ';\n' +
|
||||||
|
// add a custom loop expression
|
||||||
'for(' + (object.loopExp || 'index in ' + firstArg) + '){\n' +
|
'for(' + (object.loopExp || 'index in ' + firstArg) + '){\n' +
|
||||||
|
// when `options.useHas` is `true` compile in `hasOwnProperty` checks
|
||||||
(useHas ? 'if(hasOwnProperty.call(' + /\S+$/.exec(object.loopExp || firstArg)[0] + ',index)){\n' : '') +
|
(useHas ? 'if(hasOwnProperty.call(' + /\S+$/.exec(object.loopExp || firstArg)[0] + ',index)){\n' : '') +
|
||||||
|
// add code inside the for-in loop
|
||||||
object.inLoop +
|
object.inLoop +
|
||||||
(useHas ? '\n}' : '') +
|
(useHas ? '\n}' : '') +
|
||||||
'\n}' +
|
'\n}' +
|
||||||
|
// add code after the for-in loop
|
||||||
(object.afterLoop || '') + ';\n' +
|
(object.afterLoop || '') + ';\n' +
|
||||||
|
// end the object iteration else-statement
|
||||||
(arrayBranch ? '\n}\n' : ''))
|
(arrayBranch ? '\n}\n' : ''))
|
||||||
: ''
|
: ''
|
||||||
) +
|
) +
|
||||||
|
// add code that goes at the bottom of the iteration method
|
||||||
(options.bottom || '') + ';\n' +
|
(options.bottom || '') + ';\n' +
|
||||||
|
// finally, return the `result`
|
||||||
'return ' + (options.returns || 'result') +
|
'return ' + (options.returns || 'result') +
|
||||||
'\n}'
|
'\n}'
|
||||||
)(arrayClass, bind, concat, funcClass, hasOwnProperty, identity,
|
)(arrayClass, bind, concat, funcClass, hasOwnProperty, identity,
|
||||||
|
|||||||
Reference in New Issue
Block a user