Use native Object.create when available, optimize the creation of lodash instances, and ensure methods like forEach return the existing wrapper object when chaining, instead of creating a new one.

Former-commit-id: fa9ec371ba23ce8c35c15a66dd9b1f09f183b3a9
This commit is contained in:
John-David Dalton
2013-03-03 01:19:04 -08:00
parent aa49ce5c56
commit 8825a094ae
3 changed files with 104 additions and 39 deletions

View File

@@ -381,21 +381,26 @@
].join('\n' + indent);
});
// add `__chain__` checks to `_.mixin`
source = source.replace(matchFunction(source, 'mixin'), function(match) {
return match.replace(/^( *)return new lodash.+/m, function() {
var indent = arguments[1];
return indent + [
'',
'var result = func.apply(lodash, args);',
'if (this.__chain__) {',
' result = new lodash(result);',
' result.__chain__ = true;',
'}',
'return result;'
].join('\n' + indent);
});
});
// replace `_.mixin`
source = replaceFunction(source, 'mixin', [
'function mixin(object) {',
' forEach(functions(object), function(methodName) {',
' var func = lodash[methodName] = object[methodName];',
'',
' lodash.prototype[methodName] = function() {',
' var args = [this.__wrapped__];',
' push.apply(args, arguments);',
'',
' var result = func.apply(lodash, args);',
' if (this.__chain__) {',
' result = createWrapper(result);',
' result.__chain__ = true;',
' }',
' return result;',
' };',
' });',
'}'
].join('\n'));
// replace wrapper `Array` method assignments
source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function(match, indent) {
@@ -1174,7 +1179,7 @@
// remove `noCharByIndex` from `_.toArray`
source = source.replace(matchFunction(source, 'toArray'), function(match) {
return match.replace(/noCharByIndex[^:]+:/, '');
return match.replace(/(return\b).+?noCharByIndex[^:]+:\s*/, '$1 ');
});
// `noCharByIndex` from `iteratorTemplate`
@@ -1753,7 +1758,7 @@
if (isModern) {
// remove `_.isPlainObject` fallback
source = source.replace(matchFunction(source, 'isPlainObject'), function(match) {
return match.replace(/!getPrototypeOf.+?: */, '');
return match.replace(/!getPrototypeOf[^:]+:\s*/, '');
});
if (!isMobile) {
@@ -1927,7 +1932,7 @@
' }',
' var isArr = className == arrayClass;',
' if (!isArr) {',
' if (a.__wrapped__ || b.__wrapped__) {',
' if (a instanceof lodash || b instanceof lodash) {',
' return isEqual(a.__wrapped__ || a, b.__wrapped__ || b, stackA, stackB);',
' }',
' if (className != objectClass) {',
@@ -1989,6 +1994,19 @@
'}'
].join('\n'));
// replace `lodash`
source = replaceFunction(source, 'lodash', [
'function lodash(value) {',
' if (value instanceof lodash) {',
' return value;',
' }',
' if (!(this instanceof lodash)) {',
' return new lodash(value);',
' }',
' this.__wrapped__ = value;',
'}'
].join('\n'));
// replace `_.omit`
source = replaceFunction(source, 'omit', [
'function omit(object) {',
@@ -2189,7 +2207,7 @@
// remove conditional `charCodeCallback` use from `_.max` and `_.min`
_.each(['max', 'min'], function(methodName) {
source = source.replace(matchFunction(source, methodName), function(match) {
return match.replace(/!callback *&& *isString\(collection\)[\s\S]+?: */g, '');
return match.replace(/(return\b).+?callback *&& *isString[^:]+:\s*/g, '$1 ');
});
});
@@ -2469,9 +2487,14 @@
' lodash[methodName] = func;',
'',
' lodash.prototype[methodName] = function() {',
' var args = [this.__wrapped__];',
' var value = this.__wrapped__,',
' args = [value];',
'',
' push.apply(args, arguments);',
' return new lodash(func.apply(lodash, args));',
' var result = func.apply(lodash, args);',
" return (value && typeof value == 'object' && value == result)",
' ? this',
' : createWrapper(result);',
' };',
'});'
].join('\n' + indent);