confidence
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -29,10 +29,10 @@ // Load modules | ||
internals.Store.prototype.get = function (key, criteria) { | ||
internals.Store.prototype.get = function (key, criteria, applied) { | ||
var node = this._get(key, criteria); | ||
return internals.walk(node, criteria); | ||
var node = this._get(key, criteria, applied); | ||
return internals.walk(node, criteria, applied); | ||
}; | ||
internals.Store.prototype._get = function (key, criteria) { | ||
internals.Store.prototype._get = function (key, criteria, applied) { | ||
@@ -56,3 +56,3 @@ var self = this; | ||
var node = internals.filter(self._tree, criteria); | ||
var node = internals.filter(self._tree, criteria, applied); | ||
for (var i = 0, il = path.length; i < il && node; ++i) { | ||
@@ -64,3 +64,3 @@ if (typeof node !== 'object') { | ||
node = internals.filter(node[path[i]], criteria); | ||
node = internals.filter(node[path[i]], criteria, applied); | ||
} | ||
@@ -83,3 +83,3 @@ | ||
internals.filter = function (node, criteria) { | ||
internals.filter = function (node, criteria, applied) { | ||
@@ -94,3 +94,3 @@ if (!node || | ||
if (node.$value) { | ||
return internals.filter(node.$value, criteria); | ||
return internals.filter(node.$value, criteria, applied); | ||
} | ||
@@ -104,25 +104,56 @@ | ||
if (criterion !== undefined) { | ||
var sub = (node.$range ? internals.range(node.$range, criterion, node.$default) : node[criterion]); | ||
return internals.filter(sub, criteria); | ||
if (!node.$range) { | ||
internals.logApplied(applied, filter, node, criterion); | ||
return internals.filter(node[criterion], criteria, applied); | ||
} | ||
for (var i = 0, il = node.$range.length; i < il; ++i) { | ||
if (criterion <= node.$range[i].limit) { | ||
internals.logApplied(applied, filter, node, node.$range[i]); | ||
return internals.filter(node.$range[i].value, criteria, applied); | ||
} | ||
} | ||
// Falls-through for $default | ||
} | ||
else if (node.hasOwnProperty('$default')) { | ||
return internals.filter(node.$default, criteria); | ||
if (node.hasOwnProperty('$default')) { | ||
internals.logApplied(applied, filter, node, '$default'); | ||
return internals.filter(node.$default, criteria, applied); | ||
} | ||
else { | ||
return undefined; | ||
} | ||
internals.logApplied(applied, filter, node); | ||
return undefined; | ||
}; | ||
// Find nearest range entry | ||
internals.logApplied = function (applied, filter, node, criterion) { | ||
internals.range = function ($range, criterion, $default) { | ||
if (!applied) { | ||
return; | ||
} | ||
for (var i = 0, il = $range.length; i < il; ++i) { | ||
if (criterion <= $range[i].limit) { | ||
return $range[i].value; | ||
var record = { | ||
filter: filter | ||
}; | ||
if (criterion) { | ||
if (typeof criterion === 'object') { | ||
if (criterion.id) { | ||
record.valueId = criterion.id; | ||
} | ||
else { | ||
record.valueId = (typeof criterion.value === 'object' ? '[object]' : criterion.value.toString()); | ||
} | ||
} | ||
else { | ||
record.valueId = criterion.toString(); | ||
} | ||
} | ||
return $default; | ||
if (node && node.$id) { | ||
record.filterId = node.$id; | ||
} | ||
applied.push(record); | ||
}; | ||
@@ -133,3 +164,3 @@ | ||
internals.walk = function (node, criteria) { | ||
internals.walk = function (node, criteria, applied) { | ||
@@ -143,3 +174,3 @@ if (!node || | ||
if (node.hasOwnProperty('$value')) { | ||
return internals.walk(node.$value, criteria); | ||
return internals.walk(node.$value, criteria, applied); | ||
} | ||
@@ -152,7 +183,7 @@ | ||
var key = keys[i]; | ||
if (key === '$meta') { | ||
if (key === '$meta' || key === '$id') { | ||
continue; | ||
} | ||
var child = internals.filter(node[key], criteria); | ||
var value = internals.walk(child, criteria); | ||
var child = internals.filter(node[key], criteria, applied); | ||
var value = internals.walk(child, criteria, applied); | ||
if (value !== undefined) { | ||
@@ -270,2 +301,11 @@ parent[key] = value; | ||
} | ||
else if (key === '$id') { | ||
if (!node.$id || | ||
typeof node.$id !== 'string') { | ||
return error('Id value must be a non-empty string'); | ||
} | ||
found.id = true; | ||
} | ||
else if (key === '$value') { | ||
@@ -272,0 +312,0 @@ found.value = true; |
{ | ||
"name": "confidence", | ||
"description": "Configuration API", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/spumko/confidence", |
@@ -54,2 +54,3 @@ // Load modules | ||
$filter: 'xfactor', | ||
$id: 'x_factor', | ||
yes: '' // Value | ||
@@ -63,3 +64,7 @@ } | ||
$filter: 'random.1', | ||
$id: 'random_ab_test', | ||
$range: [ | ||
{ limit: 1, value: [1, 2] }, | ||
{ limit: 2, value: { $value: 2 } }, | ||
{ limit: 3, value: { a: 5 }, id: '3' }, | ||
{ limit: 10, value: 4 }, | ||
@@ -80,8 +85,12 @@ { limit: 20, value: 5 } | ||
var get = function (key, result, criteria) { | ||
var get = function (key, result, criteria, applied) { | ||
it('gets value for ' + key + (criteria ? ' with criteria ' + JSON.stringify(criteria) : ''), function (done) { | ||
var value = store.get(key, criteria); | ||
var resultApplied = []; | ||
var value = store.get(key, criteria, applied ? resultApplied : null); | ||
expect(value).to.deep.equal(result); | ||
if (applied) { | ||
expect(resultApplied).to.deep.equal(applied); | ||
} | ||
done(); | ||
@@ -92,3 +101,3 @@ }); | ||
get('/key1', 'abc'); | ||
get('/key2', 2); | ||
get('/key2', 2, null, [{ filter: 'env', valueId: '$default' }, { filter: 'platform', valueId: '$default' }]); | ||
get('/key2', 1, { platform: 'ios' }); | ||
@@ -101,2 +110,4 @@ get('/key2/deeper', 'value', { env: 'production' }); | ||
get('/', { key1: 'abc', key2: 2, key3: { sub1: 0, sub2: '' }, key4: [12, 13, 14], key5: {}, ab: 6 }, { xfactor: 'yes' }); | ||
get('/ab', 2, { random: { 1: 2 } }, [{ filter: 'random.1', valueId: '[object]', filterId: 'random_ab_test' }]); | ||
get('/ab', { a: 5 }, { random: { 1: 3 } }, [{ filter: 'random.1', valueId: '3', filterId: 'random_ab_test' }]); | ||
get('/ab', 4, { random: { 1: 9 } }); | ||
@@ -334,4 +345,12 @@ get('/ab', 4, { random: { 1: 10 } }); | ||
}); | ||
it('fails on invalid id', function (done) { | ||
var err = Confidence.Store.validate({ key: 5, $id: 4 }); | ||
expect(err.message).to.equal('Id value must be a non-empty string'); | ||
expect(err.path).to.equal('/'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
75914
690