From 92588ee2eba4b00216954c489cde75a79ba92e0a Mon Sep 17 00:00:00 2001 From: Alvaro Juste Date: Sat, 27 May 2017 20:31:44 -0400 Subject: [PATCH] feat: convert env prop to object --- README.md | 11 +++++--- lib/environment.js | 20 +++++++------- package.json | 2 +- src/environment.coffee | 17 ++++++++---- test/environment/getEnvProperty.coffee | 36 +++++++++++++++----------- 5 files changed, 53 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 5c06283..3042422 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/lib/environment.js b/lib/environment.js index 3ae9a0e..93af54e 100644 --- a/lib/environment.js +++ b/lib/environment.js @@ -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 @@ -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); }; @@ -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); }; diff --git a/package.json b/package.json index 2711d55..d6dd64d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "author": "Alvaro Juste ", "name": "jaune-env", - "version": "0.0.6", + "version": "0.0.7", "description": "Singleton environment information / configuration", "main": "index.js", "scripts": { diff --git a/src/environment.coffee b/src/environment.coffee index ed8908f..aba54d0 100644 --- a/src/environment.coffee +++ b/src/environment.coffee @@ -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 @@ -45,7 +51,6 @@ class Environment return proc if (proc = @getProcessProperty( "#{section}#{if property? then '.' + property else ''}"))? - return unless @config @determineEnv() @@ -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 @@ -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 diff --git a/test/environment/getEnvProperty.coffee b/test/environment/getEnvProperty.coffee index 5aa5e6e..a41d659 100644 --- a/test/environment/getEnvProperty.coffee +++ b/test/environment/getEnvProperty.coffee @@ -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' @@ -17,16 +18,16 @@ 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 -> @@ -34,7 +35,8 @@ describe 'environment', -> 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': @@ -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