Skip to content

Commit 8d9ec83

Browse files
authored
update to ensure arrays are not empty. handle numeric typings better for defaults (#16)
1 parent 7bfa68b commit 8d9ec83

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.1.0 (11/12/17)
2+
* Update typings to correctly handle default values for numeric types.
3+
* Ensure an error is thrown when `asArray` does not detect at least a single non-empty value.
4+
15
## 3.0.2 (19/10/17)
26
* Restore support for use in browser based applications
37

env-var.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ interface IPresentVariable {
77
/**
88
* Attempt to parse the variable to a float. Throws an exception if parsing fails.
99
*/
10-
asFloat: () => number|undefined;
10+
asFloat: () => number;
1111

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

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

2222
/**
2323
* Attempt to parse the variable to an integer. Throws an exception if parsing fails.
2424
* 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.
2525
*/
26-
asInt: () => number|undefined;
26+
asInt: () => number;
2727

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

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

3838
/**
3939
* Return the variable value as a String. Throws an exception if value is not a String.

lib/accessors/array.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ const asString = require('./string')
55
module.exports = function asArray (raiseError, value, delimeter) {
66
delimeter = delimeter || ','
77

8+
if (!value.includes(delimeter)) {
9+
raiseError(`should include values separated with the delimeter "${delimeter}"`)
10+
}
11+
812
return asString(raiseError, value).split(delimeter)
913
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "env-var",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "Solution for loading and sanatizing environment variables in node.js with correct typings",
55
"main": "env-var.js",
66
"typings": "env-var.d.ts",
@@ -55,7 +55,6 @@
5555
},
5656
"dependencies": {
5757
"@types/node": "~8.0.31",
58-
"camelcase": "~4.1.0",
5958
"is-url": "~1.2.2"
6059
},
6160
"engines": {

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ describe('env-var', function () {
384384
expect(mod.get('.NOPE.').asArray()).to.equal(undefined)
385385
})
386386

387+
it('should raise an error if set incorrectly', function () {
388+
process.env.COMMA_ARRAY = ''
389+
390+
expect(() => {
391+
mod.get('COMMA_ARRAY').asArray()
392+
}).to.throw('should include values separated with the delimeter ","')
393+
})
394+
387395
it('should return an array that was split on commas', function () {
388396
expect(mod.get('COMMA_ARRAY').asArray()).to.deep.equal(['1', '2', '3'])
389397
})

0 commit comments

Comments
 (0)