Skip to content

Commit e293285

Browse files
authored
Add asSet (#173)
* add asSet accessor
1 parent d900b9f commit e293285

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

env-var.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ type PublicAccessors = {
7373
*/
7474
asArray: (input: string, delimiter?: string) => Array<string>;
7575

76+
/**
77+
* Reads an environment variable as a string, then splits it on each occurrence of the specified delimiter.
78+
* By default a comma is used as the delimiter. For example a var set to "1,2,3" would become new Set(['1', '2', '3']).
79+
*/
80+
asSet: (input: string, delimiter?: string) => Set<string>;
81+
7682
/**
7783
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
7884
* The var must be set to either "true", "false" (upper or lowercase), 0 or 1 to succeed.
@@ -178,6 +184,12 @@ interface VariableAccessors <AlternateType = unknown> {
178184
*/
179185
asArray: (delimiter?: string) => AlternateType extends undefined ? undefined|Array<string> : Array<string>;
180186

187+
/**
188+
* Reads an environment variable as a string, then splits it on each occurrence of the specified delimiter.
189+
* By default a comma is used as the delimiter. For example a var set to "1,2,3" would become new Set(['1', '2', '3']).
190+
*/
191+
asSet: (delimiter?: string) => AlternateType extends undefined ? undefined|Set<string> : Set<string>;
192+
181193
/**
182194
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
183195
* The var must be set to either "true", "false" (upper or lowercase), 0 or 1 to succeed.

lib/accessors/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
asArray: require('./array'),
3+
asSet: require('./set'),
34

45
asBoolStrict: require('./bool-strict'),
56
asBool: require('./bool'),

lib/accessors/set.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const asArray = require('./array')
4+
5+
module.exports = function asSet (value, delimiter) {
6+
if (!value.length) {
7+
return new Set()
8+
} else {
9+
return new Set(asArray(value, delimiter))
10+
}
11+
}

test/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('env-var', function () {
2020
JSON_ARRAY: '[1,2,3]',
2121
COMMA_ARRAY: '1,2,3',
2222
EMPTY_ARRAY: '',
23+
DUPLICATE_ARRAY: '1,1,2,2,3,3',
2324
ARRAY_WITHOUT_DELIMITER: 'value',
2425
ARRAY_WITH_DELIMITER: 'value,',
2526
ARRAY_WITH_DELIMITER_PREFIX: ',value',
@@ -539,6 +540,40 @@ describe('env-var', function () {
539540
})
540541
})
541542

543+
describe('#asSet', function () {
544+
it('should return an empty set when not set', function () {
545+
expect(mod.get('.NOPE.').asSet()).to.deep.equal(undefined)
546+
})
547+
548+
it('should return a set that was split on commas', function () {
549+
expect(mod.get('COMMA_ARRAY').asSet()).to.deep.equal(new Set(['1', '2', '3']))
550+
})
551+
552+
it('should return a set that was split on dashes', function () {
553+
expect(mod.get('DASH_ARRAY').asSet('-')).to.deep.equal(new Set(['1', '2', '3']))
554+
})
555+
556+
it('should return an empty set if empty env var was set', function () {
557+
expect(mod.get('EMPTY_ARRAY').asSet()).to.deep.equal(new Set())
558+
})
559+
560+
it('should return set with only one value if env var doesn\'t contain delimiter', function () {
561+
expect(mod.get('ARRAY_WITHOUT_DELIMITER').asSet()).to.deep.equal(new Set(['value']))
562+
})
563+
564+
it('should return set with only one value if env var contain delimiter', function () {
565+
expect(mod.get('ARRAY_WITH_DELIMITER').asSet()).to.deep.equal(new Set(['value']))
566+
})
567+
568+
it('should return set with only one value if env var contain delimiter as prefix', function () {
569+
expect(mod.get('ARRAY_WITH_DELIMITER_PREFIX').asSet()).to.deep.equal(new Set(['value']))
570+
})
571+
572+
it('should return a set of unique values', function () {
573+
expect(mod.get('DUPLICATE_ARRAY').asSet()).to.deep.equal(new Set(['1', '2', '3']))
574+
})
575+
})
576+
542577
describe('#asPortNumber', function () {
543578
it('should raise an error for ports less than 0', function () {
544579
process.env.PORT_NUMBER = '-2'

test/types/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ describe('typescript tests', () => {
196196
})
197197
})
198198

199+
describe('#asSet', () => {
200+
it('should return a Set of strings', () => {
201+
const set = env.accessors.asSet('1,2,3');
202+
203+
expect(set).to.eql(new Set(['1', '2', '3']));
204+
});
205+
206+
it('should return a Set of strings split by period chars', () => {
207+
const set = env.accessors.asSet('1.2.3', '.');
208+
209+
expect(set).to.eql(new Set(['1', '2', '3']));
210+
});
211+
});
212+
199213
describe('#asInt', () => {
200214
it('should return an integer', () => {
201215
const ret = env.accessors.asInt('1')

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"sourceMap": false,
4+
"lib": ["es2015"],
45
"target": "es5",
56
"moduleResolution": "node",
67
"skipLibCheck": false,

0 commit comments

Comments
 (0)