DRY more code in the "underscore" build.

Former-commit-id: c1dbc3daccb1efba371c38f20b9a4ff96a7171a4
This commit is contained in:
John-David Dalton
2012-09-14 01:07:48 -07:00
parent 6c1bbd2344
commit ae7e353169

View File

@@ -401,7 +401,7 @@
* @returns {String} Returns the `isArguments` fallback snippet. * @returns {String} Returns the `isArguments` fallback snippet.
*/ */
function getIsArgumentsFallback(source) { function getIsArgumentsFallback(source) {
return (source.match(/(?:\s*\/\/.*)*\n( +)if *\((?:noArgsClass|!isArguments\(arguments\))\)[\s\S]+?};\n\1}/) || [''])[0]; return (source.match(/(?:\s*\/\/.*)*\n( +)if *\(noArgsClass\)[\s\S]+?};\n\1}/) || [''])[0];
} }
/** /**
@@ -826,12 +826,9 @@
// remove `deep` clone functionality // remove `deep` clone functionality
source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [ source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [
' function clone(value) {', ' function clone(value) {',
' if (value && objectTypes[typeof value]) {', ' return value && objectTypes[typeof value]',
' return toString.call(value) == arrayClass', ' ? (toString.call(value) == arrayClass ? slice.call(value) : extend({}, value))',
' ? slice.call(value)', ' : value',
' : extend({}, value)',
' }',
' return value',
' }' ' }'
].join('\n')); ].join('\n'));
} }
@@ -990,6 +987,7 @@
// build replacement code // build replacement code
_.forOwn({ _.forOwn({
'Arguments': 'argsClass',
'Date': 'dateClass', 'Date': 'dateClass',
'Number': 'numberClass', 'Number': 'numberClass',
'RegExp': 'regexpClass', 'RegExp': 'regexpClass',
@@ -999,6 +997,10 @@
var funcName = 'is' + key, var funcName = 'is' + key,
funcCode = matchFunction(source, funcName); funcCode = matchFunction(source, funcName);
// only DRY `isArguments` for Underscore builds
if (key == 'Arguments' && !isUnderscore) {
return;
}
if (funcCode) { if (funcCode) {
funcNames.push(funcName); funcNames.push(funcName);
objectSnippets.push("'" + key + "': " + value); objectSnippets.push("'" + key + "': " + value);
@@ -1015,16 +1017,28 @@
source = removeFunction(source, funcName); source = removeFunction(source, funcName);
}); });
// insert new DRY code after the method assignments // insert new DRY code before the `lodash` method assignments
var snippet = getMethodAssignments(source); var snippet = getMethodAssignments(source);
source = source.replace(snippet, snippet + '\n' + source = source.replace(snippet, snippet += [
' // add `_.' + funcNames.join('`, `_.') + '`\n' + '',
' ' + iteratorName + '({\n ' + objectSnippets.join(',\n ') + '\n }, function(className, key) {\n' + ' // add `_.' + funcNames.join('`, `_.') + '`',
" lodash['is' + key] = function(value) {\n" + ' ' + iteratorName + '({\n ' + objectSnippets.join(',\n ') + '\n }, function(className, key) {',
' return toString.call(value) == className;\n' + " lodash['is' + key] = function(value) {",
' };\n' + ' return toString.call(value) == className',
' });\n' ' }',
); ' });'
].join('\n'));
// move `isArguments` fallback to after the new DRY code
if (isUnderscore) {
var fallback = getIsArgumentsFallback(source);
source = removeIsArgumentsFallback(source).replace(snippet,
snippet + fallback
.replace(/\bisArguments\b/g, 'lodash.$&')
.replace(/\bnoArgsClass\b/g, '!lodash.isArguments(arguments)') +
'\n'
);
}
}()); }());
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
@@ -1091,12 +1105,7 @@
return match.replace(/\bcallee\b/g, 'merge'); return match.replace(/\bcallee\b/g, 'merge');
}); });
if (isUnderscore) { if (!isUnderscore) {
// replace `noArgsClass` from `isArguments` fallback
source = source.replace(getIsArgumentsFallback(source), function(match) {
return match.replace('noArgsClass', '!isArguments(arguments)');
});
} else {
source = removeIsArgumentsFallback(source); source = removeIsArgumentsFallback(source);
} }