Skip to content

Commit

Permalink
Merge branch 'add/env'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 28, 2012
2 parents 248f8e2 + 584d873 commit 5f6f647
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Readme.md
Expand Up @@ -109,6 +109,19 @@ yields:

Convert "yes", "no", "enabled", "disabled" into booleans.

### eson.env([prefix])

Allow environment variables to define config values. If you have the following:

```js
{
"upload path": "/data/uploads"
}
```

You could then export `UPLOAD_PATH=/tmp` to change this value. Optionally when
a `prefix` is given such as "MYAPP_" then you must prefix such as `MYAPP_UPLOAD_PATH=/tmp`.

### eson.replace(str, val)

The replace plugin allows you to replace arbitrary substrings, useful
Expand Down
1 change: 1 addition & 0 deletions lib/eson.js
Expand Up @@ -22,6 +22,7 @@ exports.version = '0.0.1';
*/

exports.ms = require('./plugins/ms');
exports.env = require('./plugins/env');
exports.glob = require('./plugins/glob');
exports.bools = require('./plugins/bools');
exports.replace = require('./plugins/replace');
Expand Down
13 changes: 13 additions & 0 deletions lib/plugins/env.js
@@ -0,0 +1,13 @@

/**
* Allow environment variables to take precedence over `val`,
* with optional `prefix`.
*/

module.exports = function(prefix){
prefix = prefix || '';
return function(key, val){
var name = prefix + key.toUpperCase().split(' ').join('_');
return process.env[name] || val;
}
};
2 changes: 1 addition & 1 deletion test/bools.js
Expand Up @@ -2,7 +2,7 @@
var Parser = require('../')
, bools = Parser.bools;

describe('ms', function(){
describe('bools', function(){
it('should parse string bool representations', function(){
bools('', 'yes').should.be.true;
bools('', 'enabled').should.be.true;
Expand Down
23 changes: 23 additions & 0 deletions test/env.js
@@ -0,0 +1,23 @@

var Parser = require('../')
, env = Parser.env;

describe('env(prefix)', function(){
it('should allow setting environment variables', function(){
var fn = env();
fn('foo', 'bar').should.equal('bar');
process.env.FOO = 'baz';
fn('foo', 'bar').should.equal('baz');
process.env.DEV_UI = 'yes';
fn('dev ui', 'no').should.equal('yes');
})

it('should accept a prefix', function(){
var fn = env('NB_');
fn('dev ui', 'no').should.equal('no');
process.env.DEV_UI = 'yes';
fn('dev ui', 'no').should.equal('no');
process.env.NB_DEV_UI = 'yes';
fn('dev ui', 'no').should.equal('yes');
})
})

0 comments on commit 5f6f647

Please sign in to comment.