Ensure fp convert works with category modules.

This commit is contained in:
John-David Dalton
2016-02-12 22:49:49 -08:00
parent bf9dcfe89c
commit de0f936e92
2 changed files with 71 additions and 15 deletions

View File

@@ -18,16 +18,20 @@ var mapping = require('./_mapping'),
* @returns {Function|Object} Returns the converted function or object. * @returns {Function|Object} Returns the converted function or object.
*/ */
function baseConvert(util, name, func, options) { function baseConvert(util, name, func, options) {
var isLib = typeof name == 'function'; var setPlaceholder,
if (isLib) { isLib = typeof name == 'function',
isObj = name === Object(name);
if (isObj) {
options = func; options = func;
func = name; func = name;
name = undefined; name = undefined;
} }
options || (options = {});
if (func == null) { if (func == null) {
throw new TypeError; throw new TypeError;
} }
options || (options = {});
var config = { var config = {
'cap': 'cap' in options ? options.cap : true, 'cap': 'cap' in options ? options.cap : true,
'curry': 'curry' in options ? options.curry : 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 forceRearg = ('rearg' in options) && options.rearg;
var _ = isLib ? func : { var helpers = isLib ? func : {
'ary': util.ary, 'ary': util.ary,
'cloneDeep': util.cloneDeep, 'cloneDeep': util.cloneDeep,
'curry': util.curry, 'curry': util.curry,
@@ -50,14 +54,14 @@ function baseConvert(util, name, func, options) {
'spread': util.spread 'spread': util.spread
}; };
var ary = _.ary, var ary = helpers.ary,
cloneDeep = _.cloneDeep, cloneDeep = helpers.cloneDeep,
curry = _.curry, curry = helpers.curry,
each = _.forEach, each = helpers.forEach,
isFunction = _.isFunction, isFunction = helpers.isFunction,
keys = _.keys, keys = helpers.keys,
rearg = _.rearg, rearg = helpers.rearg,
spread = _.spread; spread = helpers.spread;
var baseArity = function(func, n) { var baseArity = function(func, n) {
return n == 2 return n == 2
@@ -225,16 +229,16 @@ function baseConvert(util, name, func, options) {
result || (result = wrapped); result || (result = wrapped);
if (mapping.placeholder[name]) { if (mapping.placeholder[name]) {
setPlaceholder = true;
func.placeholder = result.placeholder = placeholder; func.placeholder = result.placeholder = placeholder;
} }
return result; return result;
}; };
if (!isLib) { if (!isObj) {
return wrap(name, func); return wrap(name, func);
} }
// Add placeholder. var _ = func;
_.placeholder = placeholder;
// Iterate over methods for the current ary cap. // Iterate over methods for the current ary cap.
var pairs = []; var pairs = [];
@@ -252,6 +256,9 @@ function baseConvert(util, name, func, options) {
_[pair[0]] = pair[1]; _[pair[0]] = pair[1];
}); });
if (setPlaceholder) {
_.placeholder = placeholder;
}
// Wrap the lodash method and its aliases. // Wrap the lodash method and its aliases.
each(keys(_), function(key) { each(keys(_), function(key) {
each(mapping.realToAlias[key] || [], function(alias) { each(mapping.realToAlias[key] || [], function(alias) {

View File

@@ -103,6 +103,40 @@
'rearg': false '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) { QUnit.test('should accept an `options` argument', function(assert) {
assert.expect(3); assert.expect(3);
@@ -203,6 +237,21 @@
assert.deepEqual(actual, [1, 3]); assert.deepEqual(actual, [1, 3]);
assert.deepEqual(lodash.remove(), []); 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(), []);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/