Projekt

Obecné

Profil

Stáhnout (3 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
/*!
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
8
'use strict';
9
10
var MapCache = require('map-cache');
11
12
/**
13
 * Create a new `FragmentCache` with an optional object to use for `caches`.
14
 *
15
 * ```js
16
 * var fragment = new FragmentCache();
17
 * ```
18
 * @name FragmentCache
19
 * @param {String} `cacheName`
20
 * @return {Object} Returns the [map-cache][] instance.
21
 * @api public
22
 */
23
24
function FragmentCache(caches) {
25
  this.caches = caches || {};
26
}
27
28
/**
29
 * Prototype
30
 */
31
32
FragmentCache.prototype = {
33
34
  /**
35
   * Get cache `name` from the `fragment.caches` object. Creates a new
36
   * `MapCache` if it doesn't already exist.
37
   *
38
   * ```js
39
   * var cache = fragment.cache('files');
40
   * console.log(fragment.caches.hasOwnProperty('files'));
41
   * //=> true
42
   * ```
43
   * @name .cache
44
   * @param {String} `cacheName`
45
   * @return {Object} Returns the [map-cache][] instance.
46
   * @api public
47
   */
48
49
  cache: function(cacheName) {
50
    return this.caches[cacheName] || (this.caches[cacheName] = new MapCache());
51
  },
52
53
  /**
54
   * Set a value for property `key` on cache `name`
55
   *
56
   * ```js
57
   * fragment.set('files', 'somefile.js', new File({path: 'somefile.js'}));
58
   * ```
59
   * @name .set
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
64
   * @api public
65
   */
66
67
  set: function(cacheName, key, val) {
68
    var cache = this.cache(cacheName);
69
    cache.set(key, val);
70
    return cache;
71
  },
72
73
  /**
74
   * Returns true if a non-undefined value is set for `key` on fragment cache `name`.
75
   *
76
   * ```js
77
   * var cache = fragment.cache('files');
78
   * cache.set('somefile.js');
79
   *
80
   * console.log(cache.has('somefile.js'));
81
   * //=> true
82
   *
83
   * console.log(cache.has('some-other-file.js'));
84
   * //=> false
85
   * ```
86
   * @name .has
87
   * @param {String} `name` Cache name
88
   * @param {String} `key` Optionally specify a property to check for on cache `name`
89
   * @return {Boolean}
90
   * @api public
91
   */
92
93
  has: function(cacheName, key) {
94
    return typeof this.get(cacheName, key) !== 'undefined';
95
  },
96
97
  /**
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.
101
   *
102
   * ```js
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">
108
   * ```
109
   * @name .get
110
   * @param {String} `name`
111
   * @return {Object} Returns cache `name`, or the value of `key` if specified
112
   * @api public
113
   */
114
115
  get: function(name, key) {
116
    var cache = this.cache(name);
117
    if (typeof key === 'string') {
118
      return cache.get(key);
119
    }
120
    return cache;
121
  }
122
};
123
124
/**
125
 * Expose `FragmentCache`
126
 */
127
128
exports = module.exports = FragmentCache;