From 400b5c09d7cf789db3dd693126c77fa4e0910473 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 10 Jan 2017 10:07:19 -0800 Subject: [PATCH] Move `ListCache` modules into `ListCache`. --- .internal/ListCache.js | 110 +++++++++++++++++++++++++++++------ .internal/listCacheClear.js | 13 ----- .internal/listCacheDelete.js | 32 ---------- .internal/listCacheGet.js | 19 ------ .internal/listCacheHas.js | 16 ----- .internal/listCacheSet.js | 26 --------- 6 files changed, 91 insertions(+), 125 deletions(-) delete mode 100644 .internal/listCacheClear.js delete mode 100644 .internal/listCacheDelete.js delete mode 100644 .internal/listCacheGet.js delete mode 100644 .internal/listCacheHas.js delete mode 100644 .internal/listCacheSet.js diff --git a/.internal/ListCache.js b/.internal/ListCache.js index 37244e6c6..4d9495282 100644 --- a/.internal/ListCache.js +++ b/.internal/ListCache.js @@ -1,17 +1,17 @@ -import listCacheClear from './.internal/listCacheClear.js'; -import listCacheDelete from './.internal/listCacheDelete.js'; -import listCacheGet from './.internal/listCacheGet.js'; -import listCacheHas from './.internal/listCacheHas.js'; -import listCacheSet from './.internal/listCacheSet.js'; +import assocIndexOf from './.internal/assocIndexOf.js'; + +/** Built-in value references. */ +const splice = Array.prototype.splice; -/** - * Creates an list cache object. - * - * @private - * @constructor - * @param {Array} [entries] The key-value pairs to cache. - */ class ListCache { + + /** + * Creates an list cache object. + * + * @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 +22,85 @@ class ListCache { this.set(entry[0], entry[1]); } } + + /** + * Removes all key-value entries from the list cache. + * + * @memberOf ListCache + */ + clear() { + this.__data__ = []; + this.size = 0; + } + + /** + * Removes `key` and its value from the list cache. + * + * @memberOf ListCache + * @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 index = assocIndexOf(data, key); + + if (index < 0) { + return false; + } + const lastIndex = data.length - 1; + if (index == lastIndex) { + data.pop(); + } else { + splice.call(data, index, 1); + } + --this.size; + return true; + } + + /** + * Gets the list cache value for `key`. + * + * @memberOf ListCache + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + get(key) { + const data = this.__data__; + const index = assocIndexOf(data, key); + return index < 0 ? undefined : data[index][1]; + } + + /** + * Checks if a list cache value for `key` exists. + * + * @memberOf ListCache + * @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 assocIndexOf(this.__data__, key) > -1; + } + + /** + * Sets the list cache `key` to `value`. + * + * @memberOf ListCache + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the list cache instance. + */ + set(key, value) { + const data = this.__data__; + const index = assocIndexOf(data, key); + + if (index < 0) { + ++this.size; + data.push([key, value]); + } else { + data[index][1] = value; + } + return this; + } } -// Add methods to `ListCache`. -ListCache.prototype.clear = listCacheClear; -ListCache.prototype['delete'] = listCacheDelete; -ListCache.prototype.get = listCacheGet; -ListCache.prototype.has = listCacheHas; -ListCache.prototype.set = listCacheSet; - export default ListCache; diff --git a/.internal/listCacheClear.js b/.internal/listCacheClear.js deleted file mode 100644 index 8cfa1843c..000000000 --- a/.internal/listCacheClear.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Removes all key-value entries from the list cache. - * - * @private - * @name clear - * @memberOf ListCache - */ -function listCacheClear() { - this.__data__ = []; - this.size = 0; -} - -export default listCacheClear; diff --git a/.internal/listCacheDelete.js b/.internal/listCacheDelete.js deleted file mode 100644 index 7bd64957b..000000000 --- a/.internal/listCacheDelete.js +++ /dev/null @@ -1,32 +0,0 @@ -import assocIndexOf from './.internal/assocIndexOf.js'; - -/** Built-in value references. */ -const splice = Array.prototype.splice; - -/** - * Removes `key` and its value from the list cache. - * - * @private - * @name delete - * @memberOf ListCache - * @param {string} key The key of the value to remove. - * @returns {boolean} Returns `true` if the entry was removed, else `false`. - */ -function listCacheDelete(key) { - const data = this.__data__; - const index = assocIndexOf(data, key); - - if (index < 0) { - return false; - } - const lastIndex = data.length - 1; - if (index == lastIndex) { - data.pop(); - } else { - splice.call(data, index, 1); - } - --this.size; - return true; -} - -export default listCacheDelete; diff --git a/.internal/listCacheGet.js b/.internal/listCacheGet.js deleted file mode 100644 index fd669669e..000000000 --- a/.internal/listCacheGet.js +++ /dev/null @@ -1,19 +0,0 @@ -import assocIndexOf from './.internal/assocIndexOf.js'; - -/** - * Gets the list cache value for `key`. - * - * @private - * @name get - * @memberOf ListCache - * @param {string} key The key of the value to get. - * @returns {*} Returns the entry value. - */ -function listCacheGet(key) { - const data = this.__data__; - const index = assocIndexOf(data, key); - - return index < 0 ? undefined : data[index][1]; -} - -export default listCacheGet; diff --git a/.internal/listCacheHas.js b/.internal/listCacheHas.js deleted file mode 100644 index 086910572..000000000 --- a/.internal/listCacheHas.js +++ /dev/null @@ -1,16 +0,0 @@ -import assocIndexOf from './.internal/assocIndexOf.js'; - -/** - * Checks if a list cache value for `key` exists. - * - * @private - * @name has - * @memberOf ListCache - * @param {string} key The key of the entry to check. - * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. - */ -function listCacheHas(key) { - return assocIndexOf(this.__data__, key) > -1; -} - -export default listCacheHas; diff --git a/.internal/listCacheSet.js b/.internal/listCacheSet.js deleted file mode 100644 index cf610ba62..000000000 --- a/.internal/listCacheSet.js +++ /dev/null @@ -1,26 +0,0 @@ -import assocIndexOf from './.internal/assocIndexOf.js'; - -/** - * Sets the list cache `key` to `value`. - * - * @private - * @name set - * @memberOf ListCache - * @param {string} key The key of the value to set. - * @param {*} value The value to set. - * @returns {Object} Returns the list cache instance. - */ -function listCacheSet(key, value) { - const data = this.__data__; - const index = assocIndexOf(data, key); - - if (index < 0) { - ++this.size; - data.push([key, value]); - } else { - data[index][1] = value; - } - return this; -} - -export default listCacheSet;