From 0297e31893f215ab588a379180b5439d13e0fa0d Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 13 May 2012 17:53:10 -0400 Subject: [PATCH] Simplify `createIterator`. Former-commit-id: 3058b25bf7677a45261b640bd6ff6e1af1849c2c --- lodash.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lodash.js b/lodash.js index 717656920..f99a64d18 100644 --- a/lodash.js +++ b/lodash.js @@ -3,7 +3,7 @@ * Copyright 2012 John-David Dalton * Based on Underscore.js 1.3.3, copyright 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. * - * Available under MIT license + * Available under MIT license */ ;(function(window, undefined) { 'use strict'; @@ -326,18 +326,21 @@ */ var isEmpty = createIterator({ 'args': 'value', - 'iterate': 'objects', 'init': 'true', 'top': 'var className = toString.call(value);\n' + 'if (className == arrayClass || className == stringClass) return !value.length', - 'inLoop': 'return false' + 'inLoop': { + 'object': 'return false' + } }); /*--------------------------------------------------------------------------*/ /** - * Creates compiled iteration functions. + * Creates compiled iteration functions. The iteration function will be created + * to iterate over only objects if the first argument of `options.args` is + * "object" or `options.inLoop.array` is falsey. * * @private * @param {Object} [options1, options2, ..] The compile options objects. @@ -345,10 +348,6 @@ * args - A string of comma separated arguments the iteration function will * accept. * - * iterate - A string to specify whether the iteration function is only for - * "arrays" or "objects". It is automatically set if the first argument of - * `options.args` is "array" or "object". - * * init - A string to specify the initial value of the `result` variable. * * exit - A string of code to use in place of the default exit-early check @@ -406,10 +405,10 @@ } // set additional template data values var args = data.args, + arrayBranch = data.arrayBranch, objectBranch = data.objectBranch, firstArg = /^[^,]+/.exec(args)[0], loopExp = objectBranch.loopExp, - iterate = data.iterate, iteratedObject = /\S+$/.exec(loopExp || firstArg)[0]; data.firstArg = firstArg; @@ -422,13 +421,10 @@ if (!data.exit) { data.exit = 'if (' + firstArg + ' == null) return result'; } - if (firstArg == 'object' || iterate == 'objects') { + if (firstArg == 'object' || !arrayBranch.inLoop) { data.arrayBranch = null; } - if (firstArg == 'array' || iterate == 'arrays') { - data.objectBranch = null; - } - else if (!loopExp) { + if (!loopExp) { objectBranch.loopExp = 'index in ' + iteratedObject; } // create the function factory