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');
14 * Create a `Cache` constructor that when instantiated will
15 * store values on the given `prop`.
18 * var Cache = require('cache-base').namespace('data');
19 * var cache = new Cache();
21 * cache.set('foo', 'bar');
22 * //=> {data: {foo: 'bar'}}
24 * @param {String} `prop` The property name to use for storing values.
25 * @return {Function} Returns a custom `Cache` constructor
29 function namespace(prop) {
32 * Create a new `Cache`. Internally the `Cache` constructor is created using
33 * the `namespace` function, with `cache` defined as the storage object.
36 * var app = new Cache();
38 * @param {Object} `cache` Optionally pass an object to initialize with.
43 function Cache(cache) {
56 Emitter(Cache.prototype);
59 * Assign `value` to `key`. Also emits `set` with
63 * app.on('set', function(key, val) {
64 * // do something when `set` is emitted
67 * app.set(key, value);
69 * // also takes an object or array
70 * app.set({name: 'Halle'});
71 * app.set([{foo: 'bar'}, {baz: 'quux'}]);
73 * //=> {name: 'Halle', foo: 'bar', baz: 'quux'}
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.
84 Cache.prototype.set = function(key, val) {
85 if (Array.isArray(key) && arguments.length === 2) {
88 if (isObject(key) || Array.isArray(key)) {
89 this.visit('set', key);
91 set(prop ? this[prop] : this, key, val);
92 this.emit('set', key, val);
98 * Union `array` to `key`. Also emits `set` with
102 * app.union('a.b', ['foo']);
103 * app.union('a.b', ['bar']);
104 * console.log(app.get('a'));
105 * //=> {b: ['foo', 'bar']}
108 * @param {String} `key`
109 * @param {any} `value`
110 * @return {Object} Returns the instance for chaining.
114 Cache.prototype.union = function(key, val) {
115 if (Array.isArray(key) && arguments.length === 2) {
118 var ctx = prop ? this[prop] : this;
119 union(ctx, key, arrayify(val));
120 this.emit('union', val);
125 * Return the value of `key`. Dot notation may be used
126 * to get [nested property values][get-value].
129 * app.set('a.b.c', 'd');
133 * app.get(['a', 'b']);
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`
144 Cache.prototype.get = function(key) {
145 key = toPath(arguments);
147 var ctx = prop ? this[prop] : this;
148 var val = get(ctx, key);
150 this.emit('get', key, val);
155 * Return true if app has a stored value for `key`,
156 * false only if value is `undefined`.
159 * app.set('foo', 'bar');
165 * @emits `has` with `key` and true or false as arguments.
166 * @param {String} `key`
171 Cache.prototype.has = function(key) {
172 key = toPath(arguments);
174 var ctx = prop ? this[prop] : this;
175 var val = get(ctx, key);
177 var has = typeof val !== 'undefined';
178 this.emit('has', key, has);
183 * Delete one or more properties from the instance.
186 * app.del(); // delete all
190 * app.del(['foo', 'bar']);
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.
199 Cache.prototype.del = function(key) {
200 if (Array.isArray(key)) {
201 this.visit('del', key);
203 del(prop ? this[prop] : this, key);
204 this.emit('del', key);
210 * Reset the entire cache to an empty object.
218 Cache.prototype.clear = function() {
225 * Visit `method` over the properties in the given object, or map
226 * visit over the object-elements in an array.
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.
235 Cache.prototype.visit = function(method, val) {
236 visit(this, method, val);
244 * Cast val to an array
247 function arrayify(val) {
248 return val ? (Array.isArray(val) ? val : [val]) : [];
255 module.exports = namespace();
258 * Expose `Cache.namespace`
261 module.exports.namespace = namespace;