Permalink
Browse files

add env(prefix) support

  • Loading branch information...
1 parent 64c0b8e commit b3278fd1a6ea85582c810cfaee2689234470d311 @tj committed Jun 28, 2012
Showing with 24 additions and 8 deletions.
  1. +2 −0 Readme.md
  2. +8 −4 lib/plugins/env.js
  3. +14 −4 test/env.js
View
@@ -109,6 +109,8 @@ yields:
Convert "yes", "no", "enabled", "disabled" into booleans.
+### eson.env([prefix])
+
### eson.replace(str, val)
The replace plugin allows you to replace arbitrary substrings, useful
View
@@ -1,9 +1,13 @@
/**
- * Allow environment variables to take precedence over `val`.
+ * Allow environment variables to take precedence over `val`,
+ * with optional `prefix`.
*/
-module.exports = function(key, val){
- var name = key.toUpperCase().split(' ').join('_');
- return process.env[name] || val;
+module.exports = function(prefix){
+ prefix = prefix || '';
+ return function(key, val){
+ var name = prefix + key.toUpperCase().split(' ').join('_');
+ return process.env[name] || val;
+ }
};
View
@@ -2,12 +2,22 @@
var Parser = require('../')
, env = Parser.env;
-describe('env', function(){
+describe('env(prefix)', function(){
it('should allow setting environment variables', function(){
- env('foo', 'bar').should.equal('bar');
+ var fn = env();
+ fn('foo', 'bar').should.equal('bar');
process.env.FOO = 'baz';
- env('foo', 'bar').should.equal('baz');
+ fn('foo', 'bar').should.equal('baz');
process.env.DEV_UI = 'yes';
- env('dev ui', 'no').should.equal('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 b3278fd

Please sign in to comment.