Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 8a92d664d991d03bda41f7636b5f4c871f171a3b 0 parents
@brianloveswords authored
Showing with 207 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +116 −0 index.js
  3. +90 −0 test/habitat.test.js
1  .gitignore
@@ -0,0 +1 @@
+node_modules
116 index.js
@@ -0,0 +1,116 @@
+function habitat(prefix, defaults) {
+ if (!(this instanceof habitat))
+ return new habitat(prefix);
+ if (prefix)
+ this.prefix = prefix.toUpperCase();
+ if (defaults)
+ this.defaults = this.setDefaults(defaults);
+};
+
+/**
+ * Setup default environment options
+ */
+
+habitat.prototype.setDefaults = function setDefaults(defaults) {
+ eachKey(defaults, function (key) {
+ if (typeof this.get(key) === 'undefined')
+ this.set(key, defaults[key])
+ }.bind(this));
+ return this;
+};
+
+/**
+ * Get a key from the environment with the prefix is one was passed
+ * in at construction time.
+ *
+ * @param {String} key The key to lookup in the environment
+ * @return {Mixed} Result of the key lookup
+ */
+
+habitat.prototype.get = function get(key) {
+ var envkey = this.envkey(key);
+ return process.env[envkey];
+};
+
+/**
+ * Set a value on the environment
+ *
+ * @param {String} key
+ * @param {String} value
+ * @param {Object} this
+ */
+
+habitat.prototype.set = function set(key, value) {
+ var envkey = this.envkey(key);
+ process.env[envkey] = value;
+ return this;
+};
+
+habitat.prototype.unset = function unset(key) {
+ var envkey = this.envkey(key);
+ delete process.env[envkey];
+ return this;
+};
+
+/**
+ * Set a temporary environment. Useful for testing.
+ */
+
+habitat.prototype.temp = function (obj, callback) {
+ var original = {};
+ eachKey(obj, function (key) {
+ var envkey = this.envkey(key);
+ original[key] = this.get(key);
+ this.set(key, obj[key]);
+ }.bind(this));
+
+ var reset = function reset() {
+ eachKey(original, function (key) {
+ if (typeof original[key] !== 'undefined')
+ this.set(key, original[key]);
+ else
+ delete this.unset(key);
+ }.bind(this));
+ }.bind(this);
+
+ if (callback.length === 0) {
+ callback();
+ return reset();
+ }
+ if (callback.length === 1)
+ return callback(reset)
+};
+
+
+/**
+ * Create the environment key string with the (optional) prefix.
+ *
+ * @param {String} key
+ * @param {String} Prepared environment key string.
+ */
+
+habitat.prototype.envkey = function envkey(key) {
+ var envkey;
+ if (this.prefix)
+ envkey = this.prefix + '_' + key.toUpperCase();
+ else
+ envkey = key.toUpperCase();
+ return envkey;
+};
+
+
+/**
+ * Get a key from the environment without a prefix.
+ *
+ * @see habitat#get
+ */
+
+habitat.get = function get(value) {
+ return (new habitat().get(value))
+};
+
+function eachKey(obj, fn) {
+ return Object.keys(obj).forEach(fn);
+}
+
+module.exports = habitat;
90 test/habitat.test.js
@@ -0,0 +1,90 @@
+var fs = require('fs');
+var pathutil = require('path');
+var test = require('tap').test;
+var util = require('util');
+
+var habitat = require('..');
+
+test('habitat#get: basic test', function (t) {
+ process.env['HABITAT_HELLO'] = 'world';
+
+ var env = new habitat('habitat');
+ t.same(env.get('hello'), 'world');
+
+ var env2 = new habitat('HABITAT');
+ t.same(env.get('HELLO'), 'world');
+ t.end();
+});
+
+test('habitat#get: no prefix', function (t) {
+ process.env['SOMETHING'] = 'that thing';
+ var env = new habitat();
+ t.same(env.get('something'), 'that thing');
+ t.end();
+});
+
+test('habitat.get: shortcut for non-prefixed things', function (t) {
+ process.env['SOMETHING'] = 'other thing';
+ t.same(habitat.get('something'), 'other thing');
+ t.end();
+});
+
+test('habitat#set: set a value', function (t) {
+ var env = new habitat('habitat');
+ env.set('lol', 'wut');
+ t.same(env.get('lol'), process.env['HABITAT_LOL'], 'should be "wut"');
+ t.end();
+});
+
+
+test('habitat#temp: syncronous', function (t) {
+ var env = new habitat('habitat');
+ process.env['HABITAT_HELLO'] = 'universe';
+ env.temp({
+ hello: 'world',
+ goodnight: 'moon'
+ }, function () {
+ t.same(process.env['HABITAT_HELLO'], 'world');
+ t.same(env.get('hello'), 'world');
+ });
+
+ t.same(env.get('hello'), 'universe');
+ t.end();
+});
+
+test('habitat#temp: asyncronous', function (t) {
+ var env = new habitat('habitat');
+ process.env['HABITAT_HELLO'] = 'universe';
+ env.temp({
+ hello: 'world',
+ goodnight: 'moon'
+ }, function (done) {
+ t.same(process.env['HABITAT_HELLO'], 'world');
+ t.same(env.get('hello'), 'world');
+
+ done();
+
+ t.same(env.get('hello'), 'universe');
+ t.end();
+ });
+
+});
+
+test('habitat constructor: defaults', function (t) {
+ process.env['HABITAT_AWESOME'] = 'yep';
+ var env = new habitat('habitat', {
+ rad: 'to the max',
+ awesome: 'to the extreme'
+ });
+ t.same(env.get('rad'), 'to the max');
+ t.same(env.get('awesome'), 'yep');
+ t.end();
+});
+
+test('habitat#unset', function (t) {
+ process.env['HABITAT_WUT'] = 'lol';
+ var env = new habitat('habitat');
+ env.unset('wut');
+ t.notOk(env.get('wut'), 'should not get a result');
+ t.end();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.