Skip to content

Commit

Permalink
fix(loadEnv): support compounded namespace names
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Nov 20, 2020
1 parent a15db00 commit 4b2f55a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/loaders/loadEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { loadFromEnv } from './loadEnv'
const TEST_CASES: [string, NodeJS.ProcessEnv, JsonObject][] = [
[
'map name to camelcase without namespace',
{ PASTA_FOO: 'bar' },
{ MY_APP_FOO: 'bar' },
{ foo: 'bar' }
],
[
'parse an array value',
{ PASTA_FOO: '["bar", 1337]' },
{ MY_APP_FOO: '["bar", 1337]' },
{ foo: ['bar', 1337] }
],
['trim the value', { PASTA_FOO: ' 1337 ' }, { foo: '1337' }],
['trim the value', { MY_APP_FOO: ' 1337 ' }, { foo: '1337' }],
['remove variables without namespace', { FOO: '1337' }, {}]
]

test.each(TEST_CASES)('%s', (_title, env, expected) => {
expect(loadFromEnv('pasta', env)).toEqual(expected)
expect(loadFromEnv('my-app', env)).toEqual(expected)
})
4 changes: 2 additions & 2 deletions src/loaders/loadEnv.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { camelCase, chain, replace, trim, upperCase } from 'lodash'
import { camelCase, chain, replace, snakeCase, trim } from 'lodash'
import { JsonObject } from 'type-fest'
import { parseArray } from './utils'

export function loadFromEnv(
namespace: string,
env: NodeJS.ProcessEnv
): JsonObject {
const prefix = `${upperCase(namespace)}_`
const prefix = `${snakeCase(namespace).toUpperCase()}_`
return chain(env)
.pickBy((_v, k) => k.startsWith(prefix))
.mapKeys((_v, k) => replace(k, prefix, ''))
Expand Down

0 comments on commit 4b2f55a

Please # to comment.