-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
86 additions
and
20 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
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,39 @@ | ||
import { getEnvVar } from "./env"; | ||
|
||
describe("env", () => { | ||
describe("with null/undefined key", () => { | ||
it("should return undefined", async () => { | ||
expect(getEnvVar(undefined)).toBeUndefined(); | ||
expect(getEnvVar(null)).toBeUndefined(); | ||
}); | ||
}); | ||
describe("with missing value", () => { | ||
it("should return default value", async () => { | ||
const val1 = getEnvVar("teseting123"); | ||
expect(val1).toBeUndefined(); | ||
const val2 = getEnvVar("testing123", "default"); | ||
expect(val2).toBe("default"); | ||
}); | ||
}); | ||
describe("with value", () => { | ||
it("should return it", async () => { | ||
process.env["valueset"] = "123"; | ||
const val = getEnvVar("valueset"); | ||
expect(val).toBe("123"); | ||
}); | ||
}); | ||
describe("with dash in name", () => { | ||
it("should check underscore variants", async () => { | ||
process.env["testing_123_456"] = "value"; | ||
const val = getEnvVar("testing-123-456"); | ||
expect(val).toBe("value"); | ||
}); | ||
}); | ||
describe("with underscore in name", () => { | ||
it("should check dash variants", async () => { | ||
process.env["testing-a1-b2"] = "value"; | ||
const val = getEnvVar("testing_a1_b2"); | ||
expect(val).toBe("value"); | ||
}); | ||
}); | ||
}); |
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,21 @@ | ||
/* | ||
in some environments, env vars with dashes aren't supported, or are converted to snake_case, | ||
so check all variants of a key when attempting to retrieve it | ||
*/ | ||
export function getEnvVar(key: string, defaultValue: any = undefined): any { | ||
if (!key) { | ||
return defaultValue; | ||
} | ||
const variants = [ | ||
key, | ||
key.replace(/_/g, '-'), | ||
key.replace(/-/g, '_')]; | ||
let value = defaultValue; | ||
for (const variant of variants) { | ||
if (process.env[variant]) { | ||
value = process.env[variant]; | ||
break; | ||
} | ||
} | ||
return value; | ||
} |
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