Skip to content

Commit

Permalink
Merge 8725f35 into af344ac
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuste committed May 22, 2017
2 parents af344ac + 8725f35 commit d4a0fa3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @constant Process property name for environment type
*/
var ENV_TYPE, ENV_TYPE_DEV, ENV_TYPE_PROD, Environment;
var ENV_TYPE, ENV_TYPE_DEV, ENV_TYPE_PROD, Environment, MatchNonWord;

ENV_TYPE = "env";

Expand All @@ -26,11 +26,17 @@ ENV_TYPE_DEV = "dev";

ENV_TYPE_PROD = "prod";

MatchNonWord = /\W/g;

Environment = (function() {
function Environment(config) {
this.config = config;
}

Environment.prototype.transformKey = function(key) {
return key.replace(MatchNonWord, '_');
};


/**
* @function Gets section or key within a section. Tries to get
Expand Down Expand Up @@ -101,6 +107,9 @@ Environment = (function() {
if ('string' !== typeof key) {
throw new Error('key must be a string');
}
if ((val = process.env[this.transformKey(key)]) != null) {
return val;
}
if ((val = process.env[key]) != null) {
return val;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Alvaro Juste <juste.alvaro@gmail.com>",
"name": "jaune-env",
"version": "0.0.5",
"version": "0.0.6",
"description": "Singleton environment information / configuration",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions src/environment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ ENV_TYPE_DEV = "dev"
###
ENV_TYPE_PROD = "prod"

MatchNonWord = /\W/g

class Environment

constructor: (@config) ->

transformKey: (key) -> key.replace MatchNonWord, '_'

###*
* @function Gets section or key within a section. Tries to get
* _config based on environtment or generic. But first tries to
Expand Down Expand Up @@ -80,6 +84,7 @@ class Environment
###
getProcessProperty : (key, def) ->
throw new Error 'key must be a string' if 'string' isnt typeof key
return val if (val = process.env[@transformKey key])?
return val if (val = process.env[key])?
(process.jaune ? (process.jaune = {}))[key] ? def

Expand Down
12 changes: 10 additions & 2 deletions test/environment/getEnvProperty.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ describe 'environment', ->
describe 'getEnvProperty on development', ->
before ->
process.jaune = env: 'dev'
process.env['section2.foo'] = 2
process.env['section2_foo'] = 2
process.env['section3_foo'] = 3
process.env['section4______foo'] = 4
_env = new lib
section1:
foo: no
Expand Down Expand Up @@ -63,8 +65,14 @@ describe 'environment', ->
it 'gets value when section has only 2 steps with develop on last step', ->
equal yes, _env.getEnvProperty 'section1.section13', 'foo'

it 'gets value from env variables rather than config', ->
it 'gets value from env variables rather than config with dot replaced', ->
equal 2, _env.getEnvProperty 'section2.foo'

it 'gets value from env variables rather than config with dot', ->
equal 3, _env.getEnvProperty 'section3_foo'

it 'gets value from env variables rather than config with dot', ->
equal 4, _env.getEnvProperty 'section4 #-_{!foo'

it 'gets value from env variables rather than config using two steps', ->
equal 2, _env.getEnvProperty 'section2', 'foo'

0 comments on commit d4a0fa3

Please sign in to comment.