Comparing version 1.3.0 to 2.0.0
/** | ||
* Module imports | ||
*/ | ||
var util = require('util'), | ||
Hash = require('./hash'), | ||
MemoryStore = require('./memorystore'); | ||
var util = require('util'), | ||
EventEmitter = require('events').EventEmitter, | ||
Hash = require('./hash'), | ||
MemoryStore = require('./memorystore'); | ||
@@ -14,32 +15,2 @@ /** | ||
/** | ||
* Export a factory | ||
* | ||
* @param {Object} store Store instance | ||
* @param {Object} options Options hash | ||
*/ | ||
module.exports = function(key, store, options) { | ||
return new Cachetree(key, store, options); | ||
}; | ||
/** | ||
* Export the MemoryStore | ||
*/ | ||
module.exports.MemoryStore = MemoryStore; | ||
/** | ||
* Check if an object conforms to the store interface | ||
* | ||
* @param {Object} store Object to validate | ||
* @return {Boolean} true if the object is a store | ||
*/ | ||
var isStore = module.exports.isStore = function(store) { | ||
if (store) { | ||
return ['get', 'set', 'keys', 'exists', 'del', 'flush', 'fields'].every(function(name) { | ||
return typeof store[name] === 'function'; | ||
}); | ||
} | ||
return false; | ||
}; | ||
/** | ||
* Constructor | ||
@@ -53,3 +24,4 @@ * | ||
function Cachetree() { | ||
var args = slice.call(arguments), | ||
var self = this, | ||
args = slice.call(arguments), | ||
store = null, | ||
@@ -64,5 +36,9 @@ key = 'cache', | ||
if (isStore(arg)) { | ||
if (i === 0) store = arg; | ||
if (i === 0) { | ||
store = arg; | ||
} | ||
} else if ((type === 'string' && arg) || type === 'number') { | ||
if (i <= 1) key = arg; | ||
if (i <= 1) { | ||
key = arg; | ||
} | ||
} else if (arg === Object(arg)) { | ||
@@ -79,3 +55,5 @@ if (i <= 2) { | ||
} | ||
if (!store) store = new MemoryStore(); | ||
if (!store) { | ||
store = new MemoryStore(); | ||
} | ||
Object.defineProperty(this, 'store', { | ||
@@ -92,4 +70,38 @@ value: store, | ||
Hash.call(this, key); | ||
if (store instanceof EventEmitter) { | ||
store.on('error', function(error) { | ||
self.emit('error', error); | ||
}); | ||
} | ||
} | ||
util.inherits(Cachetree, Hash); | ||
/** | ||
* Export a factory | ||
* | ||
* @param {Object} store Store instance | ||
* @param {Object} options Options hash | ||
*/ | ||
module.exports = function(key, store, options) { | ||
return new Cachetree(key, store, options); | ||
}; | ||
/** | ||
* Export the MemoryStore | ||
*/ | ||
module.exports.MemoryStore = MemoryStore; | ||
/** | ||
* Check if an object conforms to the store interface | ||
* | ||
* @param {Object} store Object to validate | ||
* @return {Boolean} true if the object is a store | ||
*/ | ||
var isStore = module.exports.isStore = function(store) { | ||
if (store) { | ||
return ['get', 'set', 'keys', 'exists', 'del', 'flush', 'fields'].every(function(name) { | ||
return typeof store[name] === 'function'; | ||
}); | ||
} | ||
return false; | ||
}; |
/** | ||
* Array.prototype.slice reference | ||
* Module imports | ||
*/ | ||
var slice = Array.prototype.slice; | ||
var util = require('util'), | ||
EventEmitter = require('events').EventEmitter; | ||
/** | ||
* Export hash | ||
* Array.prototype.slice reference | ||
*/ | ||
module.exports = Hash; | ||
var slice = Array.prototype.slice; | ||
@@ -25,5 +26,11 @@ /** | ||
} | ||
if (Array.isArray(key)) len = key.length; | ||
if (!(!root && len === 1) && !(root && len > 1)) throw new Error('Invalid key'); | ||
if (!root) root = this; | ||
if (Array.isArray(key)) { | ||
len = key.length; | ||
} | ||
if (!(!root && len === 1) && !(root && len > 1)) { | ||
throw new Error('Invalid key'); | ||
} | ||
if (!root) { | ||
root = this; | ||
} | ||
// Define properties | ||
@@ -40,5 +47,12 @@ Object.defineProperties(this, { | ||
}); | ||
EventEmitter.call(this); | ||
} | ||
util.inherits(Hash, EventEmitter); | ||
/** | ||
* Export hash | ||
*/ | ||
module.exports = Hash; | ||
/** | ||
* Execute a store function that takes variable arguments | ||
@@ -54,5 +68,9 @@ * | ||
cb; | ||
if (len > 0 && typeof args[len - 1] === 'function') cb = args.pop(); | ||
if (len > 0 && typeof args[len - 1] === 'function') { | ||
cb = args.pop(); | ||
} | ||
// Unwrap the first argument if its an array | ||
if (args.length === 1 && Array.isArray(args[0])) args = args[0]; | ||
if (args.length === 1 && Array.isArray(args[0])) { | ||
args = args[0]; | ||
} | ||
this.root.store[name](this.key, args, cb); | ||
@@ -121,3 +139,5 @@ return this; | ||
this.root.store.keys(this.childKey('*'), function(err, keys) { | ||
if (!Array.isArray(keys)) keys = []; | ||
if (!Array.isArray(keys)) { | ||
keys = []; | ||
} | ||
keys.unshift(self.key); | ||
@@ -177,3 +197,5 @@ self.root.store.flush(keys, cb); | ||
// Extract child names | ||
if (!def) def = {}; | ||
if (!def) { | ||
def = {}; | ||
} | ||
children = Object.keys(def).filter(function(value) { | ||
@@ -196,3 +218,5 @@ return value !== '__key__' && value !== '__validate__'; | ||
} else { | ||
if (def.__validate__ && typeof def.__validate__ === 'function') validateFn = true; | ||
if (def.__validate__ && typeof def.__validate__ === 'function') { | ||
validateFn = true; | ||
} | ||
prefixType = typeof def.__prefix__; | ||
@@ -206,4 +230,3 @@ if (prefixType === 'string' && def.__prefix__) { | ||
var isValid = false, | ||
type = typeof value, | ||
hash; | ||
type = typeof value; | ||
if (type === 'string' || type === 'number') { | ||
@@ -222,3 +245,5 @@ if (def.__validate__) { | ||
} | ||
if (isValid !== true) throw new Error('Invalid key'); | ||
if (isValid !== true) { | ||
throw new Error('Invalid key'); | ||
} | ||
return create(prefix + value); | ||
@@ -225,0 +250,0 @@ }; |
@@ -7,7 +7,2 @@ /** | ||
/** | ||
* Export MemoryStore | ||
*/ | ||
module.exports = MemoryStore; | ||
/** | ||
* Constructor | ||
@@ -18,9 +13,16 @@ * | ||
function MemoryStore(options) { | ||
if (!(this instanceof MemoryStore)) { | ||
return new MemoryStore(options); | ||
} | ||
var type, | ||
delimiter = ':'; | ||
this._data = {}; | ||
if (!options) options = {}; | ||
if (!options) { | ||
options = {}; | ||
} | ||
// Set the delimiter property | ||
type = typeof options.delimiter; | ||
if ((type === 'string' && options.delimiter) || type === 'number') delimiter = options.delimiter; | ||
if ((type === 'string' && options.delimiter) || type === 'number') { | ||
delimiter = options.delimiter; | ||
} | ||
Object.defineProperty(this, 'delimiter', { | ||
@@ -33,2 +35,7 @@ value: delimiter, | ||
/** | ||
* Export MemoryStore | ||
*/ | ||
module.exports = MemoryStore; | ||
/** | ||
* Get the values of all the given hash fields | ||
@@ -48,3 +55,5 @@ * | ||
data; | ||
if (typeof cb !== 'function') return this; | ||
if (typeof cb !== 'function') { | ||
return this; | ||
} | ||
if (key) { | ||
@@ -58,6 +67,10 @@ // Extract the values | ||
} else { | ||
if (len === 0) args = Object.keys(data); | ||
if (len === 0) { | ||
args = Object.keys(data); | ||
} | ||
args.forEach(function(field) { | ||
obj[field] = data[field]; | ||
if (obj[field] === undefined) obj[field] = null; | ||
if (obj[field] === undefined) { | ||
obj[field] = null; | ||
} | ||
}); | ||
@@ -95,3 +108,5 @@ cb(null, obj); | ||
if (key) { | ||
if (!this._data[key]) this._data[key] = {}; | ||
if (!this._data[key]) { | ||
this._data[key] = {}; | ||
} | ||
// Unwrap the first argument if its an array | ||
@@ -128,3 +143,5 @@ if (len === 1 && Array.isArray(args[0])) { | ||
} | ||
if (typeof cb === 'function') cb(err); | ||
if (typeof cb === 'function') { | ||
cb(err); | ||
} | ||
return this; | ||
@@ -145,4 +162,8 @@ }; | ||
} | ||
if (typeof cb !== 'function') return this; | ||
if (Array.isArray(pattern)) pattern = this.cacheKey(pattern); | ||
if (typeof cb !== 'function') { | ||
return this; | ||
} | ||
if (Array.isArray(pattern)) { | ||
pattern = this.cacheKey(pattern); | ||
} | ||
var re, | ||
@@ -157,4 +178,8 @@ source, | ||
if (source) { | ||
if (source.substring(0, 1) !== '^') source = '^' + source; | ||
if (source.substr(-1) !== '$') source += '$'; | ||
if (source.substring(0, 1) !== '^') { | ||
source = '^' + source; | ||
} | ||
if (source.substr(-1) !== '$') { | ||
source += '$'; | ||
} | ||
re = new RegExp(source, 'i'); | ||
@@ -185,5 +210,9 @@ matches = Object.keys(this._data).filter(function(key) { | ||
} | ||
if (typeof cb !== 'function') return this; | ||
if (typeof cb !== 'function') { | ||
return this; | ||
} | ||
key = this.cacheKey(key); | ||
if (!key) return cb(new Error('Invalid key')); | ||
if (!key) { | ||
return cb(new Error('Invalid key')); | ||
} | ||
var exists = this._data[key] && this._data[key][field] !== undefined; | ||
@@ -217,3 +246,5 @@ cb(null, exists); | ||
if (obj && len > 0) { | ||
if (len === 1 && Array.isArray(args[0])) args = args[0]; | ||
if (len === 1 && Array.isArray(args[0])) { | ||
args = args[0]; | ||
} | ||
args.forEach(function(field) { | ||
@@ -228,3 +259,5 @@ if (field in obj) { | ||
} | ||
if (typeof cb === 'function') cb(err); | ||
if (typeof cb === 'function') { | ||
cb(err); | ||
} | ||
return this; | ||
@@ -246,4 +279,4 @@ }; | ||
if (len > 0 && typeof args[len - 1] === 'function') { | ||
cb = args.pop(), | ||
len--; | ||
cb = args.pop(); | ||
len -= 1; | ||
} | ||
@@ -264,3 +297,5 @@ if (len === 1 && Array.isArray(args[0])) { | ||
} | ||
if (typeof cb === 'function') cb(err); | ||
if (typeof cb === 'function') { | ||
cb(err); | ||
} | ||
return this; | ||
@@ -281,3 +316,5 @@ }; | ||
type = typeof key; | ||
if ((type === 'string' && key) || (type === 'number')) return key; | ||
if ((type === 'string' && key) || (type === 'number')) { | ||
return key; | ||
} | ||
}; | ||
@@ -296,5 +333,9 @@ | ||
} | ||
if (typeof cb !== 'function') return this; | ||
if (typeof cb !== 'function') { | ||
return this; | ||
} | ||
key = this.cacheKey(key); | ||
if (!key) return cb(new Error('Invalid key')); | ||
if (!key) { | ||
return cb(new Error('Invalid key')); | ||
} | ||
if (this._data[key]) { | ||
@@ -301,0 +342,0 @@ cb(null, Object.keys(this._data[key])); |
{ | ||
"name": "cachetree", | ||
"description": "A scoped, fluent API for easily interacting with hierarchical, key-value data", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"author": "David Wood <bitprobe@gmail.com>", | ||
@@ -12,12 +12,13 @@ "repository": { | ||
"devDependencies": { | ||
"mocha": "*" | ||
"istanbul": "^0.3.6", | ||
"mocha": "^2.1.0" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
"test": "NODE_ENV=test istanbul test _mocha -- --check-leaks --reporter spec" | ||
}, | ||
"main": "./index.js", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
"node": ">= 0.10.0" | ||
} | ||
} | ||
/*global describe: true, it:true, beforeEach: true, afterEach: true, before: true, after: true */ | ||
var assert = require('assert'), | ||
var assert = require('assert'), | ||
EventEmitter = require('events').EventEmitter, | ||
cachetree = require('../'), | ||
noop = function() {}; | ||
noop = function() {}; | ||
@@ -27,3 +28,10 @@ /** | ||
describe('childKey(key)', function() { | ||
it('should be an EventEmitter', function(done) { | ||
var cache = cachetree(); | ||
assert.strictEqual(cache instanceof EventEmitter, true); | ||
cache.on('test', done); | ||
cache.emit('test'); | ||
}); | ||
describe('.childKey(key)', function() { | ||
@@ -30,0 +38,0 @@ var cache; |
/*global describe: true, it:true, beforeEach: true, afterEach: true, before: true, after: true */ | ||
var assert = require('assert'), | ||
cachetree = require('../'), | ||
var assert = require('assert'), | ||
cachetree = require('../'), | ||
MemoryStore = cachetree.MemoryStore; | ||
@@ -13,5 +13,14 @@ | ||
it('should accept a custom delimiter', function() { | ||
var inst = new MemoryStore({ delimiter: '-' }); | ||
assert.equal(inst.delimiter, '-'); | ||
describe('new MemoryStore(options)', function() { | ||
it('should not require new to be constructed', function() { | ||
var inst = MemoryStore({ client: store.client }); | ||
assert.strictEqual(inst instanceof MemoryStore, true); | ||
}); | ||
it('should accept a custom delimiter', function() { | ||
var inst = new MemoryStore({ delimiter: '-' }); | ||
assert.equal(inst.delimiter, '-'); | ||
}); | ||
}); | ||
@@ -18,0 +27,0 @@ |
Sorry, the diff of this file is not supported yet
143601
21
1447
2