Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asSet #173

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions env-var.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ type PublicAccessors = {
*/
asArray: (input: string, delimiter?: string) => Array<string>;

/**
* Reads an environment variable as a string, then splits it on each occurrence of the specified delimiter.
* 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']).
*/
asSet: (input: string, delimiter?: string) => Set<string>;

/**
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
* The var must be set to either "true", "false" (upper or lowercase), 0 or 1 to succeed.
Expand Down Expand Up @@ -178,6 +184,12 @@ interface VariableAccessors <AlternateType = unknown> {
*/
asArray: (delimiter?: string) => AlternateType extends undefined ? undefined|Array<string> : Array<string>;

/**
* Reads an environment variable as a string, then splits it on each occurrence of the specified delimiter.
* 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']).
*/
asSet: (delimiter?: string) => AlternateType extends undefined ? undefined|Set<string> : Set<string>;

/**
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
* The var must be set to either "true", "false" (upper or lowercase), 0 or 1 to succeed.
Expand Down
1 change: 1 addition & 0 deletions lib/accessors/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
asArray: require('./array'),
asSet: require('./set'),

asBoolStrict: require('./bool-strict'),
asBool: require('./bool'),
Expand Down
11 changes: 11 additions & 0 deletions lib/accessors/set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const asArray = require('./array')

module.exports = function asSet (value, delimiter) {
if (!value.length) {
return new Set()
} else {
return new Set(asArray(value, delimiter))
}
}
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('env-var', function () {
JSON_ARRAY: '[1,2,3]',
COMMA_ARRAY: '1,2,3',
EMPTY_ARRAY: '',
DUPLICATE_ARRAY: '1,1,2,2,3,3',
ARRAY_WITHOUT_DELIMITER: 'value',
ARRAY_WITH_DELIMITER: 'value,',
ARRAY_WITH_DELIMITER_PREFIX: ',value',
Expand Down Expand Up @@ -539,6 +540,40 @@ describe('env-var', function () {
})
})

describe('#asSet', function () {
it('should return an empty set when not set', function () {
evanshortiss marked this conversation as resolved.
Show resolved Hide resolved
expect(mod.get('.NOPE.').asSet()).to.deep.equal(undefined)
})

it('should return a set that was split on commas', function () {
expect(mod.get('COMMA_ARRAY').asSet()).to.deep.equal(new Set(['1', '2', '3']))
})

it('should return a set that was split on dashes', function () {
expect(mod.get('DASH_ARRAY').asSet('-')).to.deep.equal(new Set(['1', '2', '3']))
})

it('should return an empty set if empty env var was set', function () {
expect(mod.get('EMPTY_ARRAY').asSet()).to.deep.equal(new Set())
})

it('should return set with only one value if env var doesn\'t contain delimiter', function () {
expect(mod.get('ARRAY_WITHOUT_DELIMITER').asSet()).to.deep.equal(new Set(['value']))
})

it('should return set with only one value if env var contain delimiter', function () {
expect(mod.get('ARRAY_WITH_DELIMITER').asSet()).to.deep.equal(new Set(['value']))
})

it('should return set with only one value if env var contain delimiter as prefix', function () {
expect(mod.get('ARRAY_WITH_DELIMITER_PREFIX').asSet()).to.deep.equal(new Set(['value']))
})

it('should return a set of unique values', function () {
expect(mod.get('DUPLICATE_ARRAY').asSet()).to.deep.equal(new Set(['1', '2', '3']))
})
})

describe('#asPortNumber', function () {
it('should raise an error for ports less than 0', function () {
process.env.PORT_NUMBER = '-2'
Expand Down
14 changes: 14 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ describe('typescript tests', () => {
})
})

describe('#asSet', () => {
it('should return a Set of strings', () => {
const set = env.accessors.asSet('1,2,3');

expect(set).to.eql(new Set(['1', '2', '3']));
});

it('should return a Set of strings split by period chars', () => {
const set = env.accessors.asSet('1.2.3', '.');

expect(set).to.eql(new Set(['1', '2', '3']));
});
});

describe('#asInt', () => {
it('should return an integer', () => {
const ret = env.accessors.asInt('1')
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"sourceMap": false,
"lib": ["es2015"],
"target": "es5",
"moduleResolution": "node",
"skipLibCheck": false,
Expand Down
Loading