From 7c398f80198878e51c888d5286d6e564baadd923 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 27 Aug 2016 21:40:24 -0700 Subject: [PATCH] Add `Stack#size`. --- lodash.js | 28 ++++++++++++++++++---------- test/test-fp.js | 8 ++++---- test/test.js | 9 +++++---- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lodash.js b/lodash.js index 9d5829431..e119f8c8c 100644 --- a/lodash.js +++ b/lodash.js @@ -1900,8 +1900,8 @@ * @memberOf Hash */ function hashClear() { - this.size = 0; this.__data__ = nativeCreate ? nativeCreate(null) : {}; + this.size = 0; } /** @@ -2004,8 +2004,8 @@ * @memberOf ListCache */ function listCacheClear() { - this.size = 0; this.__data__ = []; + this.size = 0; } /** @@ -2258,7 +2258,8 @@ * @param {Array} [entries] The key-value pairs to cache. */ function Stack(entries) { - this.__data__ = new ListCache(entries); + var data = this.__data__ = new ListCache(entries); + this.size = data.size; } /** @@ -2270,6 +2271,7 @@ */ function stackClear() { this.__data__ = new ListCache; + this.size = 0; } /** @@ -2282,7 +2284,11 @@ * @returns {boolean} Returns `true` if the entry was removed, else `false`. */ function stackDelete(key) { - return this.__data__['delete'](key); + var data = this.__data__, + result = data['delete'](key); + + this.size = data.size; + return result; } /** @@ -2322,16 +2328,18 @@ * @returns {Object} Returns the stack cache instance. */ function stackSet(key, value) { - var cache = this.__data__; - if (cache instanceof ListCache) { - var pairs = cache.__data__; + var data = this.__data__; + if (data instanceof ListCache) { + var pairs = data.__data__; if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) { pairs.push([key, value]); + this.size = ++data.size; return this; } - cache = this.__data__ = new MapCache(pairs); + data = this.__data__ = new MapCache(pairs); } - cache.set(key, value); + data.set(key, value); + this.size = data.size; return this; } @@ -10343,7 +10351,7 @@ return memoized; } - // Assign cache to `_.memoize`. + // Expose `MapCache`. memoize.Cache = MapCache; /** diff --git a/test/test-fp.js b/test/test-fp.js index 4a18501f5..869fc4c65 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -1309,7 +1309,7 @@ var args, iteration = 0, objects = [{ 'a': 1 }, { 'a': 2 }], - stack = { '__data__': { '__data__': [objects, objects.slice().reverse()], 'size': 0 } }, + stack = { '__data__': { '__data__': [objects, objects.slice().reverse()], 'size': 2 }, 'size': 2 }, expected = [1, 2, 'a', objects[0], objects[1], stack]; fp.isEqualWith(function() { @@ -1335,7 +1335,7 @@ var args, objects = [{ 'a': 1 }, { 'a': 2 }], - stack = { '__data__': { '__data__': [], 'size': 0 } }, + stack = { '__data__': { '__data__': [], 'size': 0 }, 'size': 0 }, expected = [2, 1, 'a', objects[1], objects[0], stack]; fp.isMatchWith(function() { @@ -1444,7 +1444,7 @@ assert.expect(1); var args, - stack = { '__data__': { '__data__': [], 'size': 0 } }, + stack = { '__data__': { '__data__': [], 'size': 0 }, 'size': 0 }, expected = [[1, 2], [3], 'a', { 'a': [1, 2] }, { 'a': [3] }, stack]; fp.mergeWith(function() { @@ -1477,7 +1477,7 @@ assert.expect(1); var args, - stack = { '__data__': { '__data__': [], 'size': 0 } }, + stack = { '__data__': { '__data__': [], 'size': 0 }, 'size': 0 }, expected = [[1, 2], [3], 'a', { 'a': [1, 2] }, { 'a': [3] }, stack]; fp.mergeAllWith(function() { diff --git a/test/test.js b/test/test.js index cdbc5dd9f..2088a164e 100644 --- a/test/test.js +++ b/test/test.js @@ -1062,8 +1062,10 @@ } lodashStable.forOwn(createCaches(pairs), function(cache, kind) { + var isLarge = /^large/.test(kind); + QUnit.test('should implement a `Map` interface for ' + kind, function(assert) { - assert.expect(82); + assert.expect(83); lodashStable.each(keys, function(key, index) { var value = pairs[index][1]; @@ -1078,6 +1080,7 @@ assert.strictEqual(cache.has(key), true); }); + assert.strictEqual(cache.size, isLarge ? LARGE_ARRAY_SIZE : keys.length); assert.strictEqual(cache.clear(), undefined); assert.ok(lodashStable.every(keys, function(key) { return !cache.has(key); @@ -1090,9 +1093,7 @@ assert.expect(10); lodashStable.each(keys, function(key) { - cache.set(key, 1); - cache.set(key, 2); - + cache.set(key, 1).set(key, 2); assert.strictEqual(cache.get(key), 2); }); });