Comparing version 2.3.1 to 2.4.0
@@ -0,1 +1,9 @@ | ||
## 2.4.0 (15 December 2016) | ||
* Add `asArray([delimeter])` to read environment variables as an array by splitting | ||
the varible string on each instance of _delimeter_; | ||
* Add `asJsonArray()` to read in an environment variable that contains a JSON | ||
Array. Similar to `asJson()`, but ensures the variable is an Array. | ||
* Add `asJsonObject()` to read in an environment variable that contains a JSON | ||
Object. Similar to `asJson()`, but ensures the variable is an Object. | ||
## 2.3.0 & 2.3.1 (12 December 2016) | ||
@@ -2,0 +10,0 @@ * Add typings support for TypeScript |
@@ -103,4 +103,34 @@ 'use strict'; | ||
} catch (e) { | ||
throw new VError('env-var: failed to parse "%s" to a JSON Object'); | ||
throw new VError('env-var: failed to parse "%s" to JSON', name); | ||
} | ||
}, | ||
asJsonArray: function () { | ||
var ret = variable.asJson(); | ||
if (!Array.isArray(ret)) { | ||
throw new VError('env-var: value for "%s was not a JSON Array', name); | ||
} | ||
return ret; | ||
}, | ||
asJsonObject: function () { | ||
var ret = variable.asJson(); | ||
if (Array.isArray(ret)) { | ||
throw new VError( | ||
'env-var: value for "%s was a JSON Array, but we expected Object', | ||
name | ||
); | ||
} | ||
return ret; | ||
}, | ||
asArray: function (delimeter) { | ||
delimeter = delimeter || ','; | ||
if (value) { | ||
return value.split(delimeter); | ||
} else { | ||
return []; | ||
} | ||
} | ||
@@ -117,3 +147,6 @@ }; | ||
'asBool', | ||
'asJson' | ||
'asJson', | ||
'asJsonArray', | ||
'asJsonObject', | ||
'asArray', | ||
].forEach(function allowUndefined (fnname) { | ||
@@ -120,0 +153,0 @@ // Original function |
{ | ||
"name": "env-var", | ||
"version": "2.3.1", | ||
"description": "programmatic wrapper for process.env with support for defaults", | ||
"version": "2.4.0", | ||
"description": "solution for loading and sanatizing environment variables in node.js", | ||
"main": "lib/index.js", | ||
@@ -28,3 +28,6 @@ "scripts": { | ||
"license": "MIT", | ||
"files": ["lib/"], | ||
"files": [ | ||
"lib/", | ||
"env-var.d.ts" | ||
], | ||
"bugs": { | ||
@@ -31,0 +34,0 @@ "url": "https://github.com/evanshortiss/env-var/issues" |
# env-var | ||
[![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://github.com/ellerbrock/typescript-badges/) | ||
[![Travis CI](https://travis-ci.org/evanshortiss/env-var.svg?branch=master)](https://travis-ci.org/evanshortiss/env-var) | ||
An elegant solution for loading environment variables in node.js. | ||
solution for loading and sanatizing environment variables in node.js | ||
@@ -16,2 +17,3 @@ ## Install | ||
it is set (required), and parse it to an integer. | ||
```js | ||
@@ -123,6 +125,17 @@ var PARALLEL_LIMIT = env('PARALLEL_LIMIT').required().asPositiveInt(); | ||
#### asJson() | ||
Attempt to parse the variable to a JSON Object. Throws an exception if parsing | ||
fails. | ||
Attempt to parse the variable to a JSON Object or Array. Throws an exception if | ||
parsing fails. | ||
#### asJsonArray() | ||
The same as _asJson_ but checks that the data is a JSON Array, e.g [1,2]. | ||
#### asJsonObject() | ||
The same as _asJson_ but checks that the data is a JSON Object, e.g {a: 1}. | ||
#### asArray([delimiter]) | ||
Reads an environment variable as a string, then splits it on each occurence of | ||
the specified _delimiter_. By default a comma is used as the delimiter. For | ||
example a var set to "1,2,3" would become ['1', '2', '3']. | ||
## Example | ||
@@ -138,2 +151,4 @@ | ||
process.env.JSON = '{"key":"value"}'; | ||
process.env.COMMA_ARRAY = '1,2,3'; | ||
process.env.DASH_ARRAY = '1-2-3'; | ||
@@ -157,2 +172,8 @@ // The entire process.env object | ||
var jsonVar = env('JSON').asJson(); | ||
// Returns an array if defined, or undefined if not set | ||
var commaArray = env('COMMA_ARRAY').asArray(); | ||
// Returns an array if defined, or undefined if not set | ||
var commaArray = env('DASH_ARRAY').asArray('-'); | ||
``` | ||
@@ -211,3 +232,3 @@ | ||
describe('#concat', function () { | ||
it('should combine our var name and returned value', function () { | ||
it('should combine our var name and its returned value', function () { | ||
expect(mod.concat('HELLO')).to.equal('HELLO WORLD'); | ||
@@ -220,4 +241,12 @@ }); | ||
## TypeScript | ||
To use with TypeScript, just import it like this: | ||
```ts | ||
import env from 'get-env'; | ||
const stringVar = env('STRING').required().asString(); | ||
``` | ||
## Contributors | ||
* @MikeyBurkman | ||
* @itavy |
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
15715
6
213
247