-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
469 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"env": { | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"rules": { | ||
"indent": ["error", 2], | ||
"linebreak-style": ["error", "unix"], | ||
"quotes": ["error", "single"], | ||
"semi": ["error", "always"], | ||
"comma-dangle": ["error", "always-multiline"], | ||
"no-console": ["error", { "allow": ["error"] }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: node_js | ||
script: npm run travis | ||
node_js: | ||
- "6" | ||
- "5" | ||
- "4" | ||
- "0.12" | ||
- "0.11" | ||
- "0.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,35 @@ | ||
# confenv | ||
Load configuration from environment variables | ||
[![Build Status](https://travis-ci.org/allevo/confenv.svg?branch=master)](https://travis-ci.org/allevo/confenv) | ||
[![Coverage Status](https://coveralls.io/repos/github/allevo/confenv/badge.svg?branch=master)](https://coveralls.io/github/allevo/confenv?branch=master) | ||
|
||
Load configuration from environment variables. | ||
|
||
Simple, easy, no deps. | ||
|
||
## Why | ||
This module is useful for all environment that requires that the configuration is loaded from environment. | ||
But the environment variables aren't typed. And this means you must cast all values to the right type. | ||
|
||
Using this module, your code is clean from any type casting. | ||
|
||
## getConfigurationFrom | ||
This function takes an object as parameter and return a `Configuration` instance | ||
|
||
## getConfigurationFromEnvironment | ||
This function calls getConfigurationFrom with `process.env` as parameter | ||
|
||
## Configuration class | ||
There're two method types of Configuration class: `getAs<Type>OrUndefined` and `getAs<Type>OrThrow`. | ||
|
||
`<Type>` could be one of them: | ||
- String | ||
- Integer | ||
- Array | ||
- Boolean | ||
- Float | ||
|
||
### getAs<Type>OrUndefined | ||
Return `<Type>` or `undefined` | ||
|
||
### getAs<Type>OrThrow | ||
Return `<Type>` or throw a `TypeError` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
'use strict'; | ||
|
||
function getConfigurationFrom(obj) { | ||
function isIntegerPolyfill(n) { | ||
if (!Number.isFinite(n)) return false; | ||
if (n % 1 === 0) return true; | ||
return false; | ||
} | ||
var isInteger = 'isInteger' in Number ? Number.isInteger : isIntegerPolyfill; | ||
|
||
var ret = { | ||
getAsStringOrUndefined: function getAsStringOrUndefined(k) { | ||
if (!(k in obj)) return undefined; | ||
if (obj[k] instanceof Object && typeof obj[k] === 'object') return JSON.stringify(obj[k]); | ||
return obj[k] + ''; | ||
}, | ||
getAsStringOrThrow: function getAsStringOrThrow(k) { | ||
var d = ret.getAsStringOrUndefined(k); | ||
if (d === undefined) throw new TypeError('Cannot cast ' + k + ' to string'); | ||
return d; | ||
}, | ||
getAsIntegerOrUndefined: function getAsIntegerOrUndefined(k) { | ||
if (!(k in obj)) return undefined; | ||
if (obj[k] === '') return undefined; | ||
if (obj[k] === true) return undefined; | ||
if (obj[k] === false) return undefined; | ||
if (obj[k] === null) return undefined; | ||
if (obj[k] === undefined) return undefined; | ||
if (obj[k] instanceof Array) return undefined; | ||
var d = Number(obj[k]); | ||
if (!isInteger(d)) return undefined; | ||
return d; | ||
}, | ||
getAsIntegerOrThrow: function getAsIntegerOrThrow(k) { | ||
var d = ret.getAsIntegerOrUndefined(k); | ||
if (d === undefined) throw new TypeError('Cannot cast ' + k + ' to integer'); | ||
return d; | ||
}, | ||
getAsBooleanOrUndefined: function getAsBooleanOrUndefined(k) { | ||
if (!(k in obj)) return undefined; | ||
var d = obj[k]; | ||
if (d === 'true') return true; | ||
if (d === '1') return true; | ||
if (d === true) return true; | ||
if (d === 1) return true; | ||
|
||
if (d === 'false') return false; | ||
if (d === '0') return false; | ||
if (d === false) return false; | ||
if (d === 0) return false; | ||
if (d === '') return false; | ||
|
||
return undefined; | ||
}, | ||
getAsBooleanOrThrow: function getAsBooleanOrThrow(k) { | ||
var d = ret.getAsBooleanOrUndefined(k); | ||
if (d === undefined) throw new TypeError('Cannot cast ' + k + ' to boolean'); | ||
return d; | ||
}, | ||
getAsArrayOrUndefined: function getAsArrayOrUndefined(k) { | ||
if (!(k in obj)) return undefined; | ||
if (obj[k] === undefined) return undefined; | ||
if (obj[k] === null) return undefined; | ||
if (typeof obj[k] === 'string') return obj[k].split(','); | ||
return [obj[k]]; | ||
}, | ||
getAsArrayOrThrow: function getAsArrayOrUndefined(k) { | ||
var d = ret.getAsArrayOrUndefined(k); | ||
if (d === undefined) throw new TypeError('Cannot cast ' + k + ' to array'); | ||
return d; | ||
}, | ||
getAsNumberOrUndefined: function getAsNumberOrUndefined(k) { | ||
if (!(k in obj)) return undefined; | ||
if (obj[k] === '') return undefined; | ||
if (obj[k] === true) return undefined; | ||
if (obj[k] === false) return undefined; | ||
if (obj[k] === null) return undefined; | ||
if (obj[k] === undefined) return undefined; | ||
if (obj[k] instanceof Array) return undefined; | ||
var d = Number(obj[k]); | ||
if (!Number.isFinite(d)) return undefined; | ||
return d; | ||
}, | ||
getAsNumberOrThrow: function getAsNumberOrThrow(k) { | ||
var d = ret.getAsNumberOrUndefined(k); | ||
if (d === undefined) throw new TypeError('Cannot cast ' + k + ' to number'); | ||
return d; | ||
}, | ||
}; | ||
|
||
return ret; | ||
} | ||
|
||
function getConfigurationFromEnvironment() { | ||
return getConfigurationFrom(process.env); | ||
} | ||
|
||
module.exports = { | ||
getConfigurationFrom: getConfigurationFrom, | ||
getConfigurationFromEnvironment: getConfigurationFromEnvironment, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "confenv", | ||
"version": "1.0.0", | ||
"description": "Load configuration from environment variables", | ||
"main": "index.js", | ||
"scripts": { | ||
"mocha": "mocha test", | ||
"lint": "eslint .", | ||
"coverage": "istanbul cover _mocha -- -R spec", | ||
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"travis": "npm run lint && npm run mocha && npm run coveralls", | ||
"test": "npm run lint && npm run mocha && npm run coverage" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/allevo/confenv.git" | ||
}, | ||
"keywords": [ | ||
"config", | ||
"conf", | ||
"configuration", | ||
"environment", | ||
"process.env" | ||
], | ||
"author": "allevo", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/allevo/confenv/issues" | ||
}, | ||
"homepage": "https://github.com/allevo/confenv", | ||
"devDependencies": { | ||
"coveralls": "^2.11.9", | ||
"eslint": "^2.11.0", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^2.5.3", | ||
"mocha-lcov-reporter": "^1.2.0" | ||
} | ||
} |
Oops, something went wrong.