Enable convert to work when given lodash and options.

This commit is contained in:
John-David Dalton
2016-02-09 21:56:49 -08:00
parent c91196d240
commit e9edc06aaf
3 changed files with 40 additions and 35 deletions

View File

@@ -18,12 +18,13 @@ 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) {
options || (options = {}); var isLib = typeof name == 'function';
if (isLib) {
if (typeof func != 'function') { options = func;
func = name; func = name;
name = undefined; name = undefined;
} }
options || (options = {});
if (func == null) { if (func == null) {
throw new TypeError; throw new TypeError;
} }
@@ -37,8 +38,6 @@ function baseConvert(util, name, func, options) {
var forceRearg = ('rearg' in options) && options.rearg; var forceRearg = ('rearg' in options) && options.rearg;
var isLib = name === undefined && typeof func.VERSION == 'string';
var _ = isLib ? func : { var _ = isLib ? func : {
'ary': util.ary, 'ary': util.ary,
'cloneDeep': util.cloneDeep, 'cloneDeep': util.cloneDeep,
@@ -180,7 +179,7 @@ function baseConvert(util, name, func, options) {
}, },
'runInContext': function(runInContext) { 'runInContext': function(runInContext) {
return function(context) { return function(context) {
return baseConvert(util, runInContext(context), undefined, options); return baseConvert(util, runInContext(context), options);
}; };
} }
}; };

View File

@@ -8,7 +8,7 @@ var baseConvert = require('./_baseConvert');
* @returns {Function} Returns the converted `lodash`. * @returns {Function} Returns the converted `lodash`.
*/ */
function browserConvert(lodash, options) { function browserConvert(lodash, options) {
return baseConvert(lodash, lodash, undefined, options); return baseConvert(lodash, lodash, options);
} }
module.exports = browserConvert; module.exports = browserConvert;

View File

@@ -52,7 +52,8 @@
}; };
} }
return function(name, func, options) { return function(name, func, options) {
if (typeof func != 'function') { if (typeof name == 'function') {
options = func;
func = name; func = name;
name = undefined; name = undefined;
} }
@@ -90,18 +91,19 @@
QUnit.module('convert'); QUnit.module('convert');
(function() { (function() {
var allFalseOptions = {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
};
QUnit.test('should accept an `options` argument', function(assert) { QUnit.test('should accept an `options` argument', function(assert) {
assert.expect(3); assert.expect(3);
var array = [1, 2, 3, 4]; var array = [1, 2, 3, 4],
remove = convert('remove', _.remove, allFalseOptions);
var remove = convert('remove', _.remove, {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
});
var actual = remove(array, function(n, index) { var actual = remove(array, function(n, index) {
return index % 2 == 0; return index % 2 == 0;
@@ -150,9 +152,7 @@
QUnit.test('should respect the `cap` option', function(assert) { QUnit.test('should respect the `cap` option', function(assert) {
assert.expect(1); assert.expect(1);
var iteratee = convert('iteratee', _.iteratee, { var iteratee = convert('iteratee', _.iteratee, { 'cap': false });
'cap': false
});
var func = iteratee(function(a, b, c) { var func = iteratee(function(a, b, c) {
return [a, b, c]; return [a, b, c];
@@ -164,9 +164,7 @@
QUnit.test('should respect the `rearg` option', function(assert) { QUnit.test('should respect the `rearg` option', function(assert) {
assert.expect(1); assert.expect(1);
var add = convert('add', _.add, { var add = convert('add', _.add, { 'rearg': true });
'rearg': true
});
assert.strictEqual(add('2')('1'), '12'); assert.strictEqual(add('2')('1'), '12');
}); });
@@ -174,23 +172,31 @@
QUnit.test('should use `options` in `runInContext`', function(assert) { QUnit.test('should use `options` in `runInContext`', function(assert) {
assert.expect(3); assert.expect(3);
var runInContext = convert('runInContext', _.runInContext, {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
});
var array = [1, 2, 3, 4], var array = [1, 2, 3, 4],
runInContext = convert('runInContext', _.runInContext, allFalseOptions),
lodash = runInContext(); lodash = runInContext();
var actual = lodash.remove(array, function(n) { var actual = lodash.remove(array, function(n, index) {
return n % 2 == 0; return index % 2 == 0;
}); });
assert.deepEqual(array, [1, 3]); assert.deepEqual(array, [2, 4]);
assert.deepEqual(actual, [2, 4]); assert.deepEqual(actual, [1, 3]);
assert.deepEqual(lodash.remove(), []);
});
QUnit.test('should work when given lodash and `options`', function(assert) {
assert.expect(3);
var array = [1, 2, 3, 4],
lodash = convert(_.runInContext(), 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(), []); assert.deepEqual(lodash.remove(), []);
}); });
}()); }());