diff --git a/.internal/Stack.js b/.internal/Stack.js index 5e9370ef5..1f79959f3 100644 --- a/.internal/Stack.js +++ b/.internal/Stack.js @@ -1,29 +1,93 @@ import ListCache from './.internal/ListCache.js'; -import stackClear from './.internal/stackClear.js'; -import stackDelete from './.internal/stackDelete.js'; -import stackGet from './.internal/stackGet.js'; -import stackHas from './.internal/stackHas.js'; -import stackSet from './.internal/stackSet.js'; +import MapCache from './.internal/MapCache.js'; + +/** Used as the size to enable large array optimizations. */ +const LARGE_ARRAY_SIZE = 200; -/** - * Creates a stack cache object to store key-value pairs. - * - * @private - * @constructor - * @param {Array} [entries] The key-value pairs to cache. - */ class Stack { + + /** + * Creates a stack cache object to store key-value pairs. + * + * @private + * @constructor + * @param {Array} [entries] The key-value pairs to cache. + */ constructor(entries) { const data = this.__data__ = new ListCache(entries); this.size = data.size; } + + /** + * Removes all key-value entries from the stack. + * + * @memberOf Stack + */ + clear() { + this.__data__ = new ListCache; + this.size = 0; + } + + /** + * Removes `key` and its value from the stack. + * + * @memberOf Stack + * @param {string} key The key of the value to remove. + * @returns {boolean} Returns `true` if the entry was removed, else `false`. + */ + delete(key) { + const data = this.__data__; + const result = data['delete'](key); + + this.size = data.size; + return result; + } + + /** + * Gets the stack value for `key`. + * + * @memberOf Stack + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + get(key) { + return this.__data__.get(key); + } + + /** + * Checks if a stack value for `key` exists. + * + * @memberOf Stack + * @param {string} key The key of the entry to check. + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. + */ + has(key) { + return this.__data__.has(key); + } + + /** + * Sets the stack `key` to `value`. + * + * @memberOf Stack + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the stack cache instance. + */ + set(key, value) { + let data = this.__data__; + if (data instanceof ListCache) { + const pairs = data.__data__; + if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) { + pairs.push([key, value]); + this.size = ++data.size; + return this; + } + data = this.__data__ = new MapCache(pairs); + } + data.set(key, value); + this.size = data.size; + return this; + } } -// Add methods to `Stack`. -Stack.prototype.clear = stackClear; -Stack.prototype['delete'] = stackDelete; -Stack.prototype.get = stackGet; -Stack.prototype.has = stackHas; -Stack.prototype.set = stackSet; - export default Stack; diff --git a/.internal/stackClear.js b/.internal/stackClear.js deleted file mode 100644 index 9873e7f01..000000000 --- a/.internal/stackClear.js +++ /dev/null @@ -1,15 +0,0 @@ -import ListCache from './.internal/ListCache.js'; - -/** - * Removes all key-value entries from the stack. - * - * @private - * @name clear - * @memberOf Stack - */ -function stackClear() { - this.__data__ = new ListCache; - this.size = 0; -} - -export default stackClear; diff --git a/.internal/stackDelete.js b/.internal/stackDelete.js deleted file mode 100644 index 1f6cfbba4..000000000 --- a/.internal/stackDelete.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Removes `key` and its value from the stack. - * - * @private - * @name delete - * @memberOf Stack - * @param {string} key The key of the value to remove. - * @returns {boolean} Returns `true` if the entry was removed, else `false`. - */ -function stackDelete(key) { - const data = this.__data__; - const result = data['delete'](key); - - this.size = data.size; - return result; -} - -export default stackDelete; diff --git a/.internal/stackGet.js b/.internal/stackGet.js deleted file mode 100644 index 139b9acc1..000000000 --- a/.internal/stackGet.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Gets the stack value for `key`. - * - * @private - * @name get - * @memberOf Stack - * @param {string} key The key of the value to get. - * @returns {*} Returns the entry value. - */ -function stackGet(key) { - return this.__data__.get(key); -} - -export default stackGet; diff --git a/.internal/stackHas.js b/.internal/stackHas.js deleted file mode 100644 index bc25f5a7e..000000000 --- a/.internal/stackHas.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Checks if a stack value for `key` exists. - * - * @private - * @name has - * @memberOf Stack - * @param {string} key The key of the entry to check. - * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. - */ -function stackHas(key) { - return this.__data__.has(key); -} - -export default stackHas; diff --git a/.internal/stackSet.js b/.internal/stackSet.js deleted file mode 100644 index 97278c912..000000000 --- a/.internal/stackSet.js +++ /dev/null @@ -1,33 +0,0 @@ -import ListCache from './.internal/ListCache.js'; -import MapCache from './.internal/MapCache.js'; - -/** Used as the size to enable large array optimizations. */ -const LARGE_ARRAY_SIZE = 200; - -/** - * Sets the stack `key` to `value`. - * - * @private - * @name set - * @memberOf Stack - * @param {string} key The key of the value to set. - * @param {*} value The value to set. - * @returns {Object} Returns the stack cache instance. - */ -function stackSet(key, value) { - let data = this.__data__; - if (data instanceof ListCache) { - const pairs = data.__data__; - if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) { - pairs.push([key, value]); - this.size = ++data.size; - return this; - } - data = this.__data__ = new MapCache(pairs); - } - data.set(key, value); - this.size = data.size; - return this; -} - -export default stackSet;