Permalink
Browse files

Allow defaults to `get` [bump]

  • Loading branch information...
1 parent 7607d54 commit 71069f386b699b0f1b329a8130bc3ec24a3eac93 @brianloveswords committed Oct 4, 2012
Showing with 16 additions and 4 deletions.
  1. +4 −1 README.md
  2. +3 −2 index.js
  3. +1 −1 package.json
  4. +8 −0 test/habitat.test.js
View
@@ -55,7 +55,7 @@ var env = new habitat('airsupport', { port: 3000 })
var port = env.get('port');
```
-## habitat#get(key)
+## habitat#get(key, default)
Gets a key from the environment. Automatically prefixes with the
`prefix` passed to the constructor, if necessary.
@@ -72,6 +72,9 @@ var admins = env.get('admins');
console.log(admins.indexOf('you@example.com')) // 1
```
+If a `default` is passed, if the key is undefined in either the env or
+the constructor-set defaults, it will fall back to that.
+
## habitat#set(key, value)
Sets an environment variable, with prefix if passed.
View
@@ -24,12 +24,13 @@ habitat.prototype.setDefaults = function setDefaults(defaults) {
* in at construction time.
*
* @param {String} key The key to lookup in the environment
+ * @param {Mixed} someDefault
* @return {Mixed} Result of the key lookup
*/
-habitat.prototype.get = function get(key) {
+habitat.prototype.get = function get(key, someDefault) {
var envkey = this.envkey(key);
- var value = process.env[envkey];
+ var value = process.env[envkey] || someDefault;
return habitat.parse(value);
};
View
@@ -1,6 +1,6 @@
{
"name": "habitat",
- "version": "0.1.1",
+ "version": "0.2.0",
"description": "Small library for managing environment variables",
"main": "index.js",
"directories": {
View
@@ -108,3 +108,11 @@ test('habitat#get: array parsing', function (t) {
t.same(env.get('admins').indexOf('you@example.com'), 1);
t.end();
});
+
+
+test('habitat#get: defaults', function (t) {
+ var env = new habitat('noexist');
+ t.same(env.get('port', 3000), 3000);
+ t.same(env.get('yayay'), undefined);
+ t.end();
+});

0 comments on commit 71069f3

Please sign in to comment.