diff --git a/.internal/MapCache.js b/.internal/MapCache.js index 62206dd63..926d390be 100644 --- a/.internal/MapCache.js +++ b/.internal/MapCache.js @@ -1,17 +1,45 @@ -import mapCacheClear from './.internal/mapCacheClear.js'; -import mapCacheDelete from './.internal/mapCacheDelete.js'; -import mapCacheGet from './.internal/mapCacheGet.js'; -import mapCacheHas from './.internal/mapCacheHas.js'; -import mapCacheSet from './.internal/mapCacheSet.js'; + +import Hash from './.internal/Hash.js'; +import ListCache from './.internal/ListCache.js'; /** - * Creates a map cache object to store key-value pairs. + * Gets the data for `map`. * * @private - * @constructor - * @param {Array} [entries] The key-value pairs to cache. + * @param {Object} map The map to query. + * @param {string} key The reference key. + * @returns {*} Returns the map data. */ +function getMapData({ __data__ }, key) { + const data = __data__; + return isKeyable(key) + ? data[typeof key == 'string' ? 'string' : 'hash'] + : data.map; +} + +/** + * Checks if `value` is suitable for use as unique object key. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is suitable, else `false`. + */ +function isKeyable(value) { + const type = typeof value; + return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean') + ? (value !== '__proto__') + : (value === null); +} + class MapCache { + + /** + * Creates a map cache object to store key-value pairs. + * + * @private + * @constructor + * @param {Array} [entries] The key-value pairs to cache. + */ constructor(entries) { let index = -1; const length = entries == null ? 0 : entries.length; @@ -22,13 +50,72 @@ class MapCache { this.set(entry[0], entry[1]); } } + + /** + * Removes all key-value entries from the map. + * + * @memberOf MapCache + */ + clear() { + this.size = 0; + this.__data__ = { + 'hash': new Hash, + 'map': new (Map || ListCache), + 'string': new Hash + }; + } + + /** + * Removes `key` and its value from the map. + * + * @memberOf MapCache + * @param {string} key The key of the value to remove. + * @returns {boolean} Returns `true` if the entry was removed, else `false`. + */ + delete(key) { + const result = getMapData(this, key)['delete'](key); + this.size -= result ? 1 : 0; + return result; + } + + /** + * Gets the map value for `key`. + * + * @memberOf MapCache + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + get(key) { + return getMapData(this, key).get(key); + } + + /** + * Checks if a map value for `key` exists. + * + * @memberOf MapCache + * @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 getMapData(this, key).has(key); + } + + /** + * Sets the map `key` to `value`. + * + * @memberOf MapCache + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the map cache instance. + */ + set(key, value) { + const data = getMapData(this, key); + const size = data.size; + + data.set(key, value); + this.size += data.size == size ? 0 : 1; + return this; + } } -// Add methods to `MapCache`. -MapCache.prototype.clear = mapCacheClear; -MapCache.prototype['delete'] = mapCacheDelete; -MapCache.prototype.get = mapCacheGet; -MapCache.prototype.has = mapCacheHas; -MapCache.prototype.set = mapCacheSet; - export default MapCache; diff --git a/.internal/getMapData.js b/.internal/getMapData.js deleted file mode 100644 index d15ea4614..000000000 --- a/.internal/getMapData.js +++ /dev/null @@ -1,18 +0,0 @@ -import isKeyable from './.internal/isKeyable.js'; - -/** - * Gets the data for `map`. - * - * @private - * @param {Object} map The map to query. - * @param {string} key The reference key. - * @returns {*} Returns the map data. - */ -function getMapData({ __data__ }, key) { - const data = __data__; - return isKeyable(key) - ? data[typeof key == 'string' ? 'string' : 'hash'] - : data.map; -} - -export default getMapData; diff --git a/.internal/isKeyable.js b/.internal/isKeyable.js deleted file mode 100644 index 0931e86ef..000000000 --- a/.internal/isKeyable.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Checks if `value` is suitable for use as unique object key. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is suitable, else `false`. - */ -function isKeyable(value) { - const type = typeof value; - return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean') - ? (value !== '__proto__') - : (value === null); -} - -export default isKeyable; diff --git a/.internal/mapCacheClear.js b/.internal/mapCacheClear.js deleted file mode 100644 index e0be01516..000000000 --- a/.internal/mapCacheClear.js +++ /dev/null @@ -1,20 +0,0 @@ -import Hash from './.internal/Hash.js'; -import ListCache from './.internal/ListCache.js'; - -/** - * Removes all key-value entries from the map. - * - * @private - * @name clear - * @memberOf MapCache - */ -function mapCacheClear() { - this.size = 0; - this.__data__ = { - 'hash': new Hash, - 'map': new (Map || ListCache), - 'string': new Hash - }; -} - -export default mapCacheClear; diff --git a/.internal/mapCacheDelete.js b/.internal/mapCacheDelete.js deleted file mode 100644 index 924f15305..000000000 --- a/.internal/mapCacheDelete.js +++ /dev/null @@ -1,18 +0,0 @@ -import getMapData from './.internal/getMapData.js'; - -/** - * Removes `key` and its value from the map. - * - * @private - * @name delete - * @memberOf MapCache - * @param {string} key The key of the value to remove. - * @returns {boolean} Returns `true` if the entry was removed, else `false`. - */ -function mapCacheDelete(key) { - const result = getMapData(this, key)['delete'](key); - this.size -= result ? 1 : 0; - return result; -} - -export default mapCacheDelete; diff --git a/.internal/mapCacheGet.js b/.internal/mapCacheGet.js deleted file mode 100644 index daa9058b3..000000000 --- a/.internal/mapCacheGet.js +++ /dev/null @@ -1,16 +0,0 @@ -import getMapData from './.internal/getMapData.js'; - -/** - * Gets the map value for `key`. - * - * @private - * @name get - * @memberOf MapCache - * @param {string} key The key of the value to get. - * @returns {*} Returns the entry value. - */ -function mapCacheGet(key) { - return getMapData(this, key).get(key); -} - -export default mapCacheGet; diff --git a/.internal/mapCacheHas.js b/.internal/mapCacheHas.js deleted file mode 100644 index 49707bdde..000000000 --- a/.internal/mapCacheHas.js +++ /dev/null @@ -1,16 +0,0 @@ -import getMapData from './.internal/getMapData.js'; - -/** - * Checks if a map value for `key` exists. - * - * @private - * @name has - * @memberOf MapCache - * @param {string} key The key of the entry to check. - * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. - */ -function mapCacheHas(key) { - return getMapData(this, key).has(key); -} - -export default mapCacheHas; diff --git a/.internal/mapCacheSet.js b/.internal/mapCacheSet.js deleted file mode 100644 index a86bef00e..000000000 --- a/.internal/mapCacheSet.js +++ /dev/null @@ -1,22 +0,0 @@ -import getMapData from './.internal/getMapData.js'; - -/** - * Sets the map `key` to `value`. - * - * @private - * @name set - * @memberOf MapCache - * @param {string} key The key of the value to set. - * @param {*} value The value to set. - * @returns {Object} Returns the map cache instance. - */ -function mapCacheSet(key, value) { - const data = getMapData(this, key); - const size = data.size; - - data.set(key, value); - this.size += data.size == size ? 0 : 1; - return this; -} - -export default mapCacheSet;