Permalink
Browse files

[bump] Support camelCase lookups.

  • Loading branch information...
1 parent af245f7 commit b65bff5b96c059dbdf4ba3cefc5e2c3ba7dd4a7e @brianloveswords committed Oct 19, 2012
Showing with 56 additions and 10 deletions.
  1. +23 −9 README.md
  2. +21 −0 index.js
  3. +1 −1 package.json
  4. +11 −0 test/habitat.test.js
View
@@ -1,4 +1,5 @@
# habitat [![Build Status](https://secure.travis-ci.org/brianloveswords/habitat.png)](http://travis-ci.org/brianloveswords/habitat)
+## Version 0.3.0
Library for managing your environment vars.
@@ -28,7 +29,7 @@ your app is called `airsupport`, it's probably good to namespace your
environment variables like so:
```bash
-export AIRSUPPORT_HOST=lolcathost
+export AIRSUPPORT_HOST='lolcathost'
export AIRSUPPORT_PORT=3000
export AIRSUPPORT_WEBSOCKETS=true
```
@@ -64,7 +65,7 @@ Gets a key from the environment. Automatically prefixes with the
like a `boolean`, `number` or `json`, so you can do things like this:
```bash
-exports APP_ADMINS='["me@example.com", "you@example.com"]'
+export APP_ADMINS='["me@example.com", "you@example.com"]'
```
```js
var env = new habitat('app');
@@ -75,22 +76,35 @@ 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.
-You can also do stuff like this:
+### Getting objects
+`get` will automatically return objects if you take advantage of common prefixing:
```bash
-exports APP_DB="redis"
-exports APP_REDIS_HOST="127.0.0.1"
-exports APP_REDIS_PORT=6379
+export APP_DB='redis'
+export APP_REDIS_HOST='127.0.0.1'
+export APP_REDIS_PORT=6379
```
+
```js
var env = new habitat('app');
var db = env.get('db');
-var options = env.get('redis');
-console.log(db); // "redis"
-console.log(options.host); // "127.0.0.1"
+var options = env.get(db);
+console.log(options.host); // '127.0.0.1'
console.log(options.port); // 6379
```
+### Getting keys using camelCase
+You can also use camelcase instead of underscores if you want, habitat's got your back.
+
+```bash
+export APP_SOME_LONG_KEY='great'
+```
+
+```js
+var env = new habitat('app');
+console.log(env.get('someLongKey')) // 'great'
+```
+
## habitat#set(key, value)
Sets an environment variable, with prefix if passed.
View
@@ -29,6 +29,9 @@ habitat.prototype.setDefaults = function setDefaults(defaults) {
*/
habitat.prototype.get = function get(key, someDefault) {
+ if (key.match(/[a-z]+[A-Z]/))
+ return this.get(fromCamelCase(key));
+
var envkey = this.envkey(key);
var value = process.env[envkey] || someDefault;
if (typeof value !== 'undefined')
@@ -181,8 +184,26 @@ habitat.get = function get(value) {
return (new habitat().get(value))
};
+/**
+ * Shortcut for Object.keys(obj).forEach(fn);
+ */
+
function eachKey(obj, fn) {
return Object.keys(obj).forEach(fn);
}
+/**
+ * Convert a camelcased string to an underscored string
+ *
+ * @param {String} input
+ * @return {String} underscored string
+ */
+
+function fromCamelCase(input) {
+ var expression = /([a-z])([A-Z])/g;
+ return input.replace(expression, function (_, lower, upper) {
+ return lower + '_' + upper.toLowerCase();
+ });
+};
+
module.exports = habitat;
View
@@ -1,6 +1,6 @@
{
"name": "habitat",
- "version": "0.2.2",
+ "version": "0.3.0",
"description": "Small library for managing environment variables",
"main": "index.js",
"directories": {
View
@@ -158,3 +158,14 @@ test('habitat#get: should try `getAsObject` if no value is found', function (t)
t.notOk(value, 'should not have a value');
t.end();
});
+
+test('habitat#get: should try to expand camelcase', function (t) {
+ process.env['APP_REDIS_HOST'] = 'localhost';
+ process.env['APP_REDIS_PORT'] = 3000;
+ var env = new habitat('app');
+ var host = env.get('redisHost');
+ var port = env.get('redisPort');
+ t.same(host, 'localhost');
+ t.same(port, 3000);
+ t.end();
+});

0 comments on commit b65bff5

Please sign in to comment.