Skip to content

Commit

Permalink
fix(normalizer): keep a number primitive as is
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Nov 29, 2020
1 parent 1729d9f commit c49750d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/normalizer/expandVars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const TEST_CASES: [string, NodeJS.ProcessEnv, JsonObject, JsonObject][] = [
{ foo: '\\${BAR}' },
{ foo: '${BAR}' }
],
['do not crash on other primitives', {}, { foo: 1337 }, { foo: '1337' }]
['keep a number as is', {}, { foo: 1337 }, { foo: 1337 }]
]

test.each(TEST_CASES)('%s', (_title, env, config, expected) => {
Expand Down
10 changes: 6 additions & 4 deletions src/normalizer/expandVars.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { isArray, isObject, map, mapValues } from 'lodash'
import { isArray, isObject, isString, map, mapValues } from 'lodash'
import { JsonValue } from 'type-fest'

const varRegex = /([\\])?\${([\w]+)}/g

function expandVar(value: JsonValue, env: NodeJS.ProcessEnv): JsonValue {
return String(value).replace(varRegex, (_s, escaped, name) =>
!escaped ? env[name] ?? '' : `\${${name}}`
)
return isString(value)
? value.replace(varRegex, (_s, escaped, name) =>
!escaped ? env[name] ?? '' : `\${${name}}`
)
: value
}

export function expandVars<T>(value: T, env: NodeJS.ProcessEnv): T {
Expand Down

0 comments on commit c49750d

Please # to comment.