Skip to content

Commit 71f49dd

Browse files
committed
Move Stack modules into Stack.
1 parent c64a3bd commit 71f49dd

File tree

6 files changed

+84
-114
lines changed

6 files changed

+84
-114
lines changed

.internal/Stack.js

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,93 @@
11
import ListCache from './.internal/ListCache.js';
2-
import stackClear from './.internal/stackClear.js';
3-
import stackDelete from './.internal/stackDelete.js';
4-
import stackGet from './.internal/stackGet.js';
5-
import stackHas from './.internal/stackHas.js';
6-
import stackSet from './.internal/stackSet.js';
7-
8-
/**
9-
* Creates a stack cache object to store key-value pairs.
10-
*
11-
* @private
12-
* @constructor
13-
* @param {Array} [entries] The key-value pairs to cache.
14-
*/
2+
import MapCache from './.internal/MapCache.js';
3+
4+
/** Used as the size to enable large array optimizations. */
5+
const LARGE_ARRAY_SIZE = 200;
6+
157
class Stack {
8+
9+
/**
10+
* Creates a stack cache object to store key-value pairs.
11+
*
12+
* @private
13+
* @constructor
14+
* @param {Array} [entries] The key-value pairs to cache.
15+
*/
1616
constructor(entries) {
1717
const data = this.__data__ = new ListCache(entries);
1818
this.size = data.size;
1919
}
20-
}
2120

22-
// Add methods to `Stack`.
23-
Stack.prototype.clear = stackClear;
24-
Stack.prototype['delete'] = stackDelete;
25-
Stack.prototype.get = stackGet;
26-
Stack.prototype.has = stackHas;
27-
Stack.prototype.set = stackSet;
21+
/**
22+
* Removes all key-value entries from the stack.
23+
*
24+
* @memberOf Stack
25+
*/
26+
clear() {
27+
this.__data__ = new ListCache;
28+
this.size = 0;
29+
}
30+
31+
/**
32+
* Removes `key` and its value from the stack.
33+
*
34+
* @memberOf Stack
35+
* @param {string} key The key of the value to remove.
36+
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
37+
*/
38+
delete(key) {
39+
const data = this.__data__;
40+
const result = data['delete'](key);
41+
42+
this.size = data.size;
43+
return result;
44+
}
45+
46+
/**
47+
* Gets the stack value for `key`.
48+
*
49+
* @memberOf Stack
50+
* @param {string} key The key of the value to get.
51+
* @returns {*} Returns the entry value.
52+
*/
53+
get(key) {
54+
return this.__data__.get(key);
55+
}
56+
57+
/**
58+
* Checks if a stack value for `key` exists.
59+
*
60+
* @memberOf Stack
61+
* @param {string} key The key of the entry to check.
62+
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
63+
*/
64+
has(key) {
65+
return this.__data__.has(key);
66+
}
67+
68+
/**
69+
* Sets the stack `key` to `value`.
70+
*
71+
* @memberOf Stack
72+
* @param {string} key The key of the value to set.
73+
* @param {*} value The value to set.
74+
* @returns {Object} Returns the stack cache instance.
75+
*/
76+
set(key, value) {
77+
let data = this.__data__;
78+
if (data instanceof ListCache) {
79+
const pairs = data.__data__;
80+
if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) {
81+
pairs.push([key, value]);
82+
this.size = ++data.size;
83+
return this;
84+
}
85+
data = this.__data__ = new MapCache(pairs);
86+
}
87+
data.set(key, value);
88+
this.size = data.size;
89+
return this;
90+
}
91+
}
2892

2993
export default Stack;

.internal/stackClear.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

.internal/stackDelete.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

.internal/stackGet.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

.internal/stackHas.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

.internal/stackSet.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)