Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default (key: string, separator: string = ',') => { | ||
const rawValue = process.env[key] | ||
|
||
if (!rawValue) { | ||
return undefined | ||
} | ||
|
||
const numberArray = rawValue.split(separator).map((s) => Number(s)) | ||
|
||
if (numberArray.some((n) => isNaN(n))) { | ||
return undefined | ||
} | ||
|
||
return numberArray | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import numberArray from './numberArray' | ||
import orThrow from './orThrow' | ||
|
||
export default (key: string, separator?: string) => | ||
orThrow((k) => numberArray(k, separator), 'number array')(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default (key: string, separator: string = ',') => { | ||
const rawValue = process.env[key] | ||
|
||
if (!rawValue) { | ||
return undefined | ||
} | ||
|
||
return rawValue.split(separator) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import stringArray from './stringArray' | ||
import orThrow from './orThrow' | ||
|
||
export default (key: string, separator?: string) => | ||
orThrow((k) => stringArray(k, separator), 'string array')(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numberArray from '../src/numberArray' | ||
|
||
describe('numberArray()', () => { | ||
const key = 'foo' | ||
const originalEnv = process.env | ||
|
||
beforeEach(() => { | ||
process.env = { | ||
...originalEnv, | ||
} | ||
}) | ||
|
||
it('reads a comma separated array', () => { | ||
process.env[key] = '0,1,2,-1.5' | ||
expect(numberArray(key)).toEqual([0, 1, 2, -1.5]) | ||
}) | ||
|
||
it('reads a space separated array', () => { | ||
const separator = ' ' | ||
process.env[key] = '0 1 2 -1.5' | ||
expect(numberArray(key, separator)).toEqual([0, 1, 2, -1.5]) | ||
}) | ||
|
||
it('returns undefined if any item is not a number', () => { | ||
process.env[key] = '0,1,2,foo' | ||
expect(numberArray(key)).toBeUndefined() | ||
}) | ||
|
||
it('returns undefined if a key is not defined', () => { | ||
expect(numberArray(key)).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as numberArrayModule from '../src/numberArray' | ||
import * as orThrowModule from '../src/orThrow' | ||
import numberArrayOrThrow from '../src/numberArrayOrThrow' | ||
|
||
jest.spyOn(numberArrayModule, 'default') | ||
jest.spyOn(orThrowModule, 'default') | ||
|
||
describe('.numberArrayOrThrow()', () => { | ||
it('does something', () => { | ||
process.env.foo = '0,1,2' | ||
numberArrayOrThrow('foo') | ||
expect(orThrowModule.default).toHaveBeenCalled() | ||
expect(numberArrayModule.default).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import stringArray from '../src/stringArray' | ||
|
||
describe('stringArray()', () => { | ||
const key = 'foo' | ||
const originalEnv = process.env | ||
|
||
beforeEach(() => { | ||
process.env = { | ||
...originalEnv, | ||
} | ||
}) | ||
|
||
it('reads a comma separated array', () => { | ||
process.env[key] = '0,1,2,foo bar' | ||
expect(stringArray(key)).toEqual(['0', '1', '2', 'foo bar']) | ||
}) | ||
|
||
it('reads a space separated array', () => { | ||
const separator = ' ' | ||
process.env[key] = '0 1 2 foo bar' | ||
expect(stringArray(key, separator)).toEqual(['0', '1', '2', 'foo', 'bar']) | ||
}) | ||
|
||
it('returns undefined if a key is not defined', () => { | ||
expect(stringArray(key)).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as stringArrayModule from '../src/stringArray' | ||
import * as orThrowModule from '../src/orThrow' | ||
import stringArrayOrThrow from '../src/stringArrayOrThrow' | ||
|
||
jest.spyOn(stringArrayModule, 'default') | ||
jest.spyOn(orThrowModule, 'default') | ||
|
||
describe('.stringArrayOrThrow()', () => { | ||
it('does something', () => { | ||
process.env.foo = '0,1,2,foo bar' | ||
stringArrayOrThrow('foo') | ||
expect(orThrowModule.default).toHaveBeenCalled() | ||
expect(stringArrayModule.default).toHaveBeenCalled() | ||
}) | ||
}) |