diff --git a/lodash.js b/lodash.js index c0bdfd8c2..7f6387a59 100644 --- a/lodash.js +++ b/lodash.js @@ -10361,11 +10361,15 @@ } /** - * Checks if `value` is an empty object or collection. A value is considered - * empty if it's an `arguments` object, array, buffer, string, or jQuery-like - * collection with a length of `0` or has no own enumerable string keyed + * Checks if `value` is an empty object, collection, map, or set. + * + * Objects are considered empty if they have no own enumerable string keyed * properties. * + * Array-like values such as `arguments` objects, arrays, buffers, strings, or + * jQuery-like collections are considered empty if they have a `length` of `0`. + * Similarly, maps and sets are considered empty if they have a `size` of `0`. + * * @static * @memberOf _ * @since 0.1.0 @@ -10400,6 +10404,12 @@ return false; } } + if (isObjectLike(value)) { + var tag = getTag(value); + if (tag == mapTag || tag == setTag) { + return !value.size; + } + } return true; } diff --git a/test/test.js b/test/test.js index 0033b21fb..c6d925434 100644 --- a/test/test.js +++ b/test/test.js @@ -8744,6 +8744,38 @@ assert.strictEqual(_.isEmpty(new Foo([])), true); }); + QUnit.test('should work with maps', function(assert) { + assert.expect(4); + + if (Map) { + lodashStable.each([new Map, realm.map], function(map) { + assert.strictEqual(_.isEmpty(map), true); + map.set('a', 1); + assert.strictEqual(_.isEmpty(map), false); + map.clear(); + }); + } + else { + skipAssert(assert, 4); + } + }); + + QUnit.test('should work with sets', function(assert) { + assert.expect(4); + + if (Set) { + lodashStable.each([new Set, realm.set], function(set) { + assert.strictEqual(_.isEmpty(set), true); + set.add(1); + assert.strictEqual(_.isEmpty(set), false); + set.clear(); + }); + } + else { + skipAssert(assert, 4); + } + }); + QUnit.test('should not treat objects with negative lengths as array-like', function(assert) { assert.expect(1);