mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 20:07:49 +00:00
Add options to baseConvert.
This commit is contained in:
@@ -9,9 +9,17 @@ var mapping = require('./_mapping'),
|
|||||||
* @param {Object} util The util object.
|
* @param {Object} util The util object.
|
||||||
* @param {string} name The name of the function to wrap.
|
* @param {string} name The name of the function to wrap.
|
||||||
* @param {Function} func 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.
|
* @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') {
|
if (typeof func != 'function') {
|
||||||
func = name;
|
func = name;
|
||||||
name = undefined;
|
name = undefined;
|
||||||
@@ -19,6 +27,16 @@ function baseConvert(util, name, func) {
|
|||||||
if (func == null) {
|
if (func == null) {
|
||||||
throw new TypeError;
|
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 = name === undefined && typeof func.VERSION == 'string';
|
||||||
|
|
||||||
var _ = isLib ? func : {
|
var _ = isLib ? func : {
|
||||||
@@ -107,6 +125,9 @@ function baseConvert(util, name, func) {
|
|||||||
var func = arguments[0],
|
var func = arguments[0],
|
||||||
arity = arguments[1];
|
arity = arguments[1];
|
||||||
|
|
||||||
|
if (!config.cap) {
|
||||||
|
return iteratee(func, arity);
|
||||||
|
}
|
||||||
arity = arity > 2 ? (arity - 2) : 1;
|
arity = arity > 2 ? (arity - 2) : 1;
|
||||||
func = iteratee(func);
|
func = iteratee(func);
|
||||||
var length = func.length;
|
var length = func.length;
|
||||||
@@ -145,7 +166,7 @@ function baseConvert(util, name, func) {
|
|||||||
},
|
},
|
||||||
'runInContext': function(runInContext) {
|
'runInContext': function(runInContext) {
|
||||||
return function(context) {
|
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);
|
return wrapper(func);
|
||||||
}
|
}
|
||||||
var wrapped = func;
|
var wrapped = func;
|
||||||
if (mutateMap.array[name]) {
|
if (config.immutable) {
|
||||||
wrapped = immutWrap(func, cloneArray);
|
if (mutateMap.array[name]) {
|
||||||
}
|
wrapped = immutWrap(func, cloneArray);
|
||||||
else if (mutateMap.object[name]) {
|
}
|
||||||
wrapped = immutWrap(func, createCloner(func));
|
else if (mutateMap.object[name]) {
|
||||||
}
|
wrapped = immutWrap(func, createCloner(func));
|
||||||
else if (mutateMap.set[name]) {
|
}
|
||||||
wrapped = immutWrap(func, cloneDeep);
|
else if (mutateMap.set[name]) {
|
||||||
|
wrapped = immutWrap(func, cloneDeep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var result;
|
var result;
|
||||||
each(mapping.caps, function(cap) {
|
each(mapping.caps, function(cap) {
|
||||||
@@ -174,19 +197,22 @@ function baseConvert(util, name, func) {
|
|||||||
reargIndexes = mapping.iterateeRearg[name],
|
reargIndexes = mapping.iterateeRearg[name],
|
||||||
spreadStart = mapping.methodSpread[name];
|
spreadStart = mapping.methodSpread[name];
|
||||||
|
|
||||||
result = spreadStart === undefined
|
if (config.fixed) {
|
||||||
? ary(wrapped, cap)
|
result = spreadStart === undefined
|
||||||
: spread(wrapped, spreadStart);
|
? ary(wrapped, cap)
|
||||||
|
: spread(wrapped, spreadStart);
|
||||||
if (cap > 1 && !mapping.skipRearg[name]) {
|
}
|
||||||
|
if (config.rearg && cap > 1 && (forceRearg || !mapping.skipRearg[name])) {
|
||||||
result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[cap]);
|
result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[cap]);
|
||||||
}
|
}
|
||||||
if (reargIndexes) {
|
if (config.cap) {
|
||||||
result = iterateeRearg(result, reargIndexes);
|
if (reargIndexes) {
|
||||||
} else if (aryN) {
|
result = iterateeRearg(result, reargIndexes);
|
||||||
result = iterateeAry(result, aryN);
|
} else if (aryN) {
|
||||||
|
result = iterateeAry(result, aryN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (cap > 1) {
|
if (config.curry && cap > 1) {
|
||||||
result = curry(result, cap);
|
result = curry(result, cap);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ var baseConvert = require('./_baseConvert');
|
|||||||
* Converts `lodash` to an immutable auto-curried iteratee-first data-last version.
|
* Converts `lodash` to an immutable auto-curried iteratee-first data-last version.
|
||||||
*
|
*
|
||||||
* @param {Function} lodash The lodash function.
|
* @param {Function} lodash The lodash function.
|
||||||
|
* @param {Object} [options] The options object. See `baseConvert` for more details.
|
||||||
* @returns {Function} Returns the converted `lodash`.
|
* @returns {Function} Returns the converted `lodash`.
|
||||||
*/
|
*/
|
||||||
function browserConvert(lodash) {
|
function browserConvert(lodash, options) {
|
||||||
return baseConvert(lodash, lodash);
|
return baseConvert(lodash, lodash, undefined, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = browserConvert;
|
module.exports = browserConvert;
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ var baseConvert = require('./_baseConvert'),
|
|||||||
*
|
*
|
||||||
* @param {string} name The name of the function to wrap.
|
* @param {string} name The name of the function to wrap.
|
||||||
* @param {Function} [func] 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.
|
* @returns {Function|Object} Returns the converted function or object.
|
||||||
*/
|
*/
|
||||||
function convert(name, func) {
|
function convert(name, func, options) {
|
||||||
return baseConvert(util, name, func);
|
return baseConvert(util, name, func, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = convert;
|
module.exports = convert;
|
||||||
|
|||||||
@@ -46,8 +46,8 @@
|
|||||||
|
|
||||||
var convert = root.fp || (function() {
|
var convert = root.fp || (function() {
|
||||||
var baseConvert = load('../fp/_baseConvert.js');
|
var baseConvert = load('../fp/_baseConvert.js');
|
||||||
return function(name, func) {
|
return function(name, func, options) {
|
||||||
return baseConvert(_, name, func);
|
return baseConvert(_, name, func, options);
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -76,6 +76,101 @@
|
|||||||
console.log('Running lodash/fp tests.');
|
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');
|
QUnit.module('method arity checks');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user