Skip to content

Commit

Permalink
Merge 30be7c9 into af344ac
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuste authored May 22, 2017
2 parents af344ac + 30be7c9 commit 426137a
Show file tree
Hide file tree
Showing 5 changed files with 51 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'
25 changes: 25 additions & 0 deletions test/environment/transformKey.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
lib = require '../../'
{
equal
} = require 'assert'

describe 'environment', ->

describe 'transformKey', ->

before ->
@env = new lib
section1:
section11:
data: 'hi'
section1Debug:
foo: yes

it 'replaces spaces', ->
equal '__', @env.transformKey ' '

it 'maintains numbers and letters', ->
equal 'test123', @env.transformKey 'test123'

it 'replaces symbols', ->
equal '______', @env.transformKey '!@#/._'

0 comments on commit 426137a

Please sign in to comment.