Skip to content

Commit

Permalink
add as set
Browse files Browse the repository at this point in the history
  • Loading branch information
noamokman committed May 17, 2024
1 parent d900b9f commit f725ca9
Show file tree
Hide file tree
Showing 7 changed files with 3,799 additions and 109 deletions.
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))
}
}
30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,36 @@ describe('env-var', function () {
})
})

describe('#asSet', function () {
it('should return an empty set when not set', function () {
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']))
})
})

describe('#asPortNumber', function () {
it('should raise an error for ports less than 0', function () {
process.env.PORT_NUMBER = '-2'
Expand Down
Loading

0 comments on commit f725ca9

Please sign in to comment.