| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /*! |
| 2 | * map-cache <https://github.com/jonschlinkert/map-cache> |
| 3 | * |
| 4 | * Copyright (c) 2015, Jon Schlinkert. |
| 5 | * Licensed under the MIT License. |
| 6 | */ |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | var hasOwn = Object.prototype.hasOwnProperty; |
| 11 | |
| 12 | /** |
| 13 | * Expose `MapCache` |
| 14 | */ |
| 15 | |
| 16 | module.exports = MapCache; |
| 17 | |
| 18 | /** |
| 19 | * Creates a cache object to store key/value pairs. |
| 20 | * |
| 21 | * ```js |
| 22 | * var cache = new MapCache(); |
| 23 | * ``` |
| 24 | * |
| 25 | * @api public |
| 26 | */ |
| 27 | |
| 28 | function MapCache(data) { |
| 29 | this.__data__ = data || {}; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Adds `value` to `key` on the cache. |
| 34 | * |
| 35 | * ```js |
| 36 | * cache.set('foo', 'bar'); |
| 37 | * ``` |
| 38 | * |
| 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. |
| 42 | * @api public |
| 43 | */ |
| 44 | |
| 45 | MapCache.prototype.set = function mapSet(key, value) { |
| 46 | if (key !== '__proto__') { |
| 47 | this.__data__[key] = value; |
| 48 | } |
| 49 | return this; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Gets the cached value for `key`. |
| 54 | * |
| 55 | * ```js |
| 56 | * cache.get('foo'); |
| 57 | * //=> 'bar' |
| 58 | * ``` |
| 59 | * |
| 60 | * @param {String} `key` The key of the value to get. |
| 61 | * @returns {*} Returns the cached value. |
| 62 | * @api public |
| 63 | */ |
| 64 | |
| 65 | MapCache.prototype.get = function mapGet(key) { |
| 66 | return key === '__proto__' ? undefined : this.__data__[key]; |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * Checks if a cached value for `key` exists. |
| 71 | * |
| 72 | * ```js |
| 73 | * cache.has('foo'); |
| 74 | * //=> true |
| 75 | * ``` |
| 76 | * |
| 77 | * @param {String} `key` The key of the entry to check. |
| 78 | * @returns {Boolean} Returns `true` if an entry for `key` exists, else `false`. |
| 79 | * @api public |
| 80 | */ |
| 81 | |
| 82 | MapCache.prototype.has = function mapHas(key) { |
| 83 | return key !== '__proto__' && hasOwn.call(this.__data__, key); |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Removes `key` and its value from the cache. |
| 88 | * |
| 89 | * ```js |
| 90 | * cache.del('foo'); |
| 91 | * ``` |
| 92 | * @title .del |
| 93 | * @param {String} `key` The key of the value to remove. |
| 94 | * @returns {Boolean} Returns `true` if the entry was removed successfully, else `false`. |
| 95 | * @api public |
| 96 | */ |
| 97 | |
| 98 | MapCache.prototype.del = function mapDelete(key) { |
| 99 | return this.has(key) && delete this.__data__[key]; |
| 100 | }; |