Add support for maps and sets to _.size.

This commit is contained in:
John-David Dalton
2016-03-21 20:16:15 -07:00
parent 6d587bdf34
commit 25eb4df563
2 changed files with 34 additions and 0 deletions

View File

@@ -8572,6 +8572,12 @@
var result = collection.length;
return (result && isString(collection)) ? stringSize(collection) : result;
}
if (isObjectLike(collection)) {
var tag = getTag(collection);
if (tag == mapTag || tag == setTag) {
return collection.size;
}
}
return keys(collection).length;
}

View File

@@ -19104,6 +19104,34 @@
assert.strictEqual(_.size(new Foo(array)), 3);
});
QUnit.test('should work with maps', function(assert) {
assert.expect(1);
if (Map) {
var map = new Map;
map.set('a', 1);
map.set('b', 2);
assert.strictEqual(_.size(map), 2);
}
else {
skipAssert(assert);
}
});
QUnit.test('should work with sets', function(assert) {
assert.expect(1);
if (Set) {
var set = new Set;
set.add(1);
set.add(2);
assert.strictEqual(_.size(set), 2);
}
else {
skipAssert(assert);
}
});
QUnit.test('should not treat objects with negative lengths as array-like', function(assert) {
assert.expect(1);