123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- 'use strict';
-
- var isObject = require('isobject');
- var Emitter = require('component-emitter');
- var visit = require('collection-visit');
- var toPath = require('to-object-path');
- var union = require('union-value');
- var del = require('unset-value');
- var get = require('get-value');
- var has = require('has-value');
- var set = require('set-value');
-
-
-
- function namespace(prop) {
-
-
-
-
- function Cache(cache) {
- if (prop) {
- this[prop] = {};
- }
- if (cache) {
- this.set(cache);
- }
- }
-
-
-
-
- Emitter(Cache.prototype);
-
-
-
-
- Cache.prototype.set = function(key, val) {
- if (Array.isArray(key) && arguments.length === 2) {
- key = toPath(key);
- }
- if (isObject(key) || Array.isArray(key)) {
- this.visit('set', key);
- } else {
- set(prop ? this[prop] : this, key, val);
- this.emit('set', key, val);
- }
- return this;
- };
-
-
-
-
- Cache.prototype.union = function(key, val) {
- if (Array.isArray(key) && arguments.length === 2) {
- key = toPath(key);
- }
- var ctx = prop ? this[prop] : this;
- union(ctx, key, arrayify(val));
- this.emit('union', val);
- return this;
- };
-
-
-
-
- Cache.prototype.get = function(key) {
- key = toPath(arguments);
-
- var ctx = prop ? this[prop] : this;
- var val = get(ctx, key);
-
- this.emit('get', key, val);
- return val;
- };
-
-
-
-
- Cache.prototype.has = function(key) {
- key = toPath(arguments);
-
- var ctx = prop ? this[prop] : this;
- var val = get(ctx, key);
-
- var has = typeof val !== 'undefined';
- this.emit('has', key, has);
- return has;
- };
-
-
-
-
- Cache.prototype.del = function(key) {
- if (Array.isArray(key)) {
- this.visit('del', key);
- } else {
- del(prop ? this[prop] : this, key);
- this.emit('del', key);
- }
- return this;
- };
-
-
-
-
- Cache.prototype.clear = function() {
- if (prop) {
- this[prop] = {};
- }
- };
-
-
-
-
- Cache.prototype.visit = function(method, val) {
- visit(this, method, val);
- return this;
- };
-
- return Cache;
- }
-
-
-
- function arrayify(val) {
- return val ? (Array.isArray(val) ? val : [val]) : [];
- }
-
-
-
- module.exports = namespace();
-
-
-
- module.exports.namespace = namespace;
|