Skip to content

Commit fd2a17d

Browse files
committed
Reduce lodash.underscore.min.js.
Former-commit-id: a5032bc542e1166fab6acfd7313c305dd8236d36
1 parent 126804f commit fd2a17d

File tree

2 files changed

+101
-55
lines changed

2 files changed

+101
-55
lines changed

build.js

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@
542542
function matchFunction(source, funcName) {
543543
var result = source.match(RegExp(
544544
// match multi-line comment block (could be on a single line)
545-
'\\n +/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/\\n' +
545+
'(?:\\n +/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/\\n)?' +
546546
// begin non-capturing group
547547
'(?:' +
548548
// match a function declaration
@@ -733,16 +733,12 @@
733733
source = source.replace(RegExp('(var ' + varName + ' *=)[\\s\\S]+?(true;\\n)'), '$1$2');
734734
}
735735
source = source.replace(RegExp(
736-
// begin non-capturing group
737-
'(?:' +
738736
// match multi-line comment block
739737
'(?:\\n +/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/)?\\n' +
740738
// match a variable declaration that's not part of a declaration list
741739
'( +)var ' + varName + ' *= *(?:.+?(?:;|&&\\n[^;]+;)|(?:\\w+\\(|{)[\\s\\S]+?\\n\\1.+?;)\\n|' +
742740
// match a variable in a declaration list
743-
'\\n +' + varName + ' *=.+?,' +
744-
// end non-capturing group
745-
')'
741+
'\\n +' + varName + ' *=.+?,'
746742
), '');
747743

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

1053-
// remove large array optimizations from `cachedContains`
1054-
source = source.replace(/( +)function cachedContains[\s\S]+?\n\1}/, [
1055-
' function cachedContains(array, fromIndex) {',
1056-
' return function(value) {',
1057-
' return indexOf(array, value, fromIndex || 0) > -1;',
1058-
' };',
1049+
// remove large array optimizations
1050+
source = removeFunction(source, 'cachedContains');
1051+
source = removeVar(source, 'largeArraySize');
1052+
1053+
// replace `_.clone`
1054+
source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [
1055+
' function clone(value) {',
1056+
' return value && objectTypes[typeof value]',
1057+
' ? (isArray(value) ? slice.call(value) : extend({}, value))',
1058+
' : value',
10591059
' }'
10601060
].join('\n'));
10611061

1062-
// reduce the number of arguments passed to `cachedContains` from 3 to 2
1063-
source = source.replace(/(cachedContains\(\w+, *\w+), *\w+/g, '$1');
1062+
// replace `_.difference`
1063+
source = source.replace(/( +)function difference[\s\S]+?\n\1}/, [
1064+
' function difference(array) {',
1065+
' var index = -1,',
1066+
' length = array.length,',
1067+
' flattened = concat.apply(ArrayProto, arguments),',
1068+
' result = [];',
1069+
'',
1070+
' while (++index < length) {',
1071+
' var value = array[index]',
1072+
' if (indexOf(flattened, value, length) < 0) {',
1073+
' result.push(value);',
1074+
' }',
1075+
' }',
1076+
' return result',
1077+
' }'
1078+
].join('\n'));
1079+
1080+
// replace `_.intersection`
1081+
source = source.replace(/( +)function intersection[\s\S]+?\n\1}/, [
1082+
' function intersection(array) {',
1083+
' var argsLength = arguments.length,',
1084+
' index = -1,',
1085+
' length = array.length,',
1086+
' result = [];',
1087+
'',
1088+
' array: while (++index < length) {',
1089+
' var value = array[index]',
1090+
' if (indexOf(result, value) < 0) {',
1091+
' for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) {',
1092+
' if (indexOf(arguments[argsIndex], value) < 0) {',
1093+
' continue array;',
1094+
' }',
1095+
' }',
1096+
' result.push(value);',
1097+
' }',
1098+
' }',
1099+
' return result',
1100+
' }'
1101+
].join('\n'));
1102+
1103+
// replace `_.without`
1104+
source = source.replace(/( +)function without[\s\S]+?\n\1}/, [
1105+
' function without(array) {',
1106+
' var index = -1,',
1107+
' length = array.length,',
1108+
' result = [];',
1109+
'',
1110+
' while (++index < length) {',
1111+
' var value = array[index]',
1112+
' if (indexOf(arguments, value, 1) < 0) {',
1113+
' result.push(value);',
1114+
' }',
1115+
' }',
1116+
' return result',
1117+
' }'
1118+
].join('\n'));
10641119

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

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

1076-
// remove `deep` clone functionality
1077-
source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [
1078-
' function clone(value) {',
1079-
' return value && objectTypes[typeof value]',
1080-
' ? (isArray(value) ? slice.call(value) : extend({}, value))',
1081-
' : value',
1082-
' }'
1083-
].join('\n'));
1084-
10851131
// remove unused features from `createBound`
10861132
if (buildMethods.indexOf('partial') == -1) {
10871133
source = source.replace(matchFunction(source, 'createBound'), function(match) {

0 commit comments

Comments
 (0)