[fix] #59 root get/set should work via null/undefined as key

This commit is contained in:
Bradley Meck 2012-09-07 09:29:30 -05:00
parent ec9a13e901
commit 683f78918c
3 changed files with 56 additions and 15 deletions

View file

@ -16,9 +16,11 @@ var common = exports;
// ### function path (key) // ### function path (key)
// #### @key {string} The ':' delimited key to split // #### @key {string} The ':' delimited key to split
// Returns a fully-qualified path to a nested nconf key. // Returns a fully-qualified path to a nested nconf key.
// If given null or undefined it should return an empty path.
// '' should still be respected as a path.
// //
common.path = function (key) { common.path = function (key) {
return key.split(':'); return key == null ? [] : key.split(':');
}; };
// //

View file

@ -42,15 +42,14 @@ Memory.prototype.get = function (key) {
// //
while (path.length > 0) { while (path.length > 0) {
key = path.shift(); key = path.shift();
if (!(target && key in target)) { if (target && key in target) {
return; target = target[key];
} continue;
target = target[key];
if (path.length === 0) {
return target;
} }
return undefined;
} }
return target;
}; };
// //
@ -67,6 +66,20 @@ Memory.prototype.set = function (key, value) {
var target = this.store, var target = this.store,
path = common.path(key); path = common.path(key);
if (path.length === 0) {
//
// Root must be an object
//
if (!value || typeof value !== 'object') {
return false;
}
else {
this.reset();
this.store = value;
return true;
}
}
// //
// Update the `mtime` (modified time) of the key // Update the `mtime` (modified time) of the key
// //
@ -101,6 +114,7 @@ Memory.prototype.clear = function (key) {
} }
var target = this.store, var target = this.store,
value = target,
path = common.path(key); path = common.path(key);
// //
@ -111,17 +125,17 @@ Memory.prototype.clear = function (key) {
// //
// Scope into the object to get the appropriate nested context // Scope into the object to get the appropriate nested context
// //
while (path.length > 1) { for (var i = 0; i < path.length - 1; i++) {
key = path.shift(); key = path[i];
if (!target[key]) { value = target[key];
return; if (typeof value !== 'function' && typeof value !== 'object') {
return false;
} }
target = value;
target = target[key];
} }
// Delete the key from the nested JSON structure // Delete the key from the nested JSON structure
key = path.shift(); key = path[i];
delete target[key]; delete target[key];
return true; return true;
}; };

View file

@ -68,6 +68,31 @@ vows.describe('nconf').addBatch({
} }
} }
} }
}).addBatch({
"When using the nconf": {
"with the memory store": {
"the get() method": {
"should respond allow access to the root": function () {
assert(nconf.get(null));
assert(nconf.get(undefined));
assert(nconf.get());
}
},
"the set() method": {
"should respond allow access to the root and complain about non-objects": function () {
assert(!nconf.set(null, null));
assert(!nconf.set(null, undefined));
assert(!nconf.set(null));
assert(!nconf.set(null, ''));
assert(!nconf.set(null, 1));
var original = nconf.get();
assert(nconf.set(null, nconf.get()));
assert.notEqual(nconf.get(), original);
assert.deepEqual(nconf.get(), original)
}
}
}
}
}).addBatch({ }).addBatch({
"When using nconf": { "When using nconf": {
"with the memory store": { "with the memory store": {