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

@@ -821,8 +821,7 @@
var start = _.after(2, _.once(QUnit.start));
build(['-s', 'underscore'], function(data) {
var last,
array = [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }],
var array = [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }],
basename = path.basename(data.outputPath, '.js'),
context = createContext();
@@ -869,6 +868,7 @@
equal(actual, _.first(array), '_.find: ' + basename);
var last;
actual = lodash.forEach(array, function(value) {
last = value;
return false;
@@ -877,6 +877,15 @@
equal(last, _.last(array), '_.forEach should not exit early: ' + basename);
equal(actual, undefined, '_.forEach should return `undefined`: ' + basename);
array = [{ 'a': [1, 2] }, { 'a': [3] }];
actual = lodash.flatten(array, function(value, index) {
return this[index].a;
}, array);
deepEqual(actual, array, '_.flatten should should ignore `callback` and `thisArg`: ' + basename);
deepEqual(lodash.flatten(array, 'a'), array, '_.flatten should should ignore string `callback` values: ' + basename);
object = { 'length': 0, 'splice': Array.prototype.splice };
equal(lodash.isEmpty(object), false, '_.isEmpty should return `false` for jQuery/MooTools DOM query collections: ' + basename);

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],