settings-lib
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -0,1 +1,5 @@ | ||
# v0.1.2 / 2015-05-28 | ||
* Adjusting command line and environment maps to attempt to coerce config override values based on the type found in the default settings | ||
# v0.1.1 / 2015-03-19 | ||
@@ -2,0 +6,0 @@ |
@@ -13,5 +13,59 @@ var | ||
readCommandLineMap : {} | ||
}; | ||
}, | ||
defaultTypesMap = {}; | ||
function buildDefaultTypesMap (source, keyParts) { | ||
'use strict'; | ||
keyParts = keyParts || []; | ||
Object.keys(source).forEach(function (key) { | ||
keyParts.push(key); | ||
if (Object.prototype.toString.call(source[key]) === '[object Array]') { | ||
defaultTypesMap[keyParts.join('.')] = function (value) { | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
// remove front and back brackets, then split on commas | ||
return value.slice(1, -1).split(','); | ||
}; | ||
keyParts.pop(); | ||
return; | ||
} | ||
if (Object.prototype.toString.call(source[key]) === '[object Date]') { | ||
defaultTypesMap[keyParts.join('.')] = function (value) { | ||
return new Date(value); | ||
}; | ||
keyParts.pop(); | ||
return; | ||
} | ||
if (typeof source[key] === 'object' && source[key] !== null) { | ||
return buildDefaultTypesMap(source[key], keyParts); | ||
} | ||
defaultTypesMap[keyParts.join('.')] = function (value) { | ||
switch (typeof source[key]) { | ||
case 'boolean': | ||
return Boolean(value); | ||
case 'number': | ||
return Number(value); | ||
default: | ||
return value; | ||
} | ||
}; | ||
keyParts.pop(); | ||
return; | ||
}); | ||
} | ||
/* * | ||
@@ -73,2 +127,3 @@ * Creates a new object and merges multiple objects together, | ||
stack = {}, | ||
transform, | ||
atEnd = function (keys, key) { | ||
@@ -93,2 +148,7 @@ return keys.indexOf(key) === keys.length - 1; | ||
// define value transform function | ||
transform = defaultTypesMap[keys.join('.')] || function (input) { | ||
return input; | ||
}; | ||
// create an object graph representing string based script notation | ||
@@ -100,3 +160,3 @@ keys.forEach(function (key) { | ||
current[key] = atEnd(keys, key) ? value : {}; | ||
current[key] = atEnd(keys, key) ? transform(value) : {}; | ||
current = current[key]; | ||
@@ -189,3 +249,6 @@ }); | ||
// build a mapping for the type of all keys found in settings | ||
buildDefaultTypesMap(contents); | ||
settings.baseConfig = contents; | ||
return callback(); | ||
@@ -192,0 +255,0 @@ }); |
@@ -5,3 +5,3 @@ { | ||
"main": "./lib/settings.js", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"author": "Joshua Thomas (http://github.com/brozeph)", | ||
@@ -8,0 +8,0 @@ "engines": { |
{ | ||
"test-key" : "test-value", | ||
"sub" : { | ||
"sub-test-key" : "sub-test-value" | ||
"sub-test-key" : "sub-test-value", | ||
"sub-sub" : { | ||
"sub-sub-test-array" : ["test"], | ||
"sub-sub-test-bool" : true, | ||
"sub-sub-test-number" : 101 | ||
} | ||
} | ||
} |
@@ -301,2 +301,39 @@ describe('settings', function () { | ||
it('should coerce to type found in base config', function (done) { | ||
defaultOptions.readEnvironmentMap = { | ||
'COERCE_ARRAY' : 'sub.sub-sub.sub-sub-test-array', | ||
'COERCE_BOOL' : 'sub.sub-sub.sub-sub-test-bool', | ||
'COERCE_NUMBER' : 'sub["sub-sub"]["sub-sub-test-number"]' | ||
}; | ||
process.env.COERCE_ARRAY = '[1,2,3]'; | ||
process.env.COERCE_BOOL = 'false'; | ||
process.env.COERCE_NUMBER = '1337'; | ||
settingsLib.initialize( | ||
defaultOptions, | ||
function (err, settings) { | ||
should.not.exist(err); | ||
should.exist(settings); | ||
should.exist(settings.sub); | ||
should.exist(settings.sub['sub-sub']); | ||
should.exist(settings.sub['sub-sub']['sub-sub-test-array']); | ||
settings.sub['sub-sub']['sub-sub-test-array'].should.be.a('array'); | ||
should.exist(settings.sub['sub-sub']['sub-sub-test-bool']); | ||
settings.sub['sub-sub']['sub-sub-test-bool'].should.be.a('boolean'); | ||
should.exist(settings.sub['sub-sub']['sub-sub-test-number']); | ||
settings.sub['sub-sub']['sub-sub-test-number'].should.be.a('number'); | ||
delete process.env.COERCE_ARRAY; | ||
delete process.env.COERCE_BOOL; | ||
delete process.env.COERCE_NUMBER; | ||
done(); | ||
}); | ||
}); | ||
it('should apply environment variables over environment config', function (done) { | ||
@@ -303,0 +340,0 @@ defaultOptions.readEnvironmentMap = { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32610
810