Allow convert to work on methods that aren't converted by default.

This commit is contained in:
John-David Dalton
2016-04-03 10:14:19 -07:00
parent 69ce7cde7d
commit 75d196b01b
2 changed files with 42 additions and 24 deletions

View File

@@ -40,7 +40,9 @@ function baseConvert(util, name, func, options) {
'rearg': 'rearg' in options ? options.rearg : true 'rearg': 'rearg' in options ? options.rearg : true
}; };
var forceRearg = ('rearg' in options) && options.rearg, var forceCurry = ('curry' in options) && options.curry,
forceFixed = ('fixed' in options) && options.fixed,
forceRearg = ('rearg' in options) && options.rearg,
placeholder = isLib ? func : fallbackHolder, placeholder = isLib ? func : fallbackHolder,
pristine = isLib ? func.runInContext() : undefined; pristine = isLib ? func.runInContext() : undefined;
@@ -116,7 +118,7 @@ function baseConvert(util, name, func, options) {
}; };
var convertLib = function(options) { var convertLib = function(options) {
return _.runInContext.convert(options)(); return _.runInContext.convert(options)(undefined);
}; };
var createCloner = function(func) { var createCloner = function(func) {
@@ -125,6 +127,17 @@ function baseConvert(util, name, func, options) {
}; };
}; };
var createConverter = function(name, func) {
var oldOptions = options;
return function(options) {
var newUtil = isLib ? pristine : helpers,
newFunc = isLib ? pristine[name] : func,
newOptions = assign(assign({}, oldOptions), options);
return baseConvert(newUtil, name, newFunc, newOptions);
};
};
var immutWrap = function(func, cloner) { var immutWrap = function(func, cloner) {
return function() { return function() {
var length = arguments.length; var length = arguments.length;
@@ -232,23 +245,15 @@ function baseConvert(util, name, func, options) {
var wrap = function(name, func) { var wrap = function(name, func) {
name = mapping.aliasToReal[name] || name; name = mapping.aliasToReal[name] || name;
var wrapper = wrappers[name];
var convertMethod = function(options) { var result,
var newUtil = isLib ? pristine : helpers, wrapped = func,
newFunc = isLib ? pristine[name] : func, wrapper = wrappers[name];
newOptions = assign(assign({}, config), options);
return baseConvert(newUtil, name, newFunc, newOptions);
};
if (wrapper) { if (wrapper) {
var result = wrapper(func); wrapped = wrapper(func);
result.convert = convertMethod;
return result;
} }
var wrapped = func; else if (config.immutable) {
if (config.immutable) {
if (mutateMap.array[name]) { if (mutateMap.array[name]) {
wrapped = immutWrap(func, cloneArray); wrapped = immutWrap(func, cloneArray);
} }
@@ -267,7 +272,7 @@ function baseConvert(util, name, func, options) {
spreadStart = mapping.methodSpread[name]; spreadStart = mapping.methodSpread[name];
result = wrapped; result = wrapped;
if (config.fixed) { if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
result = spreadStart === undefined result = spreadStart === undefined
? ary(result, aryKey) ? ary(result, aryKey)
: spread(result, spreadStart); : spread(result, spreadStart);
@@ -282,7 +287,8 @@ function baseConvert(util, name, func, options) {
result = iterateeAry(result, aryN); result = iterateeAry(result, aryN);
} }
} }
if (config.curry && aryKey > 1) { if (forceCurry || (config.curry && aryKey > 1)) {
forceCurry && console.log(forceCurry, name);
result = curry(result, aryKey); result = curry(result, aryKey);
} }
return false; return false;
@@ -293,11 +299,11 @@ function baseConvert(util, name, func, options) {
result || (result = wrapped); result || (result = wrapped);
if (result == func) { if (result == func) {
result = function() { result = forceCurry ? curry(result, 1) : function() {
return func.apply(this, arguments); return func.apply(this, arguments);
}; };
} }
result.convert = convertMethod; result.convert = createConverter(name, func);
if (mapping.placeholder[name]) { if (mapping.placeholder[name]) {
setPlaceholder = true; setPlaceholder = true;
result.placeholder = func.placeholder = placeholder; result.placeholder = func.placeholder = placeholder;
@@ -330,7 +336,8 @@ function baseConvert(util, name, func, options) {
return; return;
} }
} }
pairs.push([key, wrap(key, _[key])]); _[key].convert = createConverter(key, _[key]);
pairs.push([key, _[key]]);
} }
}); });

View File

@@ -52,9 +52,10 @@ exports.aliasToReal = {
exports.aryMethod = { exports.aryMethod = {
'1': [ '1': [
'attempt', 'castArray', 'ceil', 'create', 'curry', 'curryRight', 'floor', 'attempt', 'castArray', 'ceil', 'create', 'curry', 'curryRight', 'floor',
'fromPairs', 'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin', 'flow', 'flowRight', 'fromPairs', 'invert', 'iteratee', 'memoize', 'method',
'over', 'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext', 'methodOf', 'mixin', 'over', 'overEvery', 'overSome', 'rest', 'reverse',
'spread', 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words' 'round', 'runInContext', 'spread', 'template', 'trim', 'trimEnd', 'trimStart',
'uniqueId', 'words'
], ],
'2': [ '2': [
'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll', 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll',
@@ -248,7 +249,17 @@ exports.remap = {
'trimCharsStart': 'trimStart' 'trimCharsStart': 'trimStart'
}; };
/** Used to track methods that skip `_.rearg`. */ /** Used to track methods that skip fixing their arity. */
exports.skipFixed = {
'castArray': true,
'flow': true,
'flowRight': true,
'iteratee': true,
'mixin': true,
'runInContext': true
};
/** Used to track methods that skip rearranging arguments. */
exports.skipRearg = { exports.skipRearg = {
'add': true, 'add': true,
'assign': true, 'assign': true,