mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 23:57:49 +00:00
Add support for maps and sets to _.size.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
28
test/test.js
28
test/test.js
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user