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.
*/
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) {

View File

@@ -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(), []);
});
}());
/*--------------------------------------------------------------------------*/