2 * map-cache <https://github.com/jonschlinkert/map-cache>
4 * Copyright (c) 2015, Jon Schlinkert.
5 * Licensed under the MIT License.
10 var hasOwn = Object.prototype.hasOwnProperty;
16 module.exports = MapCache;
19 * Creates a cache object to store key/value pairs.
22 * var cache = new MapCache();
28 function MapCache(data) {
29 this.__data__ = data || {};
33 * Adds `value` to `key` on the cache.
36 * cache.set('foo', 'bar');
39 * @param {String} `key` The key of the value to cache.
40 * @param {*} `value` The value to cache.
41 * @returns {Object} Returns the `Cache` object for chaining.
45 MapCache.prototype.set = function mapSet(key, value) {
46 if (key !== '__proto__') {
47 this.__data__[key] = value;
53 * Gets the cached value for `key`.
60 * @param {String} `key` The key of the value to get.
61 * @returns {*} Returns the cached value.
65 MapCache.prototype.get = function mapGet(key) {
66 return key === '__proto__' ? undefined : this.__data__[key];
70 * Checks if a cached value for `key` exists.
77 * @param {String} `key` The key of the entry to check.
78 * @returns {Boolean} Returns `true` if an entry for `key` exists, else `false`.
82 MapCache.prototype.has = function mapHas(key) {
83 return key !== '__proto__' && hasOwn.call(this.__data__, key);
87 * Removes `key` and its value from the cache.
93 * @param {String} `key` The key of the value to remove.
94 * @returns {Boolean} Returns `true` if the entry was removed successfully, else `false`.
98 MapCache.prototype.del = function mapDelete(key) {
99 return this.has(key) && delete this.__data__[key];