Skip to content

Commit

Permalink
[fix] #59 root get/set should work via null/undefined as key
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Sep 7, 2012
1 parent ec9a13e commit 683f789
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/nconf/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ var common = exports;
// ### function path (key)
// #### @key {string} The ':' delimited key to split
// 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) {
return key.split(':');
return key == null ? [] : key.split(':');
};

//
Expand Down
42 changes: 28 additions & 14 deletions lib/nconf/stores/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ Memory.prototype.get = function (key) {
//
while (path.length > 0) {
key = path.shift();
if (!(target && key in target)) {
return;
}

target = target[key];
if (path.length === 0) {
return target;
if (target && key in target) {
target = target[key];
continue;
}
return undefined;
}

return target;
};

//
Expand All @@ -67,6 +66,20 @@ Memory.prototype.set = function (key, value) {
var target = this.store,
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
//
Expand Down Expand Up @@ -101,6 +114,7 @@ Memory.prototype.clear = function (key) {
}

var target = this.store,
value = target,
path = common.path(key);

//
Expand All @@ -111,17 +125,17 @@ Memory.prototype.clear = function (key) {
//
// Scope into the object to get the appropriate nested context
//
while (path.length > 1) {
key = path.shift();
if (!target[key]) {
return;
for (var i = 0; i < path.length - 1; i++) {
key = path[i];
value = target[key];
if (typeof value !== 'function' && typeof value !== 'object') {
return false;
}

target = target[key];
target = value;
}

// Delete the key from the nested JSON structure
key = path.shift();
key = path[i];
delete target[key];
return true;
};
Expand Down
25 changes: 25 additions & 0 deletions test/nconf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
"When using nconf": {
"with the memory store": {
Expand Down

0 comments on commit 683f789

Please sign in to comment.