From 75d196b01bd0c1d4797f7386f7047ae28851186f Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 3 Apr 2016 10:14:19 -0700 Subject: [PATCH] Allow `convert` to work on methods that aren't converted by default. --- fp/_baseConvert.js | 47 ++++++++++++++++++++++++++-------------------- fp/_mapping.js | 19 +++++++++++++++---- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index 22d841664..3f5c91cb0 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -40,7 +40,9 @@ function baseConvert(util, name, func, options) { '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, pristine = isLib ? func.runInContext() : undefined; @@ -116,7 +118,7 @@ function baseConvert(util, name, func, options) { }; var convertLib = function(options) { - return _.runInContext.convert(options)(); + return _.runInContext.convert(options)(undefined); }; 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) { return function() { var length = arguments.length; @@ -232,23 +245,15 @@ function baseConvert(util, name, func, options) { var wrap = function(name, func) { name = mapping.aliasToReal[name] || name; - var wrapper = wrappers[name]; - var convertMethod = function(options) { - var newUtil = isLib ? pristine : helpers, - newFunc = isLib ? pristine[name] : func, - newOptions = assign(assign({}, config), options); - - return baseConvert(newUtil, name, newFunc, newOptions); - }; + var result, + wrapped = func, + wrapper = wrappers[name]; if (wrapper) { - var result = wrapper(func); - result.convert = convertMethod; - return result; + wrapped = wrapper(func); } - var wrapped = func; - if (config.immutable) { + else if (config.immutable) { if (mutateMap.array[name]) { wrapped = immutWrap(func, cloneArray); } @@ -267,7 +272,7 @@ function baseConvert(util, name, func, options) { spreadStart = mapping.methodSpread[name]; result = wrapped; - if (config.fixed) { + if (config.fixed && (forceFixed || !mapping.skipFixed[name])) { result = spreadStart === undefined ? ary(result, aryKey) : spread(result, spreadStart); @@ -282,7 +287,8 @@ function baseConvert(util, name, func, options) { result = iterateeAry(result, aryN); } } - if (config.curry && aryKey > 1) { + if (forceCurry || (config.curry && aryKey > 1)) { + forceCurry && console.log(forceCurry, name); result = curry(result, aryKey); } return false; @@ -293,11 +299,11 @@ function baseConvert(util, name, func, options) { result || (result = wrapped); if (result == func) { - result = function() { + result = forceCurry ? curry(result, 1) : function() { return func.apply(this, arguments); }; } - result.convert = convertMethod; + result.convert = createConverter(name, func); if (mapping.placeholder[name]) { setPlaceholder = true; result.placeholder = func.placeholder = placeholder; @@ -330,7 +336,8 @@ function baseConvert(util, name, func, options) { return; } } - pairs.push([key, wrap(key, _[key])]); + _[key].convert = createConverter(key, _[key]); + pairs.push([key, _[key]]); } }); diff --git a/fp/_mapping.js b/fp/_mapping.js index 920ac986b..e897d751d 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -52,9 +52,10 @@ exports.aliasToReal = { exports.aryMethod = { '1': [ 'attempt', 'castArray', 'ceil', 'create', 'curry', 'curryRight', 'floor', - 'fromPairs', 'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin', - 'over', 'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext', - 'spread', 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words' + 'flow', 'flowRight', 'fromPairs', 'invert', 'iteratee', 'memoize', 'method', + 'methodOf', 'mixin', 'over', 'overEvery', 'overSome', 'rest', 'reverse', + 'round', 'runInContext', 'spread', 'template', 'trim', 'trimEnd', 'trimStart', + 'uniqueId', 'words' ], '2': [ 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll', @@ -248,7 +249,17 @@ exports.remap = { '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 = { 'add': true, 'assign': true,