Add options to baseConvert.

This commit is contained in:
John-David Dalton
2016-02-07 20:17:00 -08:00
parent db23fcfe9f
commit 349c627110
4 changed files with 149 additions and 26 deletions

View File

@@ -9,9 +9,17 @@ var mapping = require('./_mapping'),
* @param {Object} util The util object.
* @param {string} name The name of the function to wrap.
* @param {Function} func The function to wrap.
* @param {Object} [options] The options object.
* @param {boolean} [options.cap=true] Specify capping iteratee arguments.
* @param {boolean} [options.curry=true] Specify currying.
* @param {boolean} [options.fixed=true] Specify fixed arity.
* @param {boolean} [options.immutable=true] Specify immutable operations.
* @param {boolean} [options.rearg=true] Specify rearranging arguments.
* @returns {Function|Object} Returns the converted function or object.
*/
function baseConvert(util, name, func) {
function baseConvert(util, name, func, options) {
options || (options = {});
if (typeof func != 'function') {
func = name;
name = undefined;
@@ -19,6 +27,16 @@ function baseConvert(util, name, func) {
if (func == null) {
throw new TypeError;
}
var config = {
'cap': 'cap' in options ? options.cap : true,
'curry': 'curry' in options ? options.curry : true,
'fixed': 'fixed' in options ? options.fixed : true,
'immutable': 'immutable' in options ? options.immutable : true,
'rearg': 'rearg' in options ? options.rearg : true
};
var forceRearg = ('rearg' in options) && options.rearg;
var isLib = name === undefined && typeof func.VERSION == 'string';
var _ = isLib ? func : {
@@ -107,6 +125,9 @@ function baseConvert(util, name, func) {
var func = arguments[0],
arity = arguments[1];
if (!config.cap) {
return iteratee(func, arity);
}
arity = arity > 2 ? (arity - 2) : 1;
func = iteratee(func);
var length = func.length;
@@ -145,7 +166,7 @@ function baseConvert(util, name, func) {
},
'runInContext': function(runInContext) {
return function(context) {
return baseConvert(util, runInContext(context));
return baseConvert(util, runInContext(context), undefined, options);
};
}
};
@@ -157,14 +178,16 @@ function baseConvert(util, name, func) {
return wrapper(func);
}
var wrapped = func;
if (mutateMap.array[name]) {
wrapped = immutWrap(func, cloneArray);
}
else if (mutateMap.object[name]) {
wrapped = immutWrap(func, createCloner(func));
}
else if (mutateMap.set[name]) {
wrapped = immutWrap(func, cloneDeep);
if (config.immutable) {
if (mutateMap.array[name]) {
wrapped = immutWrap(func, cloneArray);
}
else if (mutateMap.object[name]) {
wrapped = immutWrap(func, createCloner(func));
}
else if (mutateMap.set[name]) {
wrapped = immutWrap(func, cloneDeep);
}
}
var result;
each(mapping.caps, function(cap) {
@@ -174,19 +197,22 @@ function baseConvert(util, name, func) {
reargIndexes = mapping.iterateeRearg[name],
spreadStart = mapping.methodSpread[name];
result = spreadStart === undefined
? ary(wrapped, cap)
: spread(wrapped, spreadStart);
if (cap > 1 && !mapping.skipRearg[name]) {
if (config.fixed) {
result = spreadStart === undefined
? ary(wrapped, cap)
: spread(wrapped, spreadStart);
}
if (config.rearg && cap > 1 && (forceRearg || !mapping.skipRearg[name])) {
result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[cap]);
}
if (reargIndexes) {
result = iterateeRearg(result, reargIndexes);
} else if (aryN) {
result = iterateeAry(result, aryN);
if (config.cap) {
if (reargIndexes) {
result = iterateeRearg(result, reargIndexes);
} else if (aryN) {
result = iterateeAry(result, aryN);
}
}
if (cap > 1) {
if (config.curry && cap > 1) {
result = curry(result, cap);
}
return false;

View File

@@ -4,10 +4,11 @@ var baseConvert = require('./_baseConvert');
* Converts `lodash` to an immutable auto-curried iteratee-first data-last version.
*
* @param {Function} lodash The lodash function.
* @param {Object} [options] The options object. See `baseConvert` for more details.
* @returns {Function} Returns the converted `lodash`.
*/
function browserConvert(lodash) {
return baseConvert(lodash, lodash);
function browserConvert(lodash, options) {
return baseConvert(lodash, lodash, undefined, options);
}
module.exports = browserConvert;

View File

@@ -7,10 +7,11 @@ var baseConvert = require('./_baseConvert'),
*
* @param {string} name The name of the function to wrap.
* @param {Function} [func] The function to wrap.
* @param {Object} [options] The options object. See `baseConvert` for more details.
* @returns {Function|Object} Returns the converted function or object.
*/
function convert(name, func) {
return baseConvert(util, name, func);
function convert(name, func, options) {
return baseConvert(util, name, func, options);
}
module.exports = convert;

View File

@@ -46,8 +46,8 @@
var convert = root.fp || (function() {
var baseConvert = load('../fp/_baseConvert.js');
return function(name, func) {
return baseConvert(_, name, func);
return function(name, func, options) {
return baseConvert(_, name, func, options);
};
}());
@@ -76,6 +76,101 @@
console.log('Running lodash/fp tests.');
}
QUnit.module('convert');
(function() {
QUnit.test('should accept an `options` argument', function(assert) {
assert.expect(3);
if (!document) {
var remove = convert('remove', _.remove, {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
});
var array = [1, 2, 3, 4];
var actual = remove(array, function(n) {
return n % 2 == 0;
});
assert.deepEqual(array, [1, 3]);
assert.deepEqual(actual, [2, 4]);
assert.deepEqual(remove(), []);
}
else {
skipTest(assert, 3);
}
});
QUnit.test('should respect the `cap` option', function(assert) {
assert.expect(1);
if (!document) {
var iteratee = convert('iteratee', _.iteratee, {
'cap': false
});
var func = iteratee(function(a, b, c) {
return [a, b, c];
}, 3);
assert.deepEqual(func(1, 2, 3), [1, 2, 3]);
}
else {
skipTest(assert);
}
});
QUnit.test('should respect the `rearg` option', function(assert) {
assert.expect(1);
if (!document) {
var add = convert('add', _.add, {
'rearg': true
});
assert.strictEqual(add('2')('1'), '12');
}
else {
skipTest(assert);
}
});
QUnit.test('should use `options` in `runInContext`', function(assert) {
assert.expect(3);
if (!document) {
var runInContext = convert('runInContext', _.runInContext, {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
});
var array = [1, 2, 3, 4],
lodash = runInContext();
var actual = lodash.remove(array, function(n) {
return n % 2 == 0;
});
assert.deepEqual(array, [1, 3]);
assert.deepEqual(actual, [2, 4]);
assert.deepEqual(lodash.remove(), []);
}
else {
skipTest(assert, 3);
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('method arity checks');
(function() {