Add Stack#size.

This commit is contained in:
John-David Dalton
2016-08-27 21:40:24 -07:00
parent 6651023f7f
commit 7c398f8019
3 changed files with 27 additions and 18 deletions

View File

@@ -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;
/**

View File

@@ -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() {

View File

@@ -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);
});
});