Skip to content

Commit

Permalink
Merge pull request #4 from ajuste/feature/json_parse_env
Browse files Browse the repository at this point in the history
feat: convert env prop to object
  • Loading branch information
ajuste committed May 28, 2017
2 parents 8622b6c + 92588ee commit f6615ed
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Let`s tell jaune-env we are running under development
env.setProcessProperty('env', 'dev');
```

After that all properties look up will be performed first on {property}Develop and if not found; we fallback to {property}:
After that all properties look up will be performed first on {property}-dev
and if not found; we fallback to {property}:

```js
env.getEnvProperty('api'); // --> object from apiDevelop
Expand All @@ -81,6 +82,10 @@ env.getEnvProperty('foo'); // true
```

## Getting information from env variables
Both getEnvProperty and getProcessProperty will first check for the property to exist in an environment variable.
Both getEnvProperty and getProcessProperty will first check for the property
to exist in an environment variable.

For getEnvProperty it will concat section and property with a dot in the middle if both are specified.
For getEnvProperty it will concat section and property with a dot in the
middle if both are specified.

Also getEnvProperty will convert to object if value is a valid JSON.
20 changes: 11 additions & 9 deletions lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ Environment = (function() {
return key.replace(MatchNonWord, '_');
};

Environment.prototype.parseValue = function(value) {
try {
return JSON.parse(value);
} catch (error) {
return value;
}
};


/**
* @function Gets section or key within a section. Tries to get
Expand Down Expand Up @@ -72,7 +80,7 @@ Environment = (function() {
if (arguments.length >= 2 && (data != null)) {
data = (ref1 = data["" + property + this.envPost]) != null ? ref1 : data[property];
}
return data;
return this.parseValue(data);
};


Expand Down Expand Up @@ -103,17 +111,11 @@ Environment = (function() {
*/

Environment.prototype.getProcessProperty = function(key, def) {
var ref, ref1, val;
var ref, ref1, ref2, ref3;
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;
}
return (ref = ((ref1 = process.jaune) != null ? ref1 : (process.jaune = {}))[key]) != null ? ref : def;
return this.parseValue((ref = (ref1 = (ref2 = process.env[this.transformKey(key)]) != null ? ref2 : process.env[key]) != null ? ref1 : ((ref3 = process.jaune) != null ? ref3 : (process.jaune = {}))[key]) != null ? ref : def);
};


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.6",
"version": "0.0.7",
"description": "Singleton environment information / configuration",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 12 additions & 5 deletions src/environment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Environment

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

parseValue: (value) ->
try
JSON.parse value
catch
value

###*
* @function Gets section or key within a section. Tries to get
* _config based on environtment or generic. But first tries to
Expand All @@ -45,7 +51,6 @@ class Environment
return proc if (proc = @getProcessProperty(
"#{section}#{if property? then '.' + property else ''}"))?


return unless @config

@determineEnv()
Expand All @@ -62,7 +67,7 @@ class Environment
if arguments.length >= 2 and data?
data = data["#{property}#{@envPost}"] ? data[property]

data
@parseValue data

###*
* @function Sets process property
Expand All @@ -84,9 +89,11 @@ 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

@parseValue (
process.env[@transformKey key] ? process.env[key] ? (process.jaune ?
(process.jaune = {}))[key] ? def
)

###*
* @function Determines environment
Expand Down
36 changes: 21 additions & 15 deletions test/environment/getEnvProperty.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
lib = require '../../'
{
equal
deepEqual
} = require 'assert'

_env = null
env = null

describe 'environment', ->
describe 'initEnv', ->
it 'sets configuration', ->
_env = new lib
env = new lib
section1:
section11:
data: 'hi'
Expand All @@ -17,24 +18,25 @@ describe 'environment', ->

describe 'getEnvProperty', ->
it 'gets undefined for non existing property', ->
equal undefined, _env.getEnvProperty 'prop'
equal env.getEnvProperty('prop'), undefined

it 'gets undefined for non existing property in section', ->
equal undefined, _env.getEnvProperty 'section2', 'prop'
equal env.getEnvProperty('section2', 'prop'), undefined

it 'gets undefined for non existing property in nested section', ->
equal undefined, _env.getEnvProperty 'section1.section2', 'prop'
equal env.getEnvProperty('section1.section2', 'prop'), undefined

it 'gets correct value for nested and existing', ->
equal 'hi', _env.getEnvProperty 'section1.section11', 'data'
equal env.getEnvProperty('section1.section11', 'data'), 'hi'

describe 'getEnvProperty on development', ->
before ->
process.jaune = env: 'dev'
process.env['section2_foo'] = 2
process.env['section3_foo'] = 3
process.env['section4______foo'] = 4
_env = new lib
process.env['section5_foo'] = JSON.stringify a: 1, b: [3], c: d: e: f: 'a'
env = new lib
section1:
foo: no
'section1-dev':
Expand All @@ -54,25 +56,29 @@ describe 'environment', ->
process.jaune = {}

it 'gets value when section has only 1 step', ->
equal yes, _env.getEnvProperty 'section1', 'foo'
equal env.getEnvProperty('section1', 'foo'), yes

it 'gets value when section has only 2 steps', ->
equal yes, _env.getEnvProperty 'section1.section11', 'foo'
equal env.getEnvProperty('section1.section11', 'foo'), yes

it 'gets value when section has only 2 steps no develop on last step', ->
equal no, _env.getEnvProperty 'section1.section12', 'foo'
equal env.getEnvProperty('section1.section12', 'foo'), no

it 'gets value when section has only 2 steps with develop on last step', ->
equal yes, _env.getEnvProperty 'section1.section13', 'foo'
equal env.getEnvProperty('section1.section13', 'foo'), yes

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

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

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

it 'gets value from env variables which is a json', ->
deepEqual env.getEnvProperty('section5_foo'),
a: 1, b: [3], c: d: e: f: 'a'

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

0 comments on commit f6615ed

Please sign in to comment.