Skip to content

Commit

Permalink
Merge pull request #4 from ukitgroup/empty-string
Browse files Browse the repository at this point in the history
Empty string value fix
  • Loading branch information
Goodluckhf authored Jul 13, 2020
2 parents aac26ec + aa847cf commit c51cf3b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ class CatConfig {

We use our own version of [class-transformer](https://github.com/typestack/class-transformer): [@ukitgroup/class-transformer](https://github.com/ukitgroup/class-transformer)

If environment variable is empty or not provided, config will use default value.

## Validation

```text
Expand Down
4 changes: 2 additions & 2 deletions e2e-test/file.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('File', () => {
FIRST__NUMBER_VAR=1.1
FIRST__NUMBER_VAR_WITH_DEFAULT=7.7
FIRST__BOOL_VAR=true
FIRST__BOOL_VAR_WITH_DEFAULT=true
FIRST__BOOL_VAR_WITH_DEFAULT=
SECOND__VARIABLE=new`,
);

Expand Down Expand Up @@ -58,7 +58,7 @@ describe('File', () => {
numberVar: 1.1,
numberVarWithDefault: 7.7,
boolVar: true,
boolVarWithDefault: true,
boolVarWithDefault: false,
});
expect(secondConfig).toMatchObject({
variable: 'new',
Expand Down
16 changes: 16 additions & 0 deletions src/lib/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ describe('ConfigParser', () => {
},
});
});

it('should return empty string value as undefined', () => {
const moduleName = 'SOME_MODULE';
const variableName = 'SOME_VARIABLE';
const value = '';

expect(
configParser.parse({
[`${moduleName}${ENV_MODULE_SEPARATOR}${variableName}`]: value,
}),
).toMatchObject({
[moduleName]: {
[variableName]: undefined,
},
});
});
});
3 changes: 2 additions & 1 deletion src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class ConfigParser {
}
const moduleName = split[0];
configStorage[moduleName] = configStorage[moduleName] || {};
configStorage[moduleName][split[1]] = value;
// interpret empty string as undefined
configStorage[moduleName][split[1]] = value || undefined;
},
);
return configStorage;
Expand Down

0 comments on commit c51cf3b

Please sign in to comment.