confidence
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -12,5 +12,5 @@ // Load modules | ||
exports = module.exports = internals.Store = function () { | ||
exports = module.exports = internals.Store = function (document) { | ||
this._tree = {}; | ||
this.load(document || {}); | ||
}; | ||
@@ -22,5 +22,3 @@ | ||
var err = internals.Store.validate(document); | ||
if (err) { | ||
return err; | ||
} | ||
Hoek.assert(!err, err); | ||
@@ -34,3 +32,3 @@ this._tree = Hoek.clone(document); | ||
internals.Store.prototype.get = function (key /*, [criteria,] [depth,] next */) { | ||
internals.Store.prototype.get = function (key /*, [criteria,] [depth] */) { | ||
@@ -40,23 +38,30 @@ var self = this; | ||
var criteria = (typeof arguments[1] === 'object' ? arguments[1] : {}); | ||
var depth = (arguments.length === 4 ? arguments[2] : (arguments.length === 3 && typeof arguments[1] === 'number' ? arguments[1] : undefined)); | ||
var next = (arguments[arguments.length - 1]); | ||
var depth = (arguments.length === 3 ? arguments[2] : (arguments.length === 2 && typeof arguments[1] === 'number' ? arguments[1] : undefined)); | ||
internals.parseKey(key, /\/(\w+)/g, function (err, path) { | ||
var path = []; | ||
if (key !== '/') { | ||
var invalid = key.replace(/\/(\w+)/g, function ($0, $1) { | ||
if (err) { | ||
return next(err); | ||
path.push($1); | ||
return ''; | ||
}); | ||
if (invalid) { | ||
return null; | ||
} | ||
} | ||
var node = internals.filter(self._tree, criteria); | ||
for (var i = 0, il = path.length; i < il && node; ++i) { | ||
if (typeof node !== 'object') { | ||
node = null; | ||
break; | ||
} | ||
var node = internals.filter(self._tree, criteria); | ||
for (var i = 0, il = path.length; i < il && node; ++i) { | ||
if (typeof node !== 'object' || | ||
node instanceof Array) { | ||
node = internals.filter(node[path[i]], criteria); | ||
node = null; | ||
break; | ||
} | ||
return next(null, internals.walk(node, criteria, depth)); | ||
}); | ||
node = internals.filter(node[path[i]], criteria); | ||
} | ||
return internals.walk(node, criteria, depth); | ||
}; | ||
@@ -88,3 +93,5 @@ | ||
if (typeof node !== 'object') { | ||
if (typeof node !== 'object' || | ||
node instanceof Array) { | ||
return null; | ||
@@ -95,4 +102,3 @@ } | ||
if (node instanceof Array || | ||
node instanceof Error || | ||
if (node instanceof Error || | ||
node instanceof Date || | ||
@@ -104,12 +110,6 @@ node instanceof RegExp) { | ||
// Empty object | ||
var keys = Object.keys(node); | ||
if (keys.length === 0) { | ||
return error('Node cannot be empty'); | ||
} | ||
// Invalid keys | ||
var found = {}; | ||
var keys = Object.keys(node); | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
@@ -187,6 +187,2 @@ var key = keys[i]; | ||
found.key = true; | ||
if (!key.match(/^\w+$/)) { | ||
return error('Invalid key string ' + key); | ||
} | ||
var value = node[key]; | ||
@@ -228,23 +224,2 @@ var err = internals.Store.validate(value, path + '/' + key); | ||
// Parse key into valid segments | ||
internals.parseKey = function (key, exp, next) { | ||
var path = []; | ||
if (key !== '/') { | ||
var invalid = key.replace(exp, function ($0, $1) { | ||
path.push($1); | ||
return ''; | ||
}); | ||
if (invalid) { | ||
return next(Boom.badRequest('Bad key segment: ' + invalid)); | ||
} | ||
} | ||
return next(null, path); // Override undefined | ||
}; | ||
// Return node or value if no filter, otherwise apply filters until node or value | ||
@@ -255,6 +230,6 @@ | ||
if (!node || | ||
typeof node !== 'object' || // Value | ||
!node.$filter) { // Fork | ||
typeof node !== 'object' || node instanceof Array || // Value | ||
!node.$filter) { // Fork | ||
return node || null; // Override undefined | ||
return node || null; // Override undefined | ||
} | ||
@@ -303,3 +278,4 @@ | ||
if (!node || | ||
typeof node !== 'object') { | ||
typeof node !== 'object' || | ||
node instanceof Array) { | ||
@@ -306,0 +282,0 @@ return node || null; // Override undefined |
{ | ||
"name": "confidence", | ||
"description": "Configuration API", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/spumko/confidence", |
@@ -101,3 +101,3 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
The configuration document starts with a simple object. key names can only contain alphanumeric characters and '_' with the '$' prefix reserved | ||
for special directives. Values can contain any non-object value (e.g. strings, numbers, booleans). Values cannot be | ||
for special directives. Values can contain any non-object value (e.g. strings, numbers, booleans) as well as arrays. Values cannot be | ||
`null` or `undefined`. | ||
@@ -201,6 +201,9 @@ | ||
### new Store() | ||
### new Store([document]) | ||
Creates an empty configuration storage container. | ||
Creates an empty configuration storage container where: | ||
- `document` - an optional object containing a **confidence** configuration object generated from a parsed JSON document. | ||
If the document is invalid, will throw an error. Defaults to `{}`. | ||
```javascript | ||
@@ -217,2 +220,3 @@ var Confidence = require('confidence'); | ||
- `document` - an object containing a **confidence** configuration object generated from a parsed JSON document. | ||
If the document is invlaid, will throw an error. | ||
@@ -234,3 +238,3 @@ ```javascript | ||
### store.get(key, [criteria,] [depth,] next) | ||
### store.get(key, [criteria,] [depth]) | ||
@@ -242,13 +246,8 @@ Retrieves a value from the configuration document after applying the provided criteria where: | ||
- `depth` - optional number used to determine how deep the resulting tree is. Defaults to full depth. | ||
- `next` - required callback with the signature `function(err, value)` where: | ||
- `err` - error value if the request failed. | ||
- `value` - the configuration value found at the requested key. If key not found, returns `null` without an `err`. | ||
Note that the `next()` callback is called on the same tick. | ||
Returns the value found after applying the criteria. If the key is invalid or not found, returns null. | ||
```javascript | ||
store.get('/c', { size: 'big' }, function(err, value) { | ||
// Check err, use value | ||
}); | ||
var value = store.get('/c', { size: 'big' }); | ||
``` | ||
@@ -51,2 +51,4 @@ // Load modules | ||
}, | ||
key4: [12, 13, 14], | ||
key5: {}, | ||
ab: { | ||
@@ -72,8 +74,5 @@ // Range | ||
store.get(key, criteria, depth, function (err, value) { | ||
expect(err).to.not.exist; | ||
expect(value).to.deep.equal(result); | ||
done(); | ||
}); | ||
var value = store.get(key, criteria, depth); | ||
expect(value).to.deep.equal(result); | ||
done(); | ||
}); | ||
@@ -88,6 +87,8 @@ }; | ||
get('/key2/deeper', null); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 123 }, ab: 6 }); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 123, sub2: 6 }, ab: 6 }, { xfactor: 'yes' }); | ||
get('/', { key1: 'abc', key2: 2, key3: {}, ab: 6 }, null, 1); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 123 }, ab: 6 }, null, 2); | ||
get('/key5', {}); | ||
get('/key5', {}, null, 5); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 123 }, key4: [12, 13, 14], key5: {}, ab: 6 }); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 123, sub2: 6 }, key4: [12, 13, 14], key5: {}, ab: 6 }, { xfactor: 'yes' }); | ||
get('/', { key1: 'abc', key2: 2, key3: {}, key4: [12, 13, 14], key5: {}, ab: 6 }, null, 1); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 123 }, key4: [12, 13, 14], key5: {}, ab: 6 }, null, 2); | ||
get('/ab', 4, { random: { 1: 9 } }); | ||
@@ -98,38 +99,29 @@ get('/ab', 4, { random: { 1: 10 } }); | ||
get('/ab', 6, { random: { 1: 29 } }); | ||
it('fails on invalid key', function (done) { | ||
store.get('key', function (err, value) { | ||
expect(err).to.exist; | ||
expect(err.message).to.equal('Bad key segment: key'); | ||
done(); | ||
}); | ||
var value = store.get('key'); | ||
expect(value).to.equal(null); | ||
done(); | ||
}); | ||
it('accepts 2 arguments', function (done) { | ||
store.get('/key1', function (err, value) { | ||
expect(value).to.equal('abc'); | ||
done(); | ||
}); | ||
it('accepts 1 arguments', function (done) { | ||
var value = store.get('/key1'); | ||
expect(value).to.equal('abc'); | ||
done(); | ||
}); | ||
it('accepts 3 arguments with criteria', function (done) { | ||
store.get('/key1', {}, function (err, value) { | ||
expect(value).to.equal('abc'); | ||
done(); | ||
}); | ||
it('accepts 2 arguments with criteria', function (done) { | ||
var value = store.get('/key1', {}); | ||
expect(value).to.equal('abc'); | ||
done(); | ||
}); | ||
it('accepts 3 arguments with depth', function (done) { | ||
store.get('/', 2, function (err, value) { | ||
expect(value).to.deep.equal({ key1: 'abc', key2: 2, key3: { sub1: 123 }, ab: 6 }); | ||
done(); | ||
}); | ||
it('accepts 2 arguments with depth', function (done) { | ||
var value = store.get('/', 2); | ||
expect(value).to.deep.equal({ key1: 'abc', key2: 2, key3: { sub1: 123 }, key4: [12, 13, 14], key5: {}, ab: 6 }); | ||
done(); | ||
}); | ||
@@ -143,5 +135,7 @@ }); | ||
var store = new Confidence.Store(); | ||
var err = store.load(null); | ||
expect(err.message).to.equal('Node cannot be null or undefined'); | ||
expect(err.path).to.equal('/'); | ||
expect(function () { | ||
var err = store.load(null); | ||
}).to.throw('Node cannot be null or undefined'); | ||
done(); | ||
@@ -161,5 +155,5 @@ }); | ||
it('fails on array node', function (done) { | ||
it('fails on Error node', function (done) { | ||
var err = Confidence.Store.validate({ key: [] }); | ||
var err = Confidence.Store.validate({ key: new Error() }); | ||
expect(err.message).to.equal('Invalid node object type'); | ||
@@ -170,10 +164,2 @@ expect(err.path).to.equal('/key'); | ||
it('fails on empty object node', function (done) { | ||
var err = Confidence.Store.validate({ key: {} }); | ||
expect(err.message).to.equal('Node cannot be empty'); | ||
expect(err.path).to.equal('/key'); | ||
done(); | ||
}); | ||
it('fails on empty filter', function (done) { | ||
@@ -219,10 +205,2 @@ | ||
it('fails on invalid child key', function (done) { | ||
var err = Confidence.Store.validate({ key: { 'sub key': 'abc' } }); | ||
expect(err.message).to.equal('Invalid key string sub key'); | ||
expect(err.path).to.equal('/key'); | ||
done(); | ||
}); | ||
it('fails on invalid child node', function (done) { | ||
@@ -229,0 +207,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71916
607
249