Skip to content

Commit

Permalink
simple parse, #72
Browse files Browse the repository at this point in the history
  • Loading branch information
safanaj authored and Matt Hamann committed Sep 27, 2017
1 parent b8402d4 commit 6dafe9d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/nconf/common.js
Expand Up @@ -121,3 +121,42 @@ common.merge = function (objs) {
common.capitalize = function (str) {
return str && str[0].toUpperCase() + str.slice(1);
};

//
// ### function simpleParse (any)
// #### @any {string} String to parse as native data-type or return as is
// try to parse `any` as a native data-type
//
common.simpleParse = function (any) {
if (typeof any !== 'string')
return any;

if (any === 'false')
return false;

if (any === 'true')
return true;

if (any === 'null')
return null;

if (any === 'undefined')
return undefined;

if (/^[\d.]+$/.test(any)) {
if (any.indexOf('.') > -1)
try {
return parseFloat(any);
} catch (err) {
return any;
}
else
try {
return parseInt(any);
} catch (err) {
return any;
}
}

return any;
};
4 changes: 4 additions & 0 deletions lib/nconf/stores/memory.js
Expand Up @@ -23,6 +23,7 @@ var Memory = exports.Memory = function (options) {
this.readOnly = false;
this.loadFrom = options.loadFrom || null;
this.logicalSeparator = options.logicalSeparator || ':';
this.tryParse = options.tryParse || false;

if (this.loadFrom) {
this.store = common.loadFilesSync(this.loadFrom);
Expand Down Expand Up @@ -100,6 +101,9 @@ Memory.prototype.set = function (key, value) {

// Set the specified value in the nested JSON structure
key = path.shift();
if (this.tryParse) {
value = common.simpleParse.call(common, value);
}
target[key] = value;
return true;
};
Expand Down
37 changes: 36 additions & 1 deletion test/provider-test.js
Expand Up @@ -47,7 +47,42 @@ vows.describe('nconf/provider').addBatch({
"when 'env' is true": helpers.assertSystemConf({
script: path.join(fixturesDir, 'scripts', 'provider-env.js'),
env: { SOMETHING: 'foobar' }
})
}),
"when 'env' is true and 'tryParse' option is true": {
topic: function() {
var env = {
SOMETHING: 'foobar',
SOMEBOOL: 'true',
SOMENULL: 'null',
SOMEUNDEF: 'undefined',
SOMEINT: '3600',
SOMEFLOAT: '0.5',
SOMEBAD: '5.1a'
};
var oenv = {};
Object.keys(env).forEach(function (key) {
if (process.env[key]) oenv[key] = process.env[key];
process.env[key] = env[key];
});
var provider = new nconf.Provider().use('env', {tryParse: true});
Object.keys(env).forEach(function (key) {
delete process.env[key];
if (oenv[key]) process.env[key] = oenv[key];
});
return provider;
},
"should respond with parsed values": function (provider) {

assert.equal(provider.get('SOMETHING'), 'foobar');
assert.strictEqual(provider.get('SOMEBOOL'), true);
assert.notEqual(provider.get('SOMEBOOL'), 'true');
assert.strictEqual(provider.get('SOMENULL'), null);
assert.strictEqual(provider.get('SOMEUNDEF'), undefined);
assert.strictEqual(provider.get('SOMEINT'), 3600);
assert.strictEqual(provider.get('SOMEFLOAT'), .5);
assert.strictEqual(provider.get('SOMEBAD'), '5.1a');
}
}
},
"the default nconf provider": {
"when 'argv' is set to true": helpers.assertSystemConf({
Expand Down

0 comments on commit 6dafe9d

Please sign in to comment.