You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 'use strict';
  8. var hasOwn = Object.prototype.hasOwnProperty;
  9. /**
  10. * Expose `MapCache`
  11. */
  12. module.exports = MapCache;
  13. /**
  14. * Creates a cache object to store key/value pairs.
  15. *
  16. * ```js
  17. * var cache = new MapCache();
  18. * ```
  19. *
  20. * @api public
  21. */
  22. function MapCache(data) {
  23. this.__data__ = data || {};
  24. }
  25. /**
  26. * Adds `value` to `key` on the cache.
  27. *
  28. * ```js
  29. * cache.set('foo', 'bar');
  30. * ```
  31. *
  32. * @param {String} `key` The key of the value to cache.
  33. * @param {*} `value` The value to cache.
  34. * @returns {Object} Returns the `Cache` object for chaining.
  35. * @api public
  36. */
  37. MapCache.prototype.set = function mapSet(key, value) {
  38. if (key !== '__proto__') {
  39. this.__data__[key] = value;
  40. }
  41. return this;
  42. };
  43. /**
  44. * Gets the cached value for `key`.
  45. *
  46. * ```js
  47. * cache.get('foo');
  48. * //=> 'bar'
  49. * ```
  50. *
  51. * @param {String} `key` The key of the value to get.
  52. * @returns {*} Returns the cached value.
  53. * @api public
  54. */
  55. MapCache.prototype.get = function mapGet(key) {
  56. return key === '__proto__' ? undefined : this.__data__[key];
  57. };
  58. /**
  59. * Checks if a cached value for `key` exists.
  60. *
  61. * ```js
  62. * cache.has('foo');
  63. * //=> true
  64. * ```
  65. *
  66. * @param {String} `key` The key of the entry to check.
  67. * @returns {Boolean} Returns `true` if an entry for `key` exists, else `false`.
  68. * @api public
  69. */
  70. MapCache.prototype.has = function mapHas(key) {
  71. return key !== '__proto__' && hasOwn.call(this.__data__, key);
  72. };
  73. /**
  74. * Removes `key` and its value from the cache.
  75. *
  76. * ```js
  77. * cache.del('foo');
  78. * ```
  79. * @title .del
  80. * @param {String} `key` The key of the value to remove.
  81. * @returns {Boolean} Returns `true` if the entry was removed successfully, else `false`.
  82. * @api public
  83. */
  84. MapCache.prototype.del = function mapDelete(key) {
  85. return this.has(key) && delete this.__data__[key];
  86. };