Simplify iteratorTemplate and support for minifying double quoted strings in source.

Former-commit-id: d18cbd6dc380001fe3617f6891c84a794a13c8d1
This commit is contained in:
John-David Dalton
2012-11-24 01:34:00 -06:00
parent b4f2e9b442
commit fc9c937e67
3 changed files with 37 additions and 36 deletions

View File

@@ -731,7 +731,7 @@
// remove optimized branch in `iteratorTemplate` // remove optimized branch in `iteratorTemplate`
source = source.replace(getIteratorTemplate(source), function(match) { 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` // remove data object property assignment in `createIterator`
@@ -896,7 +896,7 @@
} }
// replace `useStrict` branch in `iteratorTemplate` with hard-coded option // replace `useStrict` branch in `iteratorTemplate` with hard-coded option
source = source.replace(getIteratorTemplate(source), function(match) { source = source.replace(getIteratorTemplate(source), function(match) {
return match.replace(/(?: *\/\/.*\n)*(\s*)' *<%.+?useStrict.+/, value ? "$1'\\'use strict\\';\\n' +" : ''); return match.replace(/(?: *\/\/.*\n)*(\s*)["'] *<%.+?useStrict.+/, value ? '$1"\'use strict\';\\n" +' : '');
}); });
return source; return source;
@@ -1386,8 +1386,8 @@
" source = 'with (' + variable + ' || {}) {\\n' + source + '\\n}\\n';", " source = 'with (' + variable + ' || {}) {\\n' + source + '\\n}\\n';",
' }', ' }',
" source = 'function(' + variable + ') {\\n' +", " source = 'function(' + variable + ') {\\n' +",
" 'var __t, __p = \\'\\', __j = Array.prototype.join;\\n' +", ' "var __t, __p = \'\', __j = Array.prototype.join;\\n" +',
" 'function print() { __p += __j.call(arguments, \\'\\') }\\n' +", ' "function print() { __p += __j.call(arguments, \'\') }\\n" +',
' source +', ' source +',
" 'return __p\\n}';", " 'return __p\\n}';",
'', '',
@@ -1525,7 +1525,7 @@
// remove `prototype` [[Enumerable]] fix from `iteratorTemplate` // remove `prototype` [[Enumerable]] fix from `iteratorTemplate`
source = source.replace(getIteratorTemplate(source), function(match) { source = source.replace(getIteratorTemplate(source), function(match) {
return match return match
.replace(/(?: *\/\/.*\n)* *' *(?:<% *)?if *\(!hasDontEnumBug *(?:&&|\))[\s\S]+?<% *} *(?:%>|').+/g, '') .replace(/(?: *\/\/.*\n)* *["'] *(?:<% *)?if *\(!hasDontEnumBug *(?:&&|\))[\s\S]+?<% *} *(?:%>|["']).+/g, '')
.replace(/!hasDontEnumBug *\|\|/g, ''); .replace(/!hasDontEnumBug *\|\|/g, '');
}); });
} }
@@ -1933,7 +1933,7 @@
'onComplete': function(source) { 'onComplete': function(source) {
// inject "use strict" directive // inject "use strict" directive
if (isStrict) { if (isStrict) {
source = source.replace(/^([\s\S]*?function[^{]+{)([^'"])/, '$1"use strict";$2'); source = source.replace(/^([\s\S]*?function[^{]+{)([^"'])/, '$1"use strict";$2');
} }
if (isCustom) { if (isCustom) {
source = addCommandsToHeader(source, options); source = addCommandsToHeader(source, options);

View File

@@ -236,9 +236,17 @@
source = source.replace("result[length]['value']", 'result[length].value'); source = source.replace("result[length]['value']", 'result[length].value');
// remove whitespace from string literals // remove whitespace from string literals
source = source.replace(/'(?:(?=(\\?))\1.)*?'/g, function(string) { source = source.replace(/^ *"(?:(?=(\\?))\1.)*?"|'(?:(?=(\\?))\2.)*?'/gm, function(string) {
// avoids removing the '\n' of the `stringEscapes` object // avoids removing the '\n' of the `stringEscapes` object
return string.replace(/\[object |delete |else |function | in |return\s+[\w']|throw |typeof |use strict|var |@ |'\\n'|\\\\n|\\n|\s+/g, function(match) { return 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;
});
});
// remove whitespace from string literals in double quotes
source = source.replace(/^ *"(?:(?=(\\?))\1.)*?"/g, function(string) {
// avoids removing the '\n' of the `stringEscapes` object
return string.replace(reWhitespace, function(match) {
return match == false || match == '\\n' ? '' : match; return match == false || match == '\\n' ? '' : match;
}); });
}); });
@@ -256,11 +264,8 @@
// remove newline from double-quoted strings in `_.template` // remove newline from double-quoted strings in `_.template`
source = source source = source
.replace('"\';\\n__with ("', '"\';__with("') .replace('"__p += \'"', '"__p+=\'"')
.replace('"\\n}__\\n__p += \'"', '"}____p+=\'"') .replace('"\';\n"', '"\';"')
.replace('"__p = \'"', '"__p=\'"')
.replace('"\';\\n"', '"\';"')
.replace("') {\\n'", "'){'")
// remove `useSourceURL` variable // 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/, '');

View File

@@ -351,10 +351,10 @@
*/ */
var iteratorTemplate = template( var iteratorTemplate = template(
// conditional strict mode // conditional strict mode
'<% if (obj.useStrict) { %>\'use strict\';\n<% } %>' + "<% if (obj.useStrict) { %>'use strict';\n<% } %>" +
// the `iteratee` may be reassigned by the `top` snippet // the `iteratee` may be reassigned by the `top` snippet
'var index, value, iteratee = <%= firstArg %>, ' + 'var index, iteratee = <%= firstArg %>, ' +
// assign the `result` variable an initial value // assign the `result` variable an initial value
'result = <%= firstArg %>;\n' + 'result = <%= firstArg %>;\n' +
// exit early if the first argument is falsey // exit early if the first argument is falsey
@@ -365,18 +365,17 @@
// array-like iteration: // array-like iteration:
'<% if (arrayLoop) { %>' + '<% if (arrayLoop) { %>' +
'var length = iteratee.length; index = -1;\n' + 'var length = iteratee.length; index = -1;\n' +
'if (typeof length == \'number\') {' + "if (typeof length == 'number') {" +
// add support for accessing string characters by index if needed // add support for accessing string characters by index if needed
' <% if (noCharByIndex) { %>\n' + ' <% if (noCharByIndex) { %>\n' +
' if (isString(iteratee)) {\n' + ' if (isString(iteratee)) {\n' +
' iteratee = iteratee.split(\'\')\n' + " iteratee = iteratee.split('')\n" +
' }' + ' }' +
' <% } %>\n' + ' <% } %>\n' +
// iterate over the array-like value // iterate over the array-like value
' while (++index < length) {\n' + ' while (++index < length) {\n' +
' value = iteratee[index];\n' +
' <%= arrayLoop %>\n' + ' <%= arrayLoop %>\n' +
' }\n' + ' }\n' +
'}\n' + '}\n' +
@@ -388,7 +387,7 @@
' var length = iteratee.length; index = -1;\n' + ' var length = iteratee.length; index = -1;\n' +
' if (length && isArguments(iteratee)) {\n' + ' if (length && isArguments(iteratee)) {\n' +
' while (++index < length) {\n' + ' while (++index < length) {\n' +
' value = iteratee[index += \'\'];\n' + " index += '';\n" +
' <%= objectLoop %>\n' + ' <%= objectLoop %>\n' +
' }\n' + ' }\n' +
' } else {' + ' } else {' +
@@ -401,8 +400,8 @@
// the the `prototype` property of functions regardless of its // the the `prototype` property of functions regardless of its
// [[Enumerable]] value. // [[Enumerable]] value.
' <% if (!hasDontEnumBug) { %>\n' + ' <% if (!hasDontEnumBug) { %>\n' +
' var skipProto = typeof iteratee == \'function\' && \n' + " var skipProto = typeof iteratee == 'function' && \n" +
' propertyIsEnumerable.call(iteratee, \'prototype\');\n' + " propertyIsEnumerable.call(iteratee, 'prototype');\n" +
' <% } %>' + ' <% } %>' +
// iterate own properties using `Object.keys` if it's fast // iterate own properties using `Object.keys` if it's fast
@@ -412,8 +411,7 @@
' length = ownProps.length;\n\n' + ' length = ownProps.length;\n\n' +
' while (++ownIndex < length) {\n' + ' while (++ownIndex < length) {\n' +
' index = ownProps[ownIndex];\n' + ' index = ownProps[ownIndex];\n' +
' <% if (!hasDontEnumBug) { %>if (!(skipProto && index == \'prototype\')) {\n <% } %>' + " <% if (!hasDontEnumBug) { %>if (!(skipProto && index == 'prototype')) {\n <% } %>" +
' value = iteratee[index];\n' +
' <%= objectLoop %>\n' + ' <%= objectLoop %>\n' +
' <% if (!hasDontEnumBug) { %>}\n<% } %>' + ' <% if (!hasDontEnumBug) { %>}\n<% } %>' +
' }' + ' }' +
@@ -422,12 +420,11 @@
' <% } else { %>\n' + ' <% } else { %>\n' +
' for (index in iteratee) {<%' + ' for (index in iteratee) {<%' +
' if (!hasDontEnumBug || useHas) { %>\n if (<%' + ' if (!hasDontEnumBug || useHas) { %>\n if (<%' +
' if (!hasDontEnumBug) { %>!(skipProto && index == \'prototype\')<% }' + " if (!hasDontEnumBug) { %>!(skipProto && index == 'prototype')<% }" +
' if (!hasDontEnumBug && useHas) { %> && <% }' + ' if (!hasDontEnumBug && useHas) { %> && <% }' +
' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' + ' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' +
' %>) {' + ' %>) {' +
' <% } %>\n' + ' <% } %>\n' +
' value = iteratee[index];\n' +
' <%= objectLoop %>;' + ' <%= objectLoop %>;' +
' <% if (!hasDontEnumBug || useHas) { %>\n }<% } %>\n' + ' <% if (!hasDontEnumBug || useHas) { %>\n }<% } %>\n' +
' }' + ' }' +
@@ -440,12 +437,11 @@
' <% if (hasDontEnumBug) { %>\n\n' + ' <% if (hasDontEnumBug) { %>\n\n' +
' var ctor = iteratee.constructor;\n' + ' var ctor = iteratee.constructor;\n' +
' <% for (var k = 0; k < 7; k++) { %>\n' + ' <% for (var k = 0; k < 7; k++) { %>\n' +
' index = \'<%= shadowed[k] %>\';\n' + " index = '<%= shadowed[k] %>';\n" +
' if (<%' + ' if (<%' +
' if (shadowed[k] == \'constructor\') {' + " if (shadowed[k] == 'constructor') {" +
' %>!(ctor && ctor.prototype === iteratee) && <%' + ' %>!(ctor && ctor.prototype === iteratee) && <%' +
' } %>hasOwnProperty.call(iteratee, index)) {\n' + ' } %>hasOwnProperty.call(iteratee, index)) {\n' +
' value = iteratee[index];\n' +
' <%= objectLoop %>\n' + ' <%= objectLoop %>\n' +
' }' + ' }' +
' <% } %>' + ' <% } %>' +
@@ -462,9 +458,9 @@
var assignIteratorOptions = { var assignIteratorOptions = {
'args': 'object, source, guard', 'args': 'object, source, guard',
'top': 'top':
'for (var argsIndex = 1, argsLength = typeof guard == \'number\' ? 2 : arguments.length; argsIndex < argsLength; argsIndex++) {\n' + "for (var argsIndex = 1, argsLength = typeof guard == 'number' ? 2 : arguments.length; argsIndex < argsLength; argsIndex++) {\n" +
' if ((iteratee = arguments[argsIndex])) {', ' if ((iteratee = arguments[argsIndex])) {',
'objectLoop': 'result[index] = value', 'objectLoop': 'result[index] = iteratee[index]',
'bottom': ' }\n}' 'bottom': ' }\n}'
}; };
@@ -474,8 +470,8 @@
var forEachIteratorOptions = { var forEachIteratorOptions = {
'args': 'collection, callback, thisArg', 'args': 'collection, callback, thisArg',
'top': 'callback = createCallback(callback, thisArg)', 'top': 'callback = createCallback(callback, thisArg)',
'arrayLoop': 'if (callback(value, index, collection) === false) return result', 'arrayLoop': 'if (callback(iteratee[index], index, collection) === false) return result',
'objectLoop': 'if (callback(value, index, collection) === false) return result' 'objectLoop': 'if (callback(iteratee[index], index, collection) === false) return result'
}; };
/** Reusable iterator options for `forIn` and `forOwn` */ /** Reusable iterator options for `forIn` and `forOwn` */
@@ -3629,7 +3625,7 @@
* @example * @example
* *
* _.escape('Moe, Larry & Curly'); * _.escape('Moe, Larry & Curly');
* // => "Moe, Larry &amp; Curly" * // => 'Moe, Larry &amp; Curly'
*/ */
function escape(string) { function escape(string) {
return string == null ? '' : (string + '').replace(reUnescapedHtml, escapeHtmlChar); return string == null ? '' : (string + '').replace(reUnescapedHtml, escapeHtmlChar);
@@ -3917,10 +3913,10 @@
// frame code as the function body // frame code as the function body
source = 'function(' + variable + ') {\n' + source = 'function(' + variable + ') {\n' +
(hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') + (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
'var __t, __p = \'\', __e = _.escape' + "var __t, __p = '', __e = _.escape" +
(isEvaluating (isEvaluating
? ', __j = Array.prototype.join;\n' + ? ', __j = Array.prototype.join;\n' +
'function print() { __p += __j.call(arguments, \'\') }\n' "function print() { __p += __j.call(arguments, '') }\n"
: (hasVariable ? '' : ', __d = ' + variable + '.' + variable + ' || ' + variable) + ';\n' : (hasVariable ? '' : ', __d = ' + variable + '.' + variable + ' || ' + variable) + ';\n'
) + ) +
source + source +
@@ -3996,7 +3992,7 @@
* @example * @example
* *
* _.unescape('Moe, Larry &amp; Curly'); * _.unescape('Moe, Larry &amp; Curly');
* // => "Moe, Larry & Curly" * // => 'Moe, Larry & Curly'
*/ */
function unescape(string) { function unescape(string) {
return string == null ? '' : (string + '').replace(reEscapedHtml, unescapeHtmlChar); return string == null ? '' : (string + '').replace(reEscapedHtml, unescapeHtmlChar);