Comparing version 0.2.2 to 0.2.5
@@ -15,6 +15,9 @@ var defaults = ['test', 'devel', 'stage', 'production']; | ||
var value = this.value; | ||
process.env.NODE_ENV = val; | ||
return function revert() { | ||
process.env.NODE_ENV = value; | ||
if(this(val)) { | ||
process.env.NODE_ENV = val; | ||
return function revert() { | ||
process.env.NODE_ENV = value; | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -27,4 +30,5 @@ | ||
* @param get A custom get function. | ||
* @param set A custom set function. | ||
* | ||
* @return An environment object. | ||
* @return An environment query object. | ||
*/ | ||
@@ -78,3 +82,3 @@ function nenv(available, get, set) { | ||
function query(value) { | ||
value = (arguments.length ? '' + value : query.value).toLowerCase(); | ||
value = (arguments.length ? '' + value : '' + query.value).toLowerCase(); | ||
for(var z in map) { | ||
@@ -121,2 +125,8 @@ if(~map[z].indexOf(value)) { | ||
// initial value validity | ||
query.valid = Boolean(query()); | ||
// determine if it was specified | ||
query.defined = query.value !== undefined; | ||
// object map | ||
@@ -128,2 +138,26 @@ query.map = map; | ||
// setup equality boolean aliases | ||
query.keys.forEach(function(key) { | ||
Object.defineProperty(query, key, { | ||
get: function equals() { | ||
return ('' + query.get()).toLowerCase() === key; | ||
} | ||
}) | ||
}) | ||
// get an object view suitable for json | ||
query.jsonify = function() { | ||
var o = {}; | ||
for(var k in query) { | ||
if(typeof query[k] !== 'function') { | ||
o[k] = query[k]; | ||
} | ||
} | ||
// pick up boolean aliases | ||
query.keys.forEach(function(key) { | ||
o[key] = query[key] ; | ||
}) | ||
return o; | ||
} | ||
// get and set functions | ||
@@ -130,0 +164,0 @@ query.get = get.bind(query); |
{ | ||
"name": "nenv", | ||
"description": "Utility for managing node development environments", | ||
"version": "0.2.2", | ||
"version": "0.2.5", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -48,2 +48,14 @@ "main": "lib/index.js", | ||
{ | ||
"title": "Example", | ||
"inc": "../../defaults.js", | ||
"type": "code", | ||
"language": "javascript" | ||
}, | ||
{ | ||
"text": "Executed with `NODE_ENV=devel`, yields:", | ||
"bin": "NODE_ENV=devel node defaults.js", | ||
"type": "code", | ||
"language": "json" | ||
}, | ||
{ | ||
"inc": [ | ||
@@ -50,0 +62,0 @@ "developer.md", |
123
README.md
@@ -7,2 +7,11 @@ Table of Contents | ||
* [Usage](#usage) | ||
* [env([value])](#envvalue) | ||
* [env.value](#envvalue) | ||
* [env.valid](#envvalid) | ||
* [env.defined](#envdefined) | ||
* [env.get()](#envget) | ||
* [env.set(val)](#envsetval) | ||
* [env.defaults](#envdefaults) | ||
* [Environments](#environments) | ||
* [Example](#example) | ||
* [Developer](#developer) | ||
@@ -29,17 +38,109 @@ * [Test](#test) | ||
```javascript | ||
function nenv([environments, get, set]) | ||
``` | ||
```javascript | ||
var env = require('nenv')(); | ||
if(!env.defined) { | ||
// do something when no environment was specified | ||
// maybe: env.set(env.DEVEL) or whichever default you want | ||
}else if(!env()) { | ||
// do something when the specified environment is invalid | ||
}else if(env.test) { | ||
// do something for test environment | ||
} | ||
``` | ||
* `environments`: Array or object of custom environments, if not specified the `defaults` are used. | ||
* `get`: A custom function for getting the environment value (optional). | ||
* `set`: A custom function for setting the environment value (optional). | ||
### env([value]) | ||
Determines if an environment value is valid. Returns `false` is the supplied value is invalid or the string key for the environment if valid. | ||
If no value is supplied then `env.value` is used which allows testing whether the default value is valid by calling with no arguments. | ||
### env.value | ||
The value returned from `get()` when `nenv()` was called, the initial environment value. | ||
### env.valid | ||
Boolean that determines whether `env.value` is a recognised environment. | ||
### env.defined | ||
Determines whether an initial value (`env.value`) was defined. | ||
### env.get() | ||
Get the *current* value of the environment, the default implementation returns `process.env.NODE_ENV`. | ||
### env.set(val) | ||
Set the *current* value of the environment, the default implementation returns `false` if the supplied value is not a known environment alias otherwise a function that may be called to revert to the *previous* value. | ||
### env.defaults | ||
Default values to use. | ||
```javascript | ||
['test', 'devel', 'stage', 'production']; | ||
``` | ||
### Environments | ||
Pass an object or array to define your available environments: | ||
```javascript | ||
var nenv = require('nenv') | ||
, env = nenv(); | ||
, env = nenv({production: ['production', 'pro'], dev: 'dev', test: 'test'}); | ||
console.log(env.keys); | ||
``` | ||
// environment value fixed at creation time | ||
console.log(env.value); | ||
## Example | ||
// get current value of environment | ||
console.log(env.get()); | ||
```javascript | ||
var env = require('./')() | ||
, str = JSON.stringify(env.jsonify(), undefined, 2); | ||
process.stdout.write(str); | ||
``` | ||
// check environment is valid | ||
console.log(env(env.TEST)); | ||
console.log(env(env.DEVEL)); | ||
console.log(env(env.STAGE)); | ||
console.log(env(env.PRODUCTION)); | ||
console.log(env('unknown')); | ||
Executed with `NODE_ENV=devel`, yields: | ||
```json | ||
{ | ||
"TEST": "test", | ||
"DEVEL": "devel", | ||
"STAGE": "stage", | ||
"PRODUCTION": "production", | ||
"value": "devel", | ||
"valid": true, | ||
"defined": true, | ||
"map": { | ||
"test": [ | ||
"test" | ||
], | ||
"devel": [ | ||
"devel" | ||
], | ||
"stage": [ | ||
"stage" | ||
], | ||
"production": [ | ||
"production" | ||
] | ||
}, | ||
"keys": [ | ||
"test", | ||
"devel", | ||
"stage", | ||
"production" | ||
], | ||
"test": false, | ||
"devel": true, | ||
"stage": false, | ||
"production": false | ||
} | ||
``` | ||
@@ -46,0 +147,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10187
7
150
173