From 2de676fe76bc68fb279ac84c1c87d59511ced8cc Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 30 Aug 2018 23:57:31 -0700 Subject: [PATCH] Ensure placeholder properties are set for fp.convert() results. [closes #3440] --- fp/_baseConvert.js | 25 +++++++++++-------------- fp/_mapping.js | 10 ---------- test/test-fp.js | 30 ++++++++++++++---------------- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index fcc28f26bd..120bc1d9e1 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -136,8 +136,7 @@ function wrapImmutable(func, cloner) { * @returns {Function|Object} Returns the converted function or object. */ function baseConvert(util, name, func, options) { - var setPlaceholder, - isLib = typeof name == 'function', + var isLib = typeof name == 'function', isObj = name === Object(name); if (isObj) { @@ -158,10 +157,10 @@ function baseConvert(util, name, func, options) { 'rearg': 'rearg' in options ? options.rearg : true }; - var forceCurry = ('curry' in options) && options.curry, + var defaultHolder = isLib ? func : fallbackHolder, + 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; var helpers = isLib ? func : { @@ -466,7 +465,7 @@ function baseConvert(util, name, func, options) { * @param {Function} func The function to wrap. * @returns {Function} Returns the converted function. */ - function wrap(name, func) { + function wrap(name, func, placeholder) { var result, realName = mapping.aliasToReal[name] || name, wrapped = func, @@ -511,17 +510,15 @@ function baseConvert(util, name, func, options) { }; } result.convert = createConverter(realName, func); - if (mapping.placeholder[realName]) { - setPlaceholder = true; - result.placeholder = func.placeholder = placeholder; - } + result.placeholder = func.placeholder = placeholder; + return result; } /*--------------------------------------------------------------------------*/ if (!isObj) { - return wrap(name, func); + return wrap(name, func, defaultHolder); } var _ = func; @@ -531,7 +528,8 @@ function baseConvert(util, name, func, options) { each(mapping.aryMethod[aryKey], function(key) { var func = _[mapping.remap[key] || key]; if (func) { - pairs.push([key, wrap(key, func)]); + + pairs.push([key, wrap(key, func, _)]); } }); }); @@ -557,9 +555,8 @@ function baseConvert(util, name, func, options) { }); _.convert = convertLib; - if (setPlaceholder) { - _.placeholder = placeholder; - } + _.placeholder = _; + // Assign aliases. each(keys(_), function(key) { each(mapping.realToAlias[key] || [], function(alias) { diff --git a/fp/_mapping.js b/fp/_mapping.js index 8f5ddf2d09..a642ec0584 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -261,16 +261,6 @@ exports.mutate = { } }; -/** Used to track methods with placeholder support */ -exports.placeholder = { - 'bind': true, - 'bindKey': true, - 'curry': true, - 'curryRight': true, - 'partial': true, - 'partialRight': true -}; - /** Used to map real names to their aliases. */ exports.realToAlias = (function() { var hasOwnProperty = Object.prototype.hasOwnProperty, diff --git a/test/test-fp.js b/test/test-fp.js index a41134ca7e..cdd5304aae 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -233,25 +233,14 @@ assert.strictEqual(add('2')('1'), '12'); }); - QUnit.test('should only add a `placeholder` property if needed', function(assert) { + QUnit.test('should add a `placeholder` property', function(assert) { assert.expect(2); if (!document) { - var methodNames = _.keys(mapping.placeholder), - expected = _.map(methodNames, _.constant(true)); - - var actual = _.map(methodNames, function(methodName) { - var object = {}; - object[methodName] = _[methodName]; - - var lodash = convert(object); - return methodName in lodash; - }); - - assert.deepEqual(actual, expected); - var lodash = convert({ 'add': _.add }); - assert.notOk('placeholder' in lodash); + + assert.strictEqual(lodash.placeholder, lodash); + assert.strictEqual(lodash.add.placeholder, lodash) } else { skipAssert(assert, 2); @@ -645,7 +634,16 @@ }); }); - _.forOwn(mapping.placeholder, function(truthy, methodName) { + var methodNames = [ + 'bind', + 'bindKey', + 'curry', + 'curryRight', + 'partial', + 'partialRight' + ] + + _.each(methodNames, function(methodName) { var func = fp[methodName]; QUnit.test('fp.' + methodName + '` should have a `placeholder` property', function(assert) {