Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Automatic variable parsing [bump]

  • Loading branch information...
commit b65d9386e9e4f7a2af269e1bb4514d89ec169759 1 parent 8c32829
@brianloveswords authored
Showing with 60 additions and 3 deletions.
  1. +14 −1 README.md
  2. +25 −1 index.js
  3. +1 −1  package.json
  4. +20 −0 test/habitat.test.js
View
15 README.md
@@ -57,7 +57,20 @@ var port = env.get('port');
## habitat#get(key)
-Gets a key from the environment. Automatically prefixes with the `prefix` passed to the constructor, if necessary.
+Gets a key from the environment. Automatically prefixes with the
+`prefix` passed to the constructor, if necessary.
+
+`habitat#get` will also try to do some parsing of the value if it looks
+like a `boolean`, `number` or `json`, so you can do things like this:
+
+```bash
+exports APP_ADMINS='["me@example.com", "you@example.com"]';
+```
+```js
+var env = habitat('app');
+var admins = env.get('admins');
+console.log(admins.indexOf('you@example.com')) // 1
+```
## habitat#set(key, value)
View
26 index.js
@@ -29,10 +29,34 @@ habitat.prototype.setDefaults = function setDefaults(defaults) {
habitat.prototype.get = function get(key) {
var envkey = this.envkey(key);
- return process.env[envkey];
+ var value = process.env[envkey];
+ return habitat.parse(value);
};
/**
+ * Attempt to nativize things coming from the environment.
+ *
+ * @param {String} thing The string coming in from the environment
+ * @return {Mixed} A native object if parseable, otherwise raw string
+ */
+
+habitat.parse = function parse(thing) {
+ var bool = /^(true|false)$/;
+ var number = /^\d+(\.\d+)?$/;
+ var json = /^(\{.*?\})|(\[.*?\])$/;
+ if (bool.test(thing))
+ return thing === 'true';
+ if (number.test(thing))
+ return parseInt(thing, 10);
+ if (json.test(thing)) {
+ try { return JSON.parse(thing) }
+ catch(err) { return thing }
+ }
+ return thing;
+};
+
+
+/**
* Set a value on the environment
*
* @param {String} key
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "habitat",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "Small library for managing environment variables",
"main": "index.js",
"directories": {
View
20 test/habitat.test.js
@@ -88,3 +88,23 @@ test('habitat#unset', function (t) {
t.notOk(env.get('wut'), 'should not get a result');
t.end();
});
+
+test('habitat.parse: parse potential things', function (t) {
+ t.same(typeof habitat.parse('true'), 'boolean');
+ t.same(typeof habitat.parse('false'), 'boolean');
+ t.same(typeof habitat.parse('3000'), 'number');
+ t.same(typeof habitat.parse('12.0'), 'number');
+ t.same(typeof habitat.parse('{"hi": "hello"}'), 'object');
+ t.same(habitat.parse('{"hi": "hello"}').hi, 'hello');
+ t.same(typeof habitat.parse('[1,2,3]'), 'object');
+ t.same(habitat.parse('[1,2,3]')[2], 3);
+ t.same(typeof habitat.parse('12/>SDc80'), 'string');
+ t.end();
+});
+
+test('habitat#get: array parsing', function (t) {
+ process.env['HABITAT_ADMINS'] = '["me@example.com", "you@example.com"]';
+ var env = new habitat('habitat');
+ t.same(env.get('admins').indexOf('you@example.com'), 1);
+ t.end();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.