Skip to content

Commit

Permalink
Simplify createIterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Oct 6, 2012
1 parent 3351f59 commit c0663c0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 79 deletions.
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@
.replace(/__p *\+= *' *';/g, '')
.replace(/(__p *\+= *)' *' *\+/g, '$1')
.replace(/(\{) *;|; *(\})/g, '$1$2')
.replace(/\(\(__t *= *\( *([^)]+) *\)\) *== *null *\? *'' *: *__t\)/g, '$1');
.replace(/\(\(__t *= *\( *([^)]+) *\)\) *== *null *\? *'' *: *__t\)/g, '($1)');

// remove the with-statement
snippet = snippet.replace(/ *with *\(.+?\) *{/, '\n').replace(/}([^}]*}[^}]*$)/, '$1');
Expand Down
28 changes: 11 additions & 17 deletions lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
// the `iteratee` may be reassigned by the `top` snippet
'var index, value, iteratee = <%= firstArg %>, ' +
// assign the `result` variable an initial value
'result<% if (init) { %> = <%= init %><% } %>;\n' +
'result = <%= init || firstArg %>;\n' +
// exit early if the first argument is falsey
'if (!<%= firstArg %>) return result;\n' +
// add code before the iteration branches
Expand Down Expand Up @@ -686,13 +686,10 @@
* @returns {Function} Returns the compiled function.
*/
function createIterator() {
var index = -1,
length = arguments.length;

// merge options into a template data object
var data = {
'bottom': '',
'hasDontEnumBug': hasDontEnumBug,
'init': '',
'isKeysFast': isKeysFast,
'noArgsEnum': noArgsEnum,
'noCharByIndex': noCharByIndex,
Expand All @@ -704,8 +701,11 @@
'objectBranch': {}
};

while (++index < length) {
var object = arguments[index];
var object,
index = -1;

// merge options into a template data object
while (object = arguments[++index]) {
for (var prop in object) {
var value = object[prop];
// keep this regexp explicit for the build pre-process
Expand All @@ -721,14 +721,8 @@
}
}
// set additional template `data` values
var args = data.args,
firstArg = /^[^,]+/.exec(args)[0],
init = data.init;

data.firstArg = firstArg;
data.init = init == null ? firstArg : init;

if (firstArg != 'collection' || !data.arrayBranch.inLoop) {
var args = data.args;
if ((data.firstArg = /^[^,]+/.exec(args)[0]) != 'collection' || !data.arrayBranch.inLoop) {
data.arrayBranch = null;
}
// create the function factory
Expand Down Expand Up @@ -1742,7 +1736,7 @@
' result[index] = stackB[stackLength]\n' +
' } else {\n' +
' stackA.push(source);\n' +
' stackB.push(value = (value = result[index]) && isArr\n' +
' stackB.push(value = (value = result[index], isArr)\n' +
' ? (isArray(value) ? value : [])\n' +
' : (isPlainObject(value) ? value : {})\n' +
' );\n' +
Expand Down Expand Up @@ -1985,7 +1979,7 @@
* // => 2
*/
var find = createIterator(baseIteratorOptions, forEachIteratorOptions, {
'init': false,
'init': 'undefined',
'inLoop': 'if (callback(value, index, collection)) return value'
});

Expand Down
Loading

0 comments on commit c0663c0

Please sign in to comment.