Skip to content

Commit

Permalink
update to ensure arrays are not empty. handle numeric typings better …
Browse files Browse the repository at this point in the history
…for defaults (#16)
  • Loading branch information
evanshortiss authored Dec 12, 2017
1 parent 7bfa68b commit 8d9ec83
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1.0 (11/12/17)
* Update typings to correctly handle default values for numeric types.
* Ensure an error is thrown when `asArray` does not detect at least a single non-empty value.

## 3.0.2 (19/10/17)
* Restore support for use in browser based applications

Expand Down
12 changes: 6 additions & 6 deletions env-var.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ interface IPresentVariable {
/**
* Attempt to parse the variable to a float. Throws an exception if parsing fails.
*/
asFloat: () => number|undefined;
asFloat: () => number;

/**
* Performs the same task as asFloat(), but also verifies that the number is positive (greater than zero).
*/
asFloatPositive: () => number|undefined;
asFloatPositive: () => number;

/**
* Performs the same task as asFloat(), but also verifies that the number is negative (less than zero).
*/
asFloatNegative: () => number|undefined;
asFloatNegative: () => number;

/**
* Attempt to parse the variable to an integer. Throws an exception if parsing fails.
* This is a strict check, meaning that if the process.env value is 1.2, an exception will be raised rather than rounding up/down.
*/
asInt: () => number|undefined;
asInt: () => number;

/**
* Performs the same task as asInt(), but also verifies that the number is positive (greater than zero).
*/
asIntPositive: () => number|undefined;
asIntPositive: () => number;

/**
* Performs the same task as asInt(), but also verifies that the number is negative (less than zero).
*/
asIntNegative: () => number|undefined;
asIntNegative: () => number;

/**
* Return the variable value as a String. Throws an exception if value is not a String.
Expand Down
4 changes: 4 additions & 0 deletions lib/accessors/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ const asString = require('./string')
module.exports = function asArray (raiseError, value, delimeter) {
delimeter = delimeter || ','

if (!value.includes(delimeter)) {
raiseError(`should include values separated with the delimeter "${delimeter}"`)
}

return asString(raiseError, value).split(delimeter)
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "env-var",
"version": "3.0.2",
"version": "3.1.0",
"description": "Solution for loading and sanatizing environment variables in node.js with correct typings",
"main": "env-var.js",
"typings": "env-var.d.ts",
Expand Down Expand Up @@ -55,7 +55,6 @@
},
"dependencies": {
"@types/node": "~8.0.31",
"camelcase": "~4.1.0",
"is-url": "~1.2.2"
},
"engines": {
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ describe('env-var', function () {
expect(mod.get('.NOPE.').asArray()).to.equal(undefined)
})

it('should raise an error if set incorrectly', function () {
process.env.COMMA_ARRAY = ''

expect(() => {
mod.get('COMMA_ARRAY').asArray()
}).to.throw('should include values separated with the delimeter ","')
})

it('should return an array that was split on commas', function () {
expect(mod.get('COMMA_ARRAY').asArray()).to.deep.equal(['1', '2', '3'])
})
Expand Down

0 comments on commit 8d9ec83

Please sign in to comment.