Geen omschrijving

index.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*!
  2. * fragment-cache <https://github.com/jonschlinkert/fragment-cache>
  3. *
  4. * Copyright (c) 2016-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var MapCache = require('map-cache');
  9. /**
  10. * Create a new `FragmentCache` with an optional object to use for `caches`.
  11. *
  12. * ```js
  13. * var fragment = new FragmentCache();
  14. * ```
  15. * @name FragmentCache
  16. * @param {String} `cacheName`
  17. * @return {Object} Returns the [map-cache][] instance.
  18. * @api public
  19. */
  20. function FragmentCache(caches) {
  21. this.caches = caches || {};
  22. }
  23. /**
  24. * Prototype
  25. */
  26. FragmentCache.prototype = {
  27. /**
  28. * Get cache `name` from the `fragment.caches` object. Creates a new
  29. * `MapCache` if it doesn't already exist.
  30. *
  31. * ```js
  32. * var cache = fragment.cache('files');
  33. * console.log(fragment.caches.hasOwnProperty('files'));
  34. * //=> true
  35. * ```
  36. * @name .cache
  37. * @param {String} `cacheName`
  38. * @return {Object} Returns the [map-cache][] instance.
  39. * @api public
  40. */
  41. cache: function(cacheName) {
  42. return this.caches[cacheName] || (this.caches[cacheName] = new MapCache());
  43. },
  44. /**
  45. * Set a value for property `key` on cache `name`
  46. *
  47. * ```js
  48. * fragment.set('files', 'somefile.js', new File({path: 'somefile.js'}));
  49. * ```
  50. * @name .set
  51. * @param {String} `name`
  52. * @param {String} `key` Property name to set
  53. * @param {any} `val` The value of `key`
  54. * @return {Object} The cache instance for chaining
  55. * @api public
  56. */
  57. set: function(cacheName, key, val) {
  58. var cache = this.cache(cacheName);
  59. cache.set(key, val);
  60. return cache;
  61. },
  62. /**
  63. * Returns true if a non-undefined value is set for `key` on fragment cache `name`.
  64. *
  65. * ```js
  66. * var cache = fragment.cache('files');
  67. * cache.set('somefile.js');
  68. *
  69. * console.log(cache.has('somefile.js'));
  70. * //=> true
  71. *
  72. * console.log(cache.has('some-other-file.js'));
  73. * //=> false
  74. * ```
  75. * @name .has
  76. * @param {String} `name` Cache name
  77. * @param {String} `key` Optionally specify a property to check for on cache `name`
  78. * @return {Boolean}
  79. * @api public
  80. */
  81. has: function(cacheName, key) {
  82. return typeof this.get(cacheName, key) !== 'undefined';
  83. },
  84. /**
  85. * Get `name`, or if specified, the value of `key`. Invokes the [cache]() method,
  86. * so that cache `name` will be created it doesn't already exist. If `key` is not passed,
  87. * the entire cache (`name`) is returned.
  88. *
  89. * ```js
  90. * var Vinyl = require('vinyl');
  91. * var cache = fragment.cache('files');
  92. * cache.set('somefile.js', new Vinyl({path: 'somefile.js'}));
  93. * console.log(cache.get('somefile.js'));
  94. * //=> <File "somefile.js">
  95. * ```
  96. * @name .get
  97. * @param {String} `name`
  98. * @return {Object} Returns cache `name`, or the value of `key` if specified
  99. * @api public
  100. */
  101. get: function(name, key) {
  102. var cache = this.cache(name);
  103. if (typeof key === 'string') {
  104. return cache.get(key);
  105. }
  106. return cache;
  107. }
  108. };
  109. /**
  110. * Expose `FragmentCache`
  111. */
  112. exports = module.exports = FragmentCache;