From de0f936e920102243342c750c588c35e1cf061cc Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 12 Feb 2016 22:49:49 -0800 Subject: [PATCH] Ensure fp `convert` works with category modules. --- fp/_baseConvert.js | 37 ++++++++++++++++++++-------------- test/test-fp.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index 96cc185f4..cb8975b2b 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -18,16 +18,20 @@ var mapping = require('./_mapping'), * @returns {Function|Object} Returns the converted function or object. */ function baseConvert(util, name, func, options) { - var isLib = typeof name == 'function'; - if (isLib) { + var setPlaceholder, + isLib = typeof name == 'function', + isObj = name === Object(name); + + if (isObj) { options = func; func = name; name = undefined; } - options || (options = {}); if (func == null) { throw new TypeError; } + options || (options = {}); + var config = { 'cap': 'cap' in options ? options.cap : true, 'curry': 'curry' in options ? options.curry : true, @@ -38,7 +42,7 @@ function baseConvert(util, name, func, options) { var forceRearg = ('rearg' in options) && options.rearg; - var _ = isLib ? func : { + var helpers = isLib ? func : { 'ary': util.ary, 'cloneDeep': util.cloneDeep, 'curry': util.curry, @@ -50,14 +54,14 @@ function baseConvert(util, name, func, options) { 'spread': util.spread }; - var ary = _.ary, - cloneDeep = _.cloneDeep, - curry = _.curry, - each = _.forEach, - isFunction = _.isFunction, - keys = _.keys, - rearg = _.rearg, - spread = _.spread; + var ary = helpers.ary, + cloneDeep = helpers.cloneDeep, + curry = helpers.curry, + each = helpers.forEach, + isFunction = helpers.isFunction, + keys = helpers.keys, + rearg = helpers.rearg, + spread = helpers.spread; var baseArity = function(func, n) { return n == 2 @@ -225,16 +229,16 @@ function baseConvert(util, name, func, options) { result || (result = wrapped); if (mapping.placeholder[name]) { + setPlaceholder = true; func.placeholder = result.placeholder = placeholder; } return result; }; - if (!isLib) { + if (!isObj) { return wrap(name, func); } - // Add placeholder. - _.placeholder = placeholder; + var _ = func; // Iterate over methods for the current ary cap. var pairs = []; @@ -252,6 +256,9 @@ function baseConvert(util, name, func, options) { _[pair[0]] = pair[1]; }); + if (setPlaceholder) { + _.placeholder = placeholder; + } // Wrap the lodash method and its aliases. each(keys(_), function(key) { each(mapping.realToAlias[key] || [], function(alias) { diff --git a/test/test-fp.js b/test/test-fp.js index 4b2f7b77d..ace1cd6ef 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -103,6 +103,40 @@ 'rearg': false }; + QUnit.test('should work when given an object', function(assert) { + assert.expect(2); + + var array = [1, 2, 3, 4], + lodash = convert({ 'remove': _.remove }); + + var actual = lodash.remove(function(n) { + return n % 2 == 0; + })(array); + + assert.deepEqual(array, [1, 2, 3, 4]); + assert.deepEqual(actual, [1, 3]); + }); + + QUnit.test('should only add a `placeholder` property if needed', function(assert) { + assert.expect(2); + + 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); + }); + QUnit.test('should accept an `options` argument', function(assert) { assert.expect(3); @@ -203,6 +237,21 @@ assert.deepEqual(actual, [1, 3]); assert.deepEqual(lodash.remove(), []); }); + + QUnit.test('should work when given an object and `options`', function(assert) { + assert.expect(3); + + var array = [1, 2, 3, 4], + lodash = convert({ 'remove': _.remove }, allFalseOptions); + + var actual = lodash.remove(array, function(n, index) { + return index % 2 == 0; + }); + + assert.deepEqual(array, [2, 4]); + assert.deepEqual(actual, [1, 3]); + assert.deepEqual(lodash.remove(), []); + }); }()); /*--------------------------------------------------------------------------*/