2 * fragment-cache <https://github.com/jonschlinkert/fragment-cache>
4 * Copyright (c) 2016-2017, Jon Schlinkert.
5 * Released under the MIT License.
10 var MapCache = require('map-cache');
13 * Create a new `FragmentCache` with an optional object to use for `caches`.
16 * var fragment = new FragmentCache();
19 * @param {String} `cacheName`
20 * @return {Object} Returns the [map-cache][] instance.
24 function FragmentCache(caches) {
25 this.caches = caches || {};
32 FragmentCache.prototype = {
35 * Get cache `name` from the `fragment.caches` object. Creates a new
36 * `MapCache` if it doesn't already exist.
39 * var cache = fragment.cache('files');
40 * console.log(fragment.caches.hasOwnProperty('files'));
44 * @param {String} `cacheName`
45 * @return {Object} Returns the [map-cache][] instance.
49 cache: function(cacheName) {
50 return this.caches[cacheName] || (this.caches[cacheName] = new MapCache());
54 * Set a value for property `key` on cache `name`
57 * fragment.set('files', 'somefile.js', new File({path: 'somefile.js'}));
60 * @param {String} `name`
61 * @param {String} `key` Property name to set
62 * @param {any} `val` The value of `key`
63 * @return {Object} The cache instance for chaining
67 set: function(cacheName, key, val) {
68 var cache = this.cache(cacheName);
74 * Returns true if a non-undefined value is set for `key` on fragment cache `name`.
77 * var cache = fragment.cache('files');
78 * cache.set('somefile.js');
80 * console.log(cache.has('somefile.js'));
83 * console.log(cache.has('some-other-file.js'));
87 * @param {String} `name` Cache name
88 * @param {String} `key` Optionally specify a property to check for on cache `name`
93 has: function(cacheName, key) {
94 return typeof this.get(cacheName, key) !== 'undefined';
98 * Get `name`, or if specified, the value of `key`. Invokes the [cache]() method,
99 * so that cache `name` will be created it doesn't already exist. If `key` is not passed,
100 * the entire cache (`name`) is returned.
103 * var Vinyl = require('vinyl');
104 * var cache = fragment.cache('files');
105 * cache.set('somefile.js', new Vinyl({path: 'somefile.js'}));
106 * console.log(cache.get('somefile.js'));
107 * //=> <File "somefile.js">
110 * @param {String} `name`
111 * @return {Object} Returns cache `name`, or the value of `key` if specified
115 get: function(name, key) {
116 var cache = this.cache(name);
117 if (typeof key === 'string') {
118 return cache.get(key);
125 * Expose `FragmentCache`
128 exports = module.exports = FragmentCache;