-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to trim a prefix from env variables (#6)
- Loading branch information
Showing
4 changed files
with
62 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,6 @@ | ||
export function envTrimPrefix(prefix: string): void { | ||
Object.keys(process.env) | ||
.filter(n => n.startsWith(prefix)) | ||
.map(n => n.substr(prefix.length)) | ||
.forEach(n => process.env[n] = process.env[prefix + n]); | ||
} |
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,43 @@ | ||
import { envTrimPrefix } from '../../src/utils/env-trim-prefix'; | ||
|
||
describe('util / envTrimPrefix', () => { | ||
describe('envTrimPrefix()', () => { | ||
beforeAll(() => { | ||
process.env['STA_FOO'] = 'hello foo'; | ||
process.env['BAR'] = 'hello bar'; | ||
}); | ||
|
||
afterAll(() => { | ||
delete process.env.STA_FOO; | ||
delete process.env.FOO; | ||
delete process.env.BAR; | ||
}); | ||
|
||
it('should trim prefix from env vars', () => { | ||
envTrimPrefix('STA_'); | ||
|
||
expect(process.env.STA_FOO).toBe('hello foo'); | ||
expect(process.env.FOO).toBe('hello foo'); | ||
expect(process.env.BAR).toBe('hello bar'); | ||
}); | ||
|
||
it('should trim prefix from env vars and override existing vars', () => { | ||
process.env.FOO = 'hello existing foo'; | ||
|
||
envTrimPrefix('STA_'); | ||
|
||
expect(process.env.STA_FOO).toBe('hello foo'); | ||
expect(process.env.FOO).toBe('hello foo'); | ||
expect(process.env.BAR).toBe('hello bar'); | ||
}); | ||
|
||
it('should should not touch env vars with a nonsense prefix', () => { | ||
envTrimPrefix('STAXX_'); | ||
|
||
expect(process.env.STA_FOO).toBe('hello foo'); | ||
expect(process.env.FOO).toBe('hello foo'); | ||
expect(process.env.BAR).toBe('hello bar'); | ||
}); | ||
|
||
}); | ||
}); |