Fix build indenting.

Former-commit-id: 87a7793799ee3ed28fe67cb894aa2876be103c50
This commit is contained in:
John-David Dalton
2013-02-22 02:00:42 -08:00
parent 860339b90e
commit 4136f1b377
2 changed files with 396 additions and 361 deletions

744
build.js
View File

@@ -275,66 +275,68 @@
function addChainMethods(source) { function addChainMethods(source) {
// add `_.chain` // add `_.chain`
source = source.replace(matchFunction(source, 'tap'), function(match) { source = source.replace(matchFunction(source, 'tap'), function(match) {
return [ var indent = getIndent(match);
return indent + [
'', '',
' /**', '/**',
' * Creates a `lodash` object that wraps the given `value`.', ' * Creates a `lodash` object that wraps the given `value`.',
' *', ' *',
' * @static', ' * @static',
' * @memberOf _', ' * @memberOf _',
' * @category Chaining', ' * @category Chaining',
' * @param {Mixed} value The value to wrap.', ' * @param {Mixed} value The value to wrap.',
' * @returns {Object} Returns the wrapper object.', ' * @returns {Object} Returns the wrapper object.',
' * @example', ' * @example',
' *', ' *',
' * var stooges = [', ' * var stooges = [',
" * { 'name': 'moe', 'age': 40 },", " * { 'name': 'moe', 'age': 40 },",
" * { 'name': 'larry', 'age': 50 },", " * { 'name': 'larry', 'age': 50 },",
" * { 'name': 'curly', 'age': 60 }", " * { 'name': 'curly', 'age': 60 }",
' * ];', ' * ];',
' *', ' *',
' * var youngest = _.chain(stooges)', ' * var youngest = _.chain(stooges)',
' * .sortBy(function(stooge) { return stooge.age; })', ' * .sortBy(function(stooge) { return stooge.age; })',
" * .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })", " * .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })",
' * .first();', ' * .first();',
" * // => 'moe is 40'", " * // => 'moe is 40'",
' */', ' */',
' function chain(value) {', 'function chain(value) {',
' value = new lodash(value);', ' value = new lodash(value);',
' value.__chain__ = true;', ' value.__chain__ = true;',
' return value;', ' return value;',
' }', '}',
'', '',
match match
].join('\n'); ].join('\n' + indent);
}); });
// add `wrapperChain` // add `wrapperChain`
source = source.replace(matchFunction(source, 'wrapperToString'), function(match) { source = source.replace(matchFunction(source, 'wrapperToString'), function(match) {
return [ var indent = getIndent(match);
return indent + [
'', '',
' /**', '/**',
' * Enables method chaining on the wrapper object.', ' * Enables method chaining on the wrapper object.',
' *', ' *',
' * @name chain', ' * @name chain',
' * @memberOf _', ' * @memberOf _',
' * @category Chaining', ' * @category Chaining',
' * @returns {Mixed} Returns the wrapper object.', ' * @returns {Mixed} Returns the wrapper object.',
' * @example', ' * @example',
' *', ' *',
' * var sum = _([1, 2, 3])', ' * var sum = _([1, 2, 3])',
' * .chain()', ' * .chain()',
' * .reduce(function(sum, num) { return sum + num; })', ' * .reduce(function(sum, num) { return sum + num; })',
' * .value()', ' * .value()',
' * // => 6`', ' * // => 6`',
' */', ' */',
' function wrapperChain() {', 'function wrapperChain() {',
' this.__chain__ = true;', ' this.__chain__ = true;',
' return this;', ' return this;',
' }', '}',
'', '',
match match
].join('\n'); ].join('\n' + indent);
}); });
// add `lodash.chain` assignment // add `lodash.chain` assignment
@@ -354,7 +356,7 @@
// move `mixin(lodash)` to after the method assignments // move `mixin(lodash)` to after the method assignments
source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(lodash\).+/, ''); source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(lodash\).+/, '');
source = source.replace(getMethodAssignments(source), function(match) { source = source.replace(getMethodAssignments(source), function(match) {
var indent = /^ *(?=lodash)/m.exec(match)[0]; var indent = /^ *(?=lodash\.)/m.exec(match)[0];
return match + [ return match + [
'', '',
'', '',
@@ -365,8 +367,8 @@
// add `__chain__` checks to `_.mixin` // add `__chain__` checks to `_.mixin`
source = source.replace(matchFunction(source, 'mixin'), function(match) { source = source.replace(matchFunction(source, 'mixin'), function(match) {
return match.replace(/^( *)return new lodash.+/m, function() { return match.replace(/^ *return new lodash.+/m, function() {
var indent = arguments[1]; var indent = getIndent(match);
return indent + [ return indent + [
'', '',
'var result = func.apply(lodash, args);', 'var result = func.apply(lodash, args);',
@@ -380,8 +382,8 @@
}); });
// replace wrapper `Array` method assignments // replace wrapper `Array` method assignments
source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function() { source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function(match) {
var indent = arguments[1]; var indent = getIndent(match);
return indent + [ return indent + [
'// add `Array` mutator functions to the wrapper', '// add `Array` mutator functions to the wrapper',
"each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {", "each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {",
@@ -705,20 +707,35 @@
* *
* @private * @private
* @param {Function} func The function to process. * @param {Function} func The function to process.
* @param {String} indent The function indent.
* @returns {String} Returns the formatted source. * @returns {String} Returns the formatted source.
*/ */
function getFunctionSource(func) { function getFunctionSource(func, indent) {
var source = func.source || (func + ''); var source = func.source || (func + '');
if (indent == null) {
indent = ' ';
}
// format leading whitespace // format leading whitespace
return source.replace(/\n(?:.*)/g, function(match, index) { return source.replace(/\n(?:.*)/g, function(match, index) {
match = match.slice(1); match = match.slice(1);
return ( return (
match == '}' && source.indexOf('}', index + 2) < 0 ? '\n ' : '\n ' '\n' + indent +
(match == '}' && source.indexOf('}', index + 2) < 0 ? '' : ' ')
) + match; ) + match;
}); });
} }
/**
* Gets the indent of the given function.
*
* @private
* @param {Function} func The function to process.
* @returns {String} Returns the indent.
*/
function getIndent(func) {
return /^ *(?=\S)/m.exec(func.source || func)[0];
}
/** /**
* Gets the `_.isArguments` fallback from `source`. * Gets the `_.isArguments` fallback from `source`.
* *
@@ -907,7 +924,9 @@
if ( snippet) { if ( snippet) {
// remove data object property assignment // remove data object property assignment
var modified = snippet.replace(RegExp("^ *'" + varName + "': *" + varName + '.+\\n', 'm'), ''); var modified = snippet.replace(RegExp("^ *'" + varName + "': *" + varName + '.+\\n', 'm'), '');
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
// clip at the `factory` assignment // clip at the `factory` assignment
snippet = modified.match(/Function\([\s\S]+$/)[0]; snippet = modified.match(/Function\([\s\S]+$/)[0];
@@ -917,7 +936,9 @@
.replace(/, *',/, "',") .replace(/, *',/, "',")
.replace(/,\s*\)/, ')') .replace(/,\s*\)/, ')')
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
} }
return source; return source;
} }
@@ -960,7 +981,9 @@
}, snippet); }, snippet);
// replace with the modified snippet // replace with the modified snippet
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
return removeFromCreateIterator(source, funcName); return removeFromCreateIterator(source, funcName);
} }
@@ -1265,7 +1288,9 @@
// clip snippet after the JSDoc comment block // clip snippet after the JSDoc comment block
match = match.replace(/^\s*(?:\/\/.*|\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)\n/, ''); match = match.replace(/^\s*(?:\/\/.*|\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)\n/, '');
source = source.replace(match, function() { source = source.replace(match, function() {
return funcValue.trimRight() + '\n'; return funcValue
.replace(RegExp('^' + getIndent(funcValue), 'gm'), getIndent(match))
.trimRight() + '\n';
}); });
} }
return source; return source;
@@ -1670,405 +1695,405 @@
// replace `_.assign` // replace `_.assign`
source = replaceFunction(source, 'assign', [ source = replaceFunction(source, 'assign', [
' function assign(object) {', 'function assign(object) {',
' if (!object) {', ' if (!object) {',
' return object;', ' return object;',
' }', ' }',
' for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {', ' for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {',
' var iterable = arguments[argsIndex];', ' var iterable = arguments[argsIndex];',
' if (iterable) {', ' if (iterable) {',
' for (var key in iterable) {', ' for (var key in iterable) {',
' object[key] = iterable[key];', ' object[key] = iterable[key];',
' }',
' }', ' }',
' }', ' }',
' return object;', ' }',
' }' ' return object;',
'}'
].join('\n')); ].join('\n'));
// replace `_.clone` // replace `_.clone`
if (useUnderscoreClone) { if (useUnderscoreClone) {
source = replaceFunction(source, 'clone', [ source = replaceFunction(source, 'clone', [
' function clone(value) {', 'function clone(value) {',
' return isObject(value)', ' return isObject(value)',
' ? (isArray(value) ? slice(value) : assign({}, value))', ' ? (isArray(value) ? slice(value) : assign({}, value))',
' : value', ' : value',
' }' '}'
].join('\n')); ].join('\n'));
} }
// replace `_.contains` // replace `_.contains`
source = replaceFunction(source, 'contains', [ source = replaceFunction(source, 'contains', [
' function contains(collection, target) {', 'function contains(collection, target) {',
' var length = collection ? collection.length : 0,', ' var length = collection ? collection.length : 0,',
' result = false;', ' result = false;',
" if (typeof length == 'number') {", " if (typeof length == 'number') {",
' result = indexOf(collection, target) > -1;', ' result = indexOf(collection, target) > -1;',
' } else {', ' } else {',
' each(collection, function(value) {', ' each(collection, function(value) {',
' return (result = value === target) && indicatorObject;', ' return (result = value === target) && indicatorObject;',
' });', ' });',
' }', ' }',
' return result;', ' return result;',
' }' '}'
].join('\n')); ].join('\n'));
// replace `_.defaults` // replace `_.defaults`
source = replaceFunction(source, 'defaults', [ source = replaceFunction(source, 'defaults', [
' function defaults(object) {', 'function defaults(object) {',
' if (!object) {', ' if (!object) {',
' return object;', ' return object;',
' }', ' }',
' for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {', ' for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {',
' var iterable = arguments[argsIndex];', ' var iterable = arguments[argsIndex];',
' if (iterable) {', ' if (iterable) {',
' for (var key in iterable) {', ' for (var key in iterable) {',
' if (object[key] == null) {', ' if (object[key] == null) {',
' object[key] = iterable[key];', ' object[key] = iterable[key];',
' }',
' }', ' }',
' }', ' }',
' }', ' }',
' return object;', ' }',
' }' ' return object;',
'}'
].join('\n')); ].join('\n'));
// replace `_.difference` // replace `_.difference`
source = replaceFunction(source, 'difference', [ source = replaceFunction(source, 'difference', [
' function difference(array) {', 'function difference(array) {',
' var index = -1,', ' var index = -1,',
' length = array.length,', ' length = array.length,',
' flattened = concat.apply(arrayRef, arguments),', ' flattened = concat.apply(arrayRef, arguments),',
' result = [];', ' result = [];',
'', '',
' while (++index < length) {', ' while (++index < length) {',
' var value = array[index]', ' var value = array[index]',
' if (indexOf(flattened, value, length) < 0) {', ' if (indexOf(flattened, value, length) < 0) {',
' result.push(value);', ' result.push(value);',
' }',
' }', ' }',
' return result', ' }',
' }' ' return result',
'}'
].join('\n')); ].join('\n'));
// replace `_.intersection` // replace `_.intersection`
source = replaceFunction(source, 'intersection', [ source = replaceFunction(source, 'intersection', [
' function intersection(array) {', 'function intersection(array) {',
' var args = arguments,', ' var args = arguments,',
' argsLength = args.length,', ' argsLength = args.length,',
' index = -1,', ' index = -1,',
' length = array ? array.length : 0,', ' length = array ? array.length : 0,',
' result = [];', ' result = [];',
'', '',
' outer:', ' outer:',
' while (++index < length) {', ' while (++index < length) {',
' var value = array[index];', ' var value = array[index];',
' if (indexOf(result, value) < 0) {', ' if (indexOf(result, value) < 0) {',
' var argsIndex = argsLength;', ' var argsIndex = argsLength;',
' while (--argsIndex) {', ' while (--argsIndex) {',
' if (indexOf(args[argsIndex], value) < 0) {', ' if (indexOf(args[argsIndex], value) < 0) {',
' continue outer;', ' continue outer;',
' }',
' }', ' }',
' result.push(value);',
' }', ' }',
' result.push(value);',
' }', ' }',
' return result;', ' }',
' }' ' return result;',
'}'
].join('\n')); ].join('\n'));
// replace `_.isEmpty` // replace `_.isEmpty`
source = replaceFunction(source, 'isEmpty', [ source = replaceFunction(source, 'isEmpty', [
' function isEmpty(value) {', 'function isEmpty(value) {',
' if (!value) {', ' if (!value) {',
' return true;',
' }',
' if (isArray(value) || isString(value)) {',
' return !value.length;',
' }',
' for (var key in value) {',
' if (hasOwnProperty.call(value, key)) {',
' return false;',
' }',
' }',
' return true;', ' return true;',
' }' ' }',
' if (isArray(value) || isString(value)) {',
' return !value.length;',
' }',
' for (var key in value) {',
' if (hasOwnProperty.call(value, key)) {',
' return false;',
' }',
' }',
' return true;',
'}'
].join('\n')); ].join('\n'));
// replace `_.isEqual` // replace `_.isEqual`
source = replaceFunction(source, 'isEqual', [ source = replaceFunction(source, 'isEqual', [
' function isEqual(a, b, stackA, stackB) {', 'function isEqual(a, b, stackA, stackB) {',
' if (a === b) {', ' if (a === b) {',
' return a !== 0 || (1 / a == 1 / b);', ' return a !== 0 || (1 / a == 1 / b);',
' }', ' }',
' var type = typeof a,', ' var type = typeof a,',
' otherType = typeof b;', ' otherType = typeof b;',
'', '',
' if (a === a &&', ' if (a === a &&',
" (!a || (type != 'function' && type != 'object')) &&", " (!a || (type != 'function' && type != 'object')) &&",
" (!b || (otherType != 'function' && otherType != 'object'))) {", " (!b || (otherType != 'function' && otherType != 'object'))) {",
' return false;',
' }',
' if (a == null || b == null) {',
' return a === b;',
' }',
' var className = toString.call(a),',
' otherClass = toString.call(b);',
'',
' if (className != otherClass) {',
' return false;',
' }',
' switch (className) {',
' case boolClass:',
' case dateClass:',
' return +a == +b;',
'',
' case numberClass:',
' return a != +a',
' ? b != +b',
' : (a == 0 ? (1 / a == 1 / b) : a == +b);',
'',
' case regexpClass:',
' case stringClass:',
" return a == b + '';",
' }',
' var isArr = className == arrayClass;',
' if (!isArr) {',
' if (a.__wrapped__ || b.__wrapped__) {',
' return isEqual(a.__wrapped__ || a, b.__wrapped__ || b, stackA, stackB);',
' }',
' if (className != objectClass) {',
' return false;', ' return false;',
' }', ' }',
' if (a == null || b == null) {', ' var ctorA = a.constructor,',
' return a === b;', ' ctorB = b.constructor;',
' }',
' var className = toString.call(a),',
' otherClass = toString.call(b);',
'', '',
' if (className != otherClass) {', ' if (ctorA != ctorB && !(',
' isFunction(ctorA) && ctorA instanceof ctorA &&',
' isFunction(ctorB) && ctorB instanceof ctorB',
' )) {',
' return false;', ' return false;',
' }', ' }',
' switch (className) {', ' }',
' case boolClass:', ' stackA || (stackA = []);',
' case dateClass:', ' stackB || (stackB = []);',
' return +a == +b;',
'', '',
' case numberClass:', ' var length = stackA.length;',
' return a != +a', ' while (length--) {',
' ? b != +b', ' if (stackA[length] == a) {',
' : (a == 0 ? (1 / a == 1 / b) : a == +b);', ' return stackB[length] == b;',
'',
' case regexpClass:',
' case stringClass:',
" return a == b + '';",
' }', ' }',
' var isArr = className == arrayClass;', ' }',
' if (!isArr) {', ' var result = true,',
' if (a.__wrapped__ || b.__wrapped__) {', ' size = 0;',
' return isEqual(a.__wrapped__ || a, b.__wrapped__ || b, stackA, stackB);',
' }',
' if (className != objectClass) {',
' return false;',
' }',
' var ctorA = a.constructor,',
' ctorB = b.constructor;',
'', '',
' if (ctorA != ctorB && !(', ' stackA.push(a);',
' isFunction(ctorA) && ctorA instanceof ctorA &&', ' stackB.push(b);',
' isFunction(ctorB) && ctorB instanceof ctorB',
' )) {',
' return false;',
' }',
' }',
' stackA || (stackA = []);',
' stackB || (stackB = []);',
'', '',
' var length = stackA.length;', ' if (isArr) {',
' while (length--) {', ' size = b.length;',
' if (stackA[length] == a) {', ' result = size == a.length;',
' return stackB[length] == b;',
' }',
' }',
' var result = true,',
' size = 0;',
'',
' stackA.push(a);',
' stackB.push(b);',
'',
' if (isArr) {',
' size = b.length;',
' result = size == a.length;',
'',
' if (result) {',
' while (size--) {',
' if (!(result = isEqual(a[size], b[size], stackA, stackB))) {',
' break;',
' }',
' }',
' }',
' return result;',
' }',
' forIn(b, function(value, key, b) {',
' if (hasOwnProperty.call(b, key)) {',
' size++;',
' return !(result = hasOwnProperty.call(a, key) && isEqual(a[key], value, stackA, stackB)) && indicatorObject;',
' }',
' });',
'', '',
' if (result) {', ' if (result) {',
' forIn(a, function(value, key, a) {', ' while (size--) {',
' if (hasOwnProperty.call(a, key)) {', ' if (!(result = isEqual(a[size], b[size], stackA, stackB))) {',
' return !(result = --size > -1) && indicatorObject;', ' break;',
' }', ' }',
' });', ' }',
' }', ' }',
' return result;', ' return result;',
' }' ' }',
' forIn(b, function(value, key, b) {',
' if (hasOwnProperty.call(b, key)) {',
' size++;',
' return !(result = hasOwnProperty.call(a, key) && isEqual(a[key], value, stackA, stackB)) && indicatorObject;',
' }',
' });',
'',
' if (result) {',
' forIn(a, function(value, key, a) {',
' if (hasOwnProperty.call(a, key)) {',
' return !(result = --size > -1) && indicatorObject;',
' }',
' });',
' }',
' return result;',
'}'
].join('\n')); ].join('\n'));
// replace `_.omit` // replace `_.omit`
source = replaceFunction(source, 'omit', [ source = replaceFunction(source, 'omit', [
' function omit(object) {', 'function omit(object) {',
' var props = concat.apply(arrayRef, arguments),', ' var props = concat.apply(arrayRef, arguments),',
' result = {};', ' result = {};',
'', '',
' forIn(object, function(value, key) {', ' forIn(object, function(value, key) {',
' if (indexOf(props, key, 1) < 0) {', ' if (indexOf(props, key, 1) < 0) {',
' result[key] = value;', ' result[key] = value;',
' }', ' }',
' });', ' });',
' return result;', ' return result;',
' }' '}'
].join('\n')); ].join('\n'));
// replace `_.pick` // replace `_.pick`
source = replaceFunction(source, 'pick', [ source = replaceFunction(source, 'pick', [
' function pick(object) {', 'function pick(object) {',
' var index = 0,', ' var index = 0,',
' props = concat.apply(arrayRef, arguments),', ' props = concat.apply(arrayRef, arguments),',
' length = props.length,', ' length = props.length,',
' result = {};', ' result = {};',
'', '',
' while (++index < length) {', ' while (++index < length) {',
' var prop = props[index];', ' var prop = props[index];',
' if (prop in object) {', ' if (prop in object) {',
' result[prop] = object[prop];', ' result[prop] = object[prop];',
' }',
' }', ' }',
' return result;', ' }',
' }' ' return result;',
'}'
].join('\n')); ].join('\n'));
// replace `_.result` // replace `_.result`
source = replaceFunction(source, 'result', [ source = replaceFunction(source, 'result', [
' function result(object, property) {', 'function result(object, property) {',
' var value = object ? object[property] : null;', ' var value = object ? object[property] : null;',
' return isFunction(value) ? object[property]() : value;', ' return isFunction(value) ? object[property]() : value;',
' }' '}'
].join('\n')); ].join('\n'));
// replace `_.template` // replace `_.template`
source = replaceFunction(source, 'template', [ source = replaceFunction(source, 'template', [
' function template(text, data, options) {', 'function template(text, data, options) {',
" text || (text = '');", " text || (text = '');",
' options = defaults({}, options, lodash.templateSettings);', ' options = defaults({}, options, lodash.templateSettings);',
'', '',
' var index = 0,', ' var index = 0,',
' source = "__p += \'",', ' source = "__p += \'",',
' variable = options.variable;', ' variable = options.variable;',
'', '',
' var reDelimiters = RegExp(', ' var reDelimiters = RegExp(',
" (options.escape || reNoMatch).source + '|' +", " (options.escape || reNoMatch).source + '|' +",
" (options.interpolate || reNoMatch).source + '|' +", " (options.interpolate || reNoMatch).source + '|' +",
" (options.evaluate || reNoMatch).source + '|$'", " (options.evaluate || reNoMatch).source + '|$'",
" , 'g');", " , 'g');",
'', '',
' text.replace(reDelimiters, function(match, escapeValue, interpolateValue, evaluateValue, offset) {', ' text.replace(reDelimiters, function(match, escapeValue, interpolateValue, evaluateValue, offset) {',
' source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);', ' source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);',
' if (escapeValue) {', ' if (escapeValue) {',
' source += "\' +\\n_.escape(" + escapeValue + ") +\\n\'";', ' source += "\' +\\n_.escape(" + escapeValue + ") +\\n\'";',
' }',
' if (evaluateValue) {',
' source += "\';\\n" + evaluateValue + ";\\n__p += \'";',
' }',
' if (interpolateValue) {',
' source += "\' +\\n((__t = (" + interpolateValue + ")) == null ? \'\' : __t) +\\n\'";',
' }',
' index = offset + match.length;',
' return match;',
' });',
'',
' source += "\';\\n";',
' if (!variable) {',
" variable = 'obj';",
" source = 'with (' + variable + ' || {}) {\\n' + source + '\\n}\\n';",
' }', ' }',
" source = 'function(' + variable + ') {\\n' +", ' if (evaluateValue) {',
' "var __t, __p = \'\', __j = Array.prototype.join;\\n" +', ' source += "\';\\n" + evaluateValue + ";\\n__p += \'";',
' "function print() { __p += __j.call(arguments, \'\') }\\n" +', ' }',
' source +', ' if (interpolateValue) {',
" 'return __p\\n}';", ' source += "\' +\\n((__t = (" + interpolateValue + ")) == null ? \'\' : __t) +\\n\'";',
' }',
' index = offset + match.length;',
' return match;',
' });',
'', '',
' try {', ' source += "\';\\n";',
" var result = Function('_', 'return ' + source)(lodash);", ' if (!variable) {',
' } catch(e) {', " variable = 'obj';",
' e.source = source;', " source = 'with (' + variable + ' || {}) {\\n' + source + '\\n}\\n';",
' throw e;', ' }',
' }', " source = 'function(' + variable + ') {\\n' +",
' if (data) {', ' "var __t, __p = \'\', __j = Array.prototype.join;\\n" +',
' return result(data);', ' "function print() { __p += __j.call(arguments, \'\') }\\n" +',
' }', ' source +',
' result.source = source;', " 'return __p\\n}';",
' return result;', '',
' }' ' try {',
" var result = Function('_', 'return ' + source)(lodash);",
' } catch(e) {',
' e.source = source;',
' throw e;',
' }',
' if (data) {',
' return result(data);',
' }',
' result.source = source;',
' return result;',
'}'
].join('\n')); ].join('\n'));
// replace `_.uniq` // replace `_.uniq`
source = replaceFunction(source, 'uniq', [ source = replaceFunction(source, 'uniq', [
' function uniq(array, isSorted, callback, thisArg) {', 'function uniq(array, isSorted, callback, thisArg) {',
' var index = -1,', ' var index = -1,',
' length = array ? array.length : 0,', ' length = array ? array.length : 0,',
' result = [],', ' result = [],',
' seen = result;', ' seen = result;',
'', '',
" if (typeof isSorted == 'function') {", " if (typeof isSorted == 'function') {",
' thisArg = callback;', ' thisArg = callback;',
' callback = isSorted;', ' callback = isSorted;',
' isSorted = false;', ' isSorted = false;',
' }', ' }',
' if (callback) {', ' if (callback) {',
' seen = [];', ' seen = [];',
' callback = createCallback(callback, thisArg);', ' callback = createCallback(callback, thisArg);',
' }', ' }',
' while (++index < length) {', ' while (++index < length) {',
' var value = array[index],', ' var value = array[index],',
' computed = callback ? callback(value, index, array) : value;', ' computed = callback ? callback(value, index, array) : value;',
'', '',
' if (isSorted', ' if (isSorted',
' ? !index || seen[seen.length - 1] !== computed', ' ? !index || seen[seen.length - 1] !== computed',
' : indexOf(seen, computed) < 0', ' : indexOf(seen, computed) < 0',
' ) {', ' ) {',
' if (callback) {', ' if (callback) {',
' seen.push(computed);', ' seen.push(computed);',
' }',
' result.push(value);',
' }', ' }',
' result.push(value);',
' }', ' }',
' return result;', ' }',
' }' ' return result;',
'}'
].join('\n')); ].join('\n'));
// replace `_.uniqueId` // replace `_.uniqueId`
source = replaceFunction(source, 'uniqueId', [ source = replaceFunction(source, 'uniqueId', [
' function uniqueId(prefix) {', 'function uniqueId(prefix) {',
" var id = ++idCounter + '';", " var id = ++idCounter + '';",
' return prefix ? prefix + id : id;', ' return prefix ? prefix + id : id;',
' }' '}'
].join('\n')); ].join('\n'));
// replace `_.where` // replace `_.where`
source = replaceFunction(source, 'where', [ source = replaceFunction(source, 'where', [
' function where(collection, properties, first) {', 'function where(collection, properties, first) {',
' return (first && isEmpty(properties))', ' return (first && isEmpty(properties))',
' ? null', ' ? null',
' : (first ? find : filter)(collection, properties);', ' : (first ? find : filter)(collection, properties);',
' }' '}'
].join('\n')); ].join('\n'));
// replace `_.without` // replace `_.without`
source = replaceFunction(source, 'without', [ source = replaceFunction(source, 'without', [
' function without(array) {', 'function without(array) {',
' var index = -1,', ' var index = -1,',
' length = array.length,', ' length = array.length,',
' result = [];', ' result = [];',
'', '',
' while (++index < length) {', ' while (++index < length) {',
' var value = array[index]', ' var value = array[index]',
' if (indexOf(arguments, value, 1) < 0) {', ' if (indexOf(arguments, value, 1) < 0) {',
' result.push(value);', ' result.push(value);',
' }',
' }', ' }',
' return result', ' }',
' }' ' return result',
'}'
].join('\n')); ].join('\n'));
// add `_.findWhere` // add `_.findWhere`
source = source.replace(matchFunction(source, 'find'), function (match) { source = source.replace(matchFunction(source, 'find'), function (match) {
return match + [ return match + [
'', '',
' function findWhere(object, properties) {', 'function findWhere(object, properties) {',
' return where(object, properties, true);', ' return where(object, properties, true);',
' }', '}',
'' ''
].join('\n') ].join('\n')
}); });
@@ -2198,11 +2223,11 @@
// inline all functions defined with `createIterator` // inline all functions defined with `createIterator`
_.functions(lodash).forEach(function(methodName) { _.functions(lodash).forEach(function(methodName) {
// strip leading underscores to match pseudo private functions // strip leading underscores to match pseudo private functions
var reFunc = RegExp('(\\bvar ' + methodName.replace(/^_/, '') + ' *= *)createIterator\\(((?:{|[a-zA-Z])[\\s\\S]+?)\\);\\n'); var reFunc = RegExp('(^ *var ' + methodName.replace(/^_/, '') + ' *= *)createIterator\\(((?:{|[a-zA-Z])[\\s\\S]+?)\\);\\n');
if (reFunc.test(source)) { if (reFunc.test(source)) {
// extract, format, and inject the compiled function's source code // extract, format, and inject the compiled function's source code
source = source.replace(reFunc, function(match, captured) { source = source.replace(reFunc, function(match, captured) {
return captured + getFunctionSource(lodash[methodName]) + ';\n'; return captured + getFunctionSource(lodash[methodName], getIndent(captured)) + ';\n';
}); });
} }
}); });
@@ -2225,7 +2250,9 @@
if (!exposeIsPlainObject) { if (!exposeIsPlainObject) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.isPlainObject *= *.+\n/m, ''); modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.isPlainObject *= *.+\n/m, '');
} }
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
}()); }());
// remove `thisArg` from unexposed `forIn` and `forOwn` // remove `thisArg` from unexposed `forIn` and `forOwn`
@@ -2273,8 +2300,9 @@
} }
if (!(isMobile || isUnderscore)) { if (!(isMobile || isUnderscore)) {
// inline `iteratorTemplate` template // inline `iteratorTemplate` template
source = source.replace(getIteratorTemplate(source), function() { source = source.replace(getIteratorTemplate(source), function(match) {
var snippet = getFunctionSource(lodash._iteratorTemplate); var indent = getIndent(match),
snippet = getFunctionSource(lodash._iteratorTemplate, indent);
// prepend data object references to property names to avoid having to // prepend data object references to property names to avoid having to
// use a with-statement // use a with-statement
@@ -2303,7 +2331,7 @@
// remove comments, including sourceURLs // remove comments, including sourceURLs
snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, ''); snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, '');
return ' var iteratorTemplate = ' + snippet + ';\n'; return indent + 'var iteratorTemplate = ' + snippet + ';\n';
}); });
} }
} }

View File

@@ -296,7 +296,9 @@
}); });
// replace with modified snippet // replace with modified snippet
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
}); });
}()); }());
@@ -331,7 +333,10 @@
if (isCreateIterator) { if (isCreateIterator) {
// clip before the `factory` call to avoid minifying its arguments // clip before the `factory` call to avoid minifying its arguments
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
snippet = modified = modified.replace(/return factory\([\s\S]+$/, ''); snippet = modified = modified.replace(/return factory\([\s\S]+$/, '');
} }
// minify `createIterator` option property names // minify `createIterator` option property names
@@ -358,7 +363,9 @@
}); });
// replace with modified snippet // replace with modified snippet
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
}); });
return source; return source;