Add _.ary. [closes #814]

This commit is contained in:
John-David Dalton
2014-12-03 22:57:46 -08:00
parent 08b956cf88
commit 48a7e40520
2 changed files with 165 additions and 17 deletions

View File

@@ -879,6 +879,114 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.ary');
(function() {
function fn() {
return slice.call(arguments);
}
test('should cap the numer of params provided to `func`', 2, function() {
var actual = _.map(['6', '8', '10'], _.ary(parseInt, 1));
deepEqual(actual, [6, 8, 10]);
var capped = _.ary(fn, 2);
deepEqual(capped('a', 'b', 'c', 'd'), ['a', 'b']);
});
test('should work when provided less than the capped numer of arguments', 1, function() {
var capped = _.ary(fn, 3);
deepEqual(capped('a'), ['a']);
});
test('should work when combined with other methods that use metadata', 2, function() {
var array = ['a', 'b', 'c'],
includes = _.curry(_.rearg(_.ary(_.includes, 2), 1, 0), 2);
strictEqual(includes('b')(array, 2), true);
if (!isNpm) {
includes = _(_.includes).ary(2).rearg(1, 0).curry(2).value();
strictEqual(includes('b')(array, 2), true);
}
else {
skipTest();
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.rearg');
(function() {
function fn() {
return slice.call(arguments);
}
test('should reorder arguments provided to `func`', 1, function() {
var rearged = _.rearg(fn, [2, 0, 1]);
deepEqual(rearged('b', 'c', 'a'), ['a', 'b', 'c']);
});
test('should work with repeated indexes', 1, function() {
var rearged = _.rearg(fn, [1, 1, 1]);
deepEqual(rearged('c', 'a', 'b'), ['a', 'a', 'a']);
});
test('should use `undefined` for nonexistent indexes', 1, function() {
var rearged = _.rearg(fn, [1, 4]);
deepEqual(rearged('b', 'a', 'c'), ['a', undefined, 'c']);
});
test('should use `undefined` for non-index values', 1, function() {
var values = _.reject(empties, function(value) {
return value === 0 || _.isArray(value);
}).concat(-1, 1.1);
var expected = _.map(values, _.constant([undefined, 'b', 'c']));
var actual = _.map(values, function(value) {
var rearged = _.rearg(fn, [value]);
return rearged('a', 'b', 'c');
});
deepEqual(actual, expected);
});
test('should not rearrange arguments when no indexes are provided', 2, function() {
var rearged = _.rearg(fn);
deepEqual(rearged('a', 'b', 'c'), ['a', 'b', 'c']);
rearged = _.rearg(fn, [], []);
deepEqual(rearged('a', 'b', 'c'), ['a', 'b', 'c']);
});
test('should accept multiple index arguments', 1, function() {
var rearged = _.rearg(fn, 2, 0, 1);
deepEqual(rearged('b', 'c', 'a'), ['a', 'b', 'c']);
});
test('should accept multiple arrays of indexes', 1, function() {
var rearged = _.rearg(fn, [2], [0, 1]);
deepEqual(rearged('b', 'c', 'a'), ['a', 'b', 'c']);
});
test('should work with fewer indexes than arguments', 1, function() {
var rearged = _.rearg(fn, [1, 0]);
deepEqual(rearged('b', 'a', 'c'), ['a', 'b', 'c']);
});
test('should work on functions that have been rearged', 1, function() {
var rearged1 = _.rearg(fn, 2, 1, 0),
rearged2 = _.rearg(rearged1, 1, 0, 2);
deepEqual(rearged2('b', 'c', 'a'), ['a', 'b', 'c']);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.assign');
(function() {
@@ -13649,6 +13757,7 @@
var rejectFalsey = [
'after',
'ary',
'backflow',
'before',
'bind',
@@ -13739,7 +13848,7 @@
});
});
test('should throw a TypeError for falsey arguments', 21, function() {
test('should throw a TypeError for falsey arguments', 22, function() {
_.each(rejectFalsey, function(methodName) {
var expected = _.map(falsey, _.constant(true)),
func = _[methodName];