| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var isObject = require('isobject'); |
| 4 | var Emitter = require('component-emitter'); |
| 5 | var visit = require('collection-visit'); |
| 6 | var toPath = require('to-object-path'); |
| 7 | var union = require('union-value'); |
| 8 | var del = require('unset-value'); |
| 9 | var get = require('get-value'); |
| 10 | var has = require('has-value'); |
| 11 | var set = require('set-value'); |
| 12 | |
| 13 | /** |
| 14 | * Create a `Cache` constructor that when instantiated will |
| 15 | * store values on the given `prop`. |
| 16 | * |
| 17 | * ```js |
| 18 | * var Cache = require('cache-base').namespace('data'); |
| 19 | * var cache = new Cache(); |
| 20 | * |
| 21 | * cache.set('foo', 'bar'); |
| 22 | * //=> {data: {foo: 'bar'}} |
| 23 | * ``` |
| 24 | * @param {String} `prop` The property name to use for storing values. |
| 25 | * @return {Function} Returns a custom `Cache` constructor |
| 26 | * @api public |
| 27 | */ |
| 28 | |
| 29 | function namespace(prop) { |
| 30 | |
| 31 | /** |
| 32 | * Create a new `Cache`. Internally the `Cache` constructor is created using |
| 33 | * the `namespace` function, with `cache` defined as the storage object. |
| 34 | * |
| 35 | * ```js |
| 36 | * var app = new Cache(); |
| 37 | * ``` |
| 38 | * @param {Object} `cache` Optionally pass an object to initialize with. |
| 39 | * @constructor |
| 40 | * @api public |
| 41 | */ |
| 42 | |
| 43 | function Cache(cache) { |
| 44 | if (prop) { |
| 45 | this[prop] = {}; |
| 46 | } |
| 47 | if (cache) { |
| 48 | this.set(cache); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Inherit Emitter |
| 54 | */ |
| 55 | |
| 56 | Emitter(Cache.prototype); |
| 57 | |
| 58 | /** |
| 59 | * Assign `value` to `key`. Also emits `set` with |
| 60 | * the key and value. |
| 61 | * |
| 62 | * ```js |
| 63 | * app.on('set', function(key, val) { |
| 64 | * // do something when `set` is emitted |
| 65 | * }); |
| 66 | * |
| 67 | * app.set(key, value); |
| 68 | * |
| 69 | * // also takes an object or array |
| 70 | * app.set({name: 'Halle'}); |
| 71 | * app.set([{foo: 'bar'}, {baz: 'quux'}]); |
| 72 | * console.log(app); |
| 73 | * //=> {name: 'Halle', foo: 'bar', baz: 'quux'} |
| 74 | * ``` |
| 75 | * |
| 76 | * @name .set |
| 77 | * @emits `set` with `key` and `value` as arguments. |
| 78 | * @param {String} `key` |
| 79 | * @param {any} `value` |
| 80 | * @return {Object} Returns the instance for chaining. |
| 81 | * @api public |
| 82 | */ |
| 83 | |
| 84 | Cache.prototype.set = function(key, val) { |
| 85 | if (Array.isArray(key) && arguments.length === 2) { |
| 86 | key = toPath(key); |
| 87 | } |
| 88 | if (isObject(key) || Array.isArray(key)) { |
| 89 | this.visit('set', key); |
| 90 | } else { |
| 91 | set(prop ? this[prop] : this, key, val); |
| 92 | this.emit('set', key, val); |
| 93 | } |
| 94 | return this; |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Union `array` to `key`. Also emits `set` with |
| 99 | * the key and value. |
| 100 | * |
| 101 | * ```js |
| 102 | * app.union('a.b', ['foo']); |
| 103 | * app.union('a.b', ['bar']); |
| 104 | * console.log(app.get('a')); |
| 105 | * //=> {b: ['foo', 'bar']} |
| 106 | * ``` |
| 107 | * @name .union |
| 108 | * @param {String} `key` |
| 109 | * @param {any} `value` |
| 110 | * @return {Object} Returns the instance for chaining. |
| 111 | * @api public |
| 112 | */ |
| 113 | |
| 114 | Cache.prototype.union = function(key, val) { |
| 115 | if (Array.isArray(key) && arguments.length === 2) { |
| 116 | key = toPath(key); |
| 117 | } |
| 118 | var ctx = prop ? this[prop] : this; |
| 119 | union(ctx, key, arrayify(val)); |
| 120 | this.emit('union', val); |
| 121 | return this; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * Return the value of `key`. Dot notation may be used |
| 126 | * to get [nested property values][get-value]. |
| 127 | * |
| 128 | * ```js |
| 129 | * app.set('a.b.c', 'd'); |
| 130 | * app.get('a.b'); |
| 131 | * //=> {c: 'd'} |
| 132 | * |
| 133 | * app.get(['a', 'b']); |
| 134 | * //=> {c: 'd'} |
| 135 | * ``` |
| 136 | * |
| 137 | * @name .get |
| 138 | * @emits `get` with `key` and `value` as arguments. |
| 139 | * @param {String} `key` The name of the property to get. Dot-notation may be used. |
| 140 | * @return {any} Returns the value of `key` |
| 141 | * @api public |
| 142 | */ |
| 143 | |
| 144 | Cache.prototype.get = function(key) { |
| 145 | key = toPath(arguments); |
| 146 | |
| 147 | var ctx = prop ? this[prop] : this; |
| 148 | var val = get(ctx, key); |
| 149 | |
| 150 | this.emit('get', key, val); |
| 151 | return val; |
| 152 | }; |
| 153 | |
| 154 | /** |
| 155 | * Return true if app has a stored value for `key`, |
| 156 | * false only if value is `undefined`. |
| 157 | * |
| 158 | * ```js |
| 159 | * app.set('foo', 'bar'); |
| 160 | * app.has('foo'); |
| 161 | * //=> true |
| 162 | * ``` |
| 163 | * |
| 164 | * @name .has |
| 165 | * @emits `has` with `key` and true or false as arguments. |
| 166 | * @param {String} `key` |
| 167 | * @return {Boolean} |
| 168 | * @api public |
| 169 | */ |
| 170 | |
| 171 | Cache.prototype.has = function(key) { |
| 172 | key = toPath(arguments); |
| 173 | |
| 174 | var ctx = prop ? this[prop] : this; |
| 175 | var val = get(ctx, key); |
| 176 | |
| 177 | var has = typeof val !== 'undefined'; |
| 178 | this.emit('has', key, has); |
| 179 | return has; |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Delete one or more properties from the instance. |
| 184 | * |
| 185 | * ```js |
| 186 | * app.del(); // delete all |
| 187 | * // or |
| 188 | * app.del('foo'); |
| 189 | * // or |
| 190 | * app.del(['foo', 'bar']); |
| 191 | * ``` |
| 192 | * @name .del |
| 193 | * @emits `del` with the `key` as the only argument. |
| 194 | * @param {String|Array} `key` Property name or array of property names. |
| 195 | * @return {Object} Returns the instance for chaining. |
| 196 | * @api public |
| 197 | */ |
| 198 | |
| 199 | Cache.prototype.del = function(key) { |
| 200 | if (Array.isArray(key)) { |
| 201 | this.visit('del', key); |
| 202 | } else { |
| 203 | del(prop ? this[prop] : this, key); |
| 204 | this.emit('del', key); |
| 205 | } |
| 206 | return this; |
| 207 | }; |
| 208 | |
| 209 | /** |
| 210 | * Reset the entire cache to an empty object. |
| 211 | * |
| 212 | * ```js |
| 213 | * app.clear(); |
| 214 | * ``` |
| 215 | * @api public |
| 216 | */ |
| 217 | |
| 218 | Cache.prototype.clear = function() { |
| 219 | if (prop) { |
| 220 | this[prop] = {}; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | /** |
| 225 | * Visit `method` over the properties in the given object, or map |
| 226 | * visit over the object-elements in an array. |
| 227 | * |
| 228 | * @name .visit |
| 229 | * @param {String} `method` The name of the `base` method to call. |
| 230 | * @param {Object|Array} `val` The object or array to iterate over. |
| 231 | * @return {Object} Returns the instance for chaining. |
| 232 | * @api public |
| 233 | */ |
| 234 | |
| 235 | Cache.prototype.visit = function(method, val) { |
| 236 | visit(this, method, val); |
| 237 | return this; |
| 238 | }; |
| 239 | |
| 240 | return Cache; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Cast val to an array |
| 245 | */ |
| 246 | |
| 247 | function arrayify(val) { |
| 248 | return val ? (Array.isArray(val) ? val : [val]) : []; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Expose `Cache` |
| 253 | */ |
| 254 | |
| 255 | module.exports = namespace(); |
| 256 | |
| 257 | /** |
| 258 | * Expose `Cache.namespace` |
| 259 | */ |
| 260 | |
| 261 | module.exports.namespace = namespace; |