Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled array on .get configurations #84

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@ class Config {

key = key.split(':');
obj = this._store;
var isArray = false;

while (obj && key.length) {
if (isArray) {
var idx = Number(key[0]);
if (Number.isInteger(idx) && idx >= 0) {
if (obj.length <= idx) return undefined;

obj = obj[key.shift()];
}
}

if (obj.constructor !== Object) {
// Do not allow traversal into complex types,
// such as Buffer, Date, etc. So, this type
// of key will fail: 'foo:mystring:length'
return undefined;
}
}

obj = obj[key.shift()];
isArray = Array.isArray(obj);
}

return obj;
Expand Down
17 changes: 16 additions & 1 deletion test/confit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ test('confit', function (t) {


t.test('get', function (t) {
var basedir;
//setting process.env.env to development, should not change 'env:env'.
//This should be ignored and 'env:env' should solely depend on process.env.NODE_ENV
process.env.env = 'development';
basedir = path.join(__dirname, 'fixtures', 'config');

confit().create(function (err, config) {
confit(basedir).create(function (err, config) {
var val;

t.error(err);
Expand Down Expand Up @@ -71,6 +73,19 @@ test('confit', function (t) {
val = config.get(false);
t.equal(typeof val, 'undefined');

val = config.get('arr');
t.equal(typeof val, 'object');
t.equal(Array.isArray(val), true);

val = config.get('arr:0:name');
t.equal(val, 'object1');

val = config.get('arr_empty:0');
t.equal(typeof val, 'undefined');

val = config.get('arr_empty:0:name');
t.equal(typeof val, 'undefined');

t.end();
});
});
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
}
},
"value": false,
"arr":[{
"name": "object1"
}],
"arr_empty":[],
"imported": "import:./child.json"
}