Add callback and thisArg arguments to _.flatten. [closes #204]

Former-commit-id: 166d6af35c3905c87498ee74abd143f6fdba451d
This commit is contained in:
John-David Dalton
2013-03-03 23:12:13 -08:00
parent d88da3589d
commit 41e401b05e
10 changed files with 303 additions and 95 deletions

View File

@@ -789,6 +789,46 @@
QUnit.module('lodash.flatten');
(function() {
var array = [{ 'a': [1, [2]] }, { 'a': [3] }];
test('should work with a `callback`', function() {
var actual = _.flatten(array, function(value) {
return value.a;
});
deepEqual(actual, [1, 2, 3]);
});
test('should work with `isShallow` and `callback`', function() {
var actual = _.flatten(array, true, function(value) {
return value.a;
});
deepEqual(actual, [1, [2], 3]);
});
test('should pass the correct `callback` arguments', function() {
var args;
_.flatten(array, function() {
args || (args = slice.call(arguments));
});
deepEqual(args, [{ 'a': [1, [2]] }, 0, array]);
});
test('supports the `thisArg` argument', function() {
var actual = _.flatten(array, function(value, index) {
return this[index].a;
}, array);
deepEqual(actual, [1, 2, 3]);
});
test('should work with a string for `callback`', function() {
deepEqual(_.flatten(array, 'a'), [1, 2, 3]);
});
test('should treat sparse arrays as dense', function() {
var array = [[1, 2, 3], Array(3)],
expected = [1, 2, 3],