Skip to content

Commit

Permalink
fix(parser): ensure identifier fully-match before using it
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Jan 19, 2024
1 parent 0e612ae commit 821ef92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class EnvParser {

if (value.includes(':')) {
for (const identifier of identifiers) {
if (value.startsWith(identifier)) {
if (value.startsWith(`${identifier}:`)) {
result[key] = await EnvParser.#identifiers[identifier](
value.substring(identifier.length + 1)
)
Expand Down
23 changes: 23 additions & 0 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ test.group('Env Parser', () => {
})
})

test('identifier is used only when complete value is matched', async ({
assert,
cleanup,
expectTypeOf,
}) => {
cleanup(() => {
EnvParser.removeIdentifier('file')
})

EnvParser.identifier('file', (_value: string) => {
return '3000'
})

const envString = ['ENV_USER=file_romain:romain'].join('\n')
const parser = new EnvParser(envString)
const parsed = await parser.parse()

expectTypeOf(parsed).toEqualTypeOf<DotenvParseOutput>()
assert.deepEqual(parsed, {
ENV_USER: 'file_romain:romain',
})
})

test('throw when identifier is already defined', async ({ assert, cleanup }) => {
cleanup(() => {
EnvParser.removeIdentifier('file')
Expand Down

0 comments on commit 821ef92

Please sign in to comment.