From 99e02f30fb41d66c37125a4f54f50430c0937980 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 29 Dec 2012 16:03:22 -0600 Subject: [PATCH] Simplify `createIterator`. and remove whitespace between `else {` in template text. Former-commit-id: a673f0edbe9b8655cbe1e91d9063e58130172495 --- build.js | 17 ++++++++--------- build/pre-compile.js | 8 ++++---- lodash.js | 40 ++++++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/build.js b/build.js index a9ae40f27..253450ae4 100755 --- a/build.js +++ b/build.js @@ -169,12 +169,12 @@ /** Used to inline `iteratorTemplate` */ var iteratorOptions = [ 'args', - 'arrayLoop', + 'arrays', 'bottom', 'firstArg', 'hasDontEnumBug', 'isKeysFast', - 'objectLoop', + 'loop', 'nonEnumArgs', 'noCharByIndex', 'shadowed', @@ -893,7 +893,7 @@ // remove optimized branch in `iteratorTemplate` source = source.replace(getIteratorTemplate(source), function(match) { - return match.replace(/(?: *\/\/.*\n)* *["']( *)<% *if *\(isKeysFast[\s\S]+?["']\1<% *} *else *\{ *%>.+\n([\s\S]+?) *["']\1<% *} *%>.+/, "'\\n' +\n$2"); + return match.replace(/(?: *\/\/.*\n)* *["']( *)<% *if *\(isKeysFast[\s\S]+?["']\1<% *} *else *{ *%>.+\n([\s\S]+?) *["']\1<% *} *%>.+/, "'\\n' +\n$2"); }); // remove data object property assignment in `createIterator` @@ -980,7 +980,7 @@ */ function removeNoNodeClass(source) { // remove `noNodeClass` assignment - source = source.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *try *\{(?:\s*\/\/.*)*\n *var noNodeClass[\s\S]+?catch[^}]+}\n/, ''); + source = source.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *try *{(?:\s*\/\/.*)*\n *var noNodeClass[\s\S]+?catch[^}]+}\n/, ''); // remove `noNodeClass` from `shimIsPlainObject` source = source.replace(matchFunction(source, 'shimIsPlainObject'), function(match) { @@ -1097,7 +1097,7 @@ } /** - * Hard-codes the `useStrict` template option value for `iteratorTemplate`. + * Hard-codes the `strict` template option value for `iteratorTemplate`. * * @private * @param {String} source The source to process. @@ -1109,9 +1109,9 @@ if (value) { source = source.replace(/^[\s\S]*?function[^{]+{/, "$&\n 'use strict';"); } - // replace `useStrict` branch in `iteratorTemplate` with hard-coded option + // replace `strict` branch in `iteratorTemplate` with hard-coded option source = source.replace(getIteratorTemplate(source), function(match) { - return match.replace(/(?: *\/\/.*\n)*(\s*)["'] *<%.+?useStrict.+/, value ? '$1"\'use strict\';\\n" +' : ''); + return match.replace(/(?: *\/\/.*\n)*(\s*)["'] *<%.+?strict.+/, value ? '$1"\'use strict\';\\n" +' : ''); }); return source; @@ -1780,7 +1780,6 @@ source = removeIsArgumentsFallback(source); } - source = removeFromCreateIterator(source, 'nativeKeys'); source = removeCreateFunction(source); } @@ -1916,7 +1915,7 @@ .replace(/'(?:\\n|\s)+'/g, "''") .replace(/__p *\+= *' *';/g, '') .replace(/(__p *\+= *)' *' *\+/g, '$1') - .replace(/(\{) *;|; *(\})/g, '$1$2') + .replace(/({) *;|; *(\})/g, '$1$2') .replace(/\(\(__t *= *\( *([^)]+) *\)\) *== *null *\? *'' *: *__t\)/g, '($1)'); // remove the with-statement diff --git a/build/pre-compile.js b/build/pre-compile.js index f4f19ca5d..a4ca3c661 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -35,12 +35,12 @@ /** Used to minify `compileIterator` option properties */ var iteratorOptions = [ 'args', - 'arrayLoop', + 'arrays', 'bottom', 'firstArg', 'hasDontEnumBug', 'isKeysFast', - 'objectLoop', + 'loop', 'nonEnumArgs', 'noCharByIndex', 'shadowed', @@ -235,7 +235,7 @@ string = string.slice(captured.length); } // avoids removing the '\n' of the `stringEscapes` object - string = string.replace(/\[object |delete |else |function | in |return\s+[\w"']|throw |typeof |use strict|var |@ |(["'])\\n\1|\\\\n|\\n|\s+/g, function(match) { + string = string.replace(/\[object |delete |else (?!{)|function | in |return\s+[\w"']|throw |typeof |use strict|var |@ |(["'])\\n\1|\\\\n|\\n|\s+/g, function(match) { return match == false || match == '\\n' ? '' : match; }); // prepend object literal property name @@ -256,7 +256,7 @@ .replace('"\';\n"', '"\';"') // remove `useSourceURL` variable - source = source.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *try *\{(?:\s*\/\/.*)*\n *var useSourceURL[\s\S]+?catch[^}]+}\n/, ''); + source = source.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *try *{(?:\s*\/\/.*)*\n *var useSourceURL[\s\S]+?catch[^}]+}\n/, ''); // remove debug sourceURL use in `_.template` source = source.replace(/(?:\s*\/\/.*\n)* *var sourceURL[^;]+;|\+ *sourceURL/g, ''); diff --git a/lodash.js b/lodash.js index a79941cd9..8591d7cb1 100644 --- a/lodash.js +++ b/lodash.js @@ -359,7 +359,7 @@ '<%= top %>;\n' + // array-like iteration: - '<% if (arrayLoop) { %>' + + '<% if (arrays) { %>' + 'var length = iteratee.length; index = -1;\n' + "if (typeof length == 'number') {" + @@ -372,7 +372,7 @@ // iterate over the array-like value ' while (++index < length) {\n' + - ' <%= arrayLoop %>\n' + + ' <%= loop %>\n' + ' }\n' + '}\n' + 'else {' + @@ -384,7 +384,7 @@ ' if (length && isArguments(iteratee)) {\n' + ' while (++index < length) {\n' + " index += '';\n" + - ' <%= objectLoop %>\n' + + ' <%= loop %>\n' + ' }\n' + ' } else {' + ' <% } %>' + @@ -408,7 +408,7 @@ ' while (++ownIndex < length) {\n' + ' index = ownProps[ownIndex];\n' + " <% if (!hasDontEnumBug) { %>if (!(skipProto && index == 'prototype')) {\n <% } %>" + - ' <%= objectLoop %>\n' + + ' <%= loop %>\n' + ' <% if (!hasDontEnumBug) { %>}\n<% } %>' + ' }' + @@ -421,7 +421,7 @@ ' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' + ' %>) {' + ' <% } %>\n' + - ' <%= objectLoop %>;' + + ' <%= loop %>;' + ' <% if (!hasDontEnumBug || useHas) { %>\n }<% } %>\n' + ' }' + ' <% } %>' + @@ -438,11 +438,11 @@ " if (shadowed[k] == 'constructor') {" + ' %>!(ctor && ctor.prototype === iteratee) && <%' + ' } %>hasOwnProperty.call(iteratee, index)) {\n' + - ' <%= objectLoop %>\n' + + ' <%= loop %>\n' + ' }' + ' <% } %>' + ' <% } %>' + - ' <% if (arrayLoop || nonEnumArgs) { %>\n}<% } %>\n' + + ' <% if (arrays || nonEnumArgs) { %>\n}<% } %>\n' + // add code to the bottom of the iteration function '<%= bottom %>;\n' + @@ -454,9 +454,10 @@ var assignIteratorOptions = { 'args': 'object, source, guard', 'top': - "for (var argsIndex = 1, argsLength = typeof guard == 'number' ? 2 : arguments.length; argsIndex < argsLength; argsIndex++) {\n" + + "var argsIndex = 0, argsLength = typeof guard == 'number' ? 2 : arguments.length;\n" + + 'while (++argsIndex < argsLength) {\n' + ' if ((iteratee = arguments[argsIndex])) {', - 'objectLoop': 'result[index] = iteratee[index]', + 'loop': 'result[index] = iteratee[index]', 'bottom': ' }\n}' }; @@ -464,15 +465,15 @@ * Reusable iterator options shared by `each`, `forIn`, and `forOwn`. */ var eachIteratorOptions = { + 'arrays': true, 'args': 'collection, callback, thisArg', 'top': "callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg)", - 'arrayLoop': 'if (callback(iteratee[index], index, collection) === false) return result', - 'objectLoop': 'if (callback(iteratee[index], index, collection) === false) return result' + 'loop': 'if (callback(iteratee[index], index, collection) === false) return result' }; /** Reusable iterator options for `forIn` and `forOwn` */ var forOwnIteratorOptions = { - 'arrayLoop': null + 'arrays': false }; /*--------------------------------------------------------------------------*/ @@ -648,25 +649,28 @@ * * @private * @param {Object} [options1, options2, ...] The compile options object(s). + * arrays - A boolean to specify support for iterating arrays and array-like objects. * useHas - A boolean to specify using `hasOwnProperty` checks in the object loop. * args - A string of comma separated arguments the iteration function will accept. * top - A string of code to execute before the iteration branches. - * arrayLoop - A string of code to execute in the array loop. - * objectLoop - A string of code to execute in the object loop. + * loop - A string of code to execute in the object loop. * bottom - A string of code to execute after the iteration branches. * * @returns {Function} Returns the compiled function. */ function createIterator() { var data = { - 'arrayLoop': '', - 'bottom': '', + // support properties 'hasDontEnumBug': hasDontEnumBug, 'isKeysFast': isKeysFast, - 'objectLoop': '', 'nonEnumArgs': nonEnumArgs, 'noCharByIndex': noCharByIndex, 'shadowed': shadowed, + + // iterator options + 'arrays': false, + 'bottom': '', + 'loop': '', 'top': '', 'useHas': true }; @@ -1139,7 +1143,7 @@ * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' } */ var defaults = createIterator(assignIteratorOptions, { - 'objectLoop': 'if (result[index] == null) ' + assignIteratorOptions.objectLoop + 'loop': 'if (result[index] == null) ' + assignIteratorOptions.loop }); /**