Skip to content

Commit

Permalink
Reduce lodash.underscore.min.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Oct 2, 2012
1 parent 86a4350 commit ca867aa
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 55 deletions.
94 changes: 70 additions & 24 deletions build.js
Expand Up @@ -542,7 +542,7 @@
function matchFunction(source, funcName) {
var result = source.match(RegExp(
// match multi-line comment block (could be on a single line)
'\\n +/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/\\n' +
'(?:\\n +/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/\\n)?' +
// begin non-capturing group
'(?:' +
// match a function declaration
Expand Down Expand Up @@ -733,16 +733,12 @@
source = source.replace(RegExp('(var ' + varName + ' *=)[\\s\\S]+?(true;\\n)'), '$1$2');
}
source = source.replace(RegExp(
// begin non-capturing group
'(?:' +
// match multi-line comment block
'(?:\\n +/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/)?\\n' +
// match a variable declaration that's not part of a declaration list
'( +)var ' + varName + ' *= *(?:.+?(?:;|&&\\n[^;]+;)|(?:\\w+\\(|{)[\\s\\S]+?\\n\\1.+?;)\\n|' +
// match a variable in a declaration list
'\\n +' + varName + ' *=.+?,' +
// end non-capturing group
')'
'\\n +' + varName + ' *=.+?,'
), '');

// remove a varaible at the start of a variable declaration list
Expand Down Expand Up @@ -1050,17 +1046,76 @@
source = removeVar(source, 'arrayLikeClasses');
source = removeVar(source, 'cloneableClasses');

// remove large array optimizations from `cachedContains`
source = source.replace(/( +)function cachedContains[\s\S]+?\n\1}/, [
' function cachedContains(array, fromIndex) {',
' return function(value) {',
' return indexOf(array, value, fromIndex || 0) > -1;',
' };',
// remove large array optimizations
source = removeFunction(source, 'cachedContains');
source = removeVar(source, 'largeArraySize');

// replace `_.clone`
source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [
' function clone(value) {',
' return value && objectTypes[typeof value]',
' ? (isArray(value) ? slice.call(value) : extend({}, value))',
' : value',
' }'
].join('\n'));

// reduce the number of arguments passed to `cachedContains` from 3 to 2
source = source.replace(/(cachedContains\(\w+, *\w+), *\w+/g, '$1');
// replace `_.difference`
source = source.replace(/( +)function difference[\s\S]+?\n\1}/, [
' function difference(array) {',
' var index = -1,',
' length = array.length,',
' flattened = concat.apply(ArrayProto, arguments),',
' result = [];',
'',
' while (++index < length) {',
' var value = array[index]',
' if (indexOf(flattened, value, length) < 0) {',
' result.push(value);',
' }',
' }',
' return result',
' }'
].join('\n'));

// replace `_.intersection`
source = source.replace(/( +)function intersection[\s\S]+?\n\1}/, [
' function intersection(array) {',
' var argsLength = arguments.length,',
' index = -1,',
' length = array.length,',
' result = [];',
'',
' array: while (++index < length) {',
' var value = array[index]',
' if (indexOf(result, value) < 0) {',
' for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) {',
' if (indexOf(arguments[argsIndex], value) < 0) {',
' continue array;',
' }',
' }',
' result.push(value);',
' }',
' }',
' return result',
' }'
].join('\n'));

// replace `_.without`
source = source.replace(/( +)function without[\s\S]+?\n\1}/, [
' function without(array) {',
' var index = -1,',
' length = array.length,',
' result = [];',
'',
' while (++index < length) {',
' var value = array[index]',
' if (indexOf(arguments, value, 1) < 0) {',
' result.push(value);',
' }',
' }',
' return result',
' }'
].join('\n'));

// replace `arrayLikeClasses` in `_.isEmpty`
source = source.replace(/'if *\(arrayLikeClasses[\s\S]+?' \|\|\\n/, "'if (isArray(value) || className == stringClass ||");
Expand All @@ -1069,19 +1124,10 @@
source = source.replace(/(?: *\/\/.*\n)*( +)var isArr *= *arrayLikeClasses[^}]+}/, '$1var isArr = isArray(a);');

// remove "exit early" feature from `_.each`
source = source.replace(/( )+var baseIteratorOptions *=[\s\S]+?\n\1.+?;/, function(match) {
source = source.replace(/( +)var baseIteratorOptions *=[\s\S]+?\n\1.+?;/, function(match) {
return match.replace(/if *\(callback[^']+/, 'callback(value, index, collection)');
});

// remove `deep` clone functionality
source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [
' function clone(value) {',
' return value && objectTypes[typeof value]',
' ? (isArray(value) ? slice.call(value) : extend({}, value))',
' : value',
' }'
].join('\n'));

// remove unused features from `createBound`
if (buildMethods.indexOf('partial') == -1) {
source = source.replace(matchFunction(source, 'createBound'), function(match) {
Expand Down

0 comments on commit ca867aa

Please sign in to comment.