Skip to content

Commit

Permalink
Merge pull request #2 from hexlabsio/bug/transforms-with-defaults
Browse files Browse the repository at this point in the history
Only apply transform when value is given or select default.
  • Loading branch information
chrisbarbour authored Dec 21, 2023
2 parents 9ac8462 + 91b3a37 commit ecd882d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
9 changes: 9 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { JestConfigWithTsJest } from 'ts-jest';
import { defaultsESM as tsjPreset } from 'ts-jest/presets';


const config: JestConfigWithTsJest = {
...tsjPreset,
}

export default config;
19 changes: 1 addition & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "rollup -c rollup.config.ts --configPlugin @rollup/plugin-typescript",
"prebuild": "npm run lint",
"lint": "eslint src --ext .ts",
"test": "npx jest --runInBand --detectOpenHandles --colors --verbose --reporters=default --coverage",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --no-cache --ci --runInBand --collectCoverage --restoreMocks",
"prepare": "husky install"
},
"keywords": ["environment", "variables", "env", "envs"],
Expand Down Expand Up @@ -49,22 +49,5 @@
"@typescript-eslint/ban-types": 0,
"@typescript-eslint/no-explicit-any": 0
}
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.json"
}
},
"coverageThreshold": {
"global": {
"branches": 75,
"functions": 90,
"lines": 90,
"statements": 90
}
}
}
}
8 changes: 5 additions & 3 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export class EnvironmentBuilder<E = unknown, O = unknown> {

private requiredEnvs(environment: any): { errors: string[], requiredEnvs: any } {
return this.info.requiredKeys.reduce((result, key) => {
const envValue = environment[key] ?? (this.info.defaultValues as any)[key];
const value = environment[key];
const hasValue = value !== undefined;
const envValue = hasValue ? environment[key] : (this.info.defaultValues as any)[key];
if (envValue !== undefined) {
const transformed = this.info.transforms[key] ? this.info.transforms[key](envValue) : envValue;
const transformed = (this.info.transforms[key] && hasValue) ? this.info.transforms[key](envValue) : envValue;
return {errors: result.errors, requiredEnvs: {...result.requiredEnvs, [key]: transformed}};
}
return {errors: [...result.errors, key], requiredEnvs: {...result.requiredEnvs, [key]: envValue}};
Expand All @@ -57,4 +59,4 @@ export class EnvironmentBuilder<E = unknown, O = unknown> {
return ({...result, [key]: transformed });
}, {});
}
}
}
15 changes: 10 additions & 5 deletions src/environment.spec.ts → test/environment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { EnvironmentBuilder } from './environment';


import { EnvironmentBuilder } from '../src/environment';

describe('Environment', () => {

Expand Down Expand Up @@ -52,6 +50,13 @@ describe('Environment', () => {
});

});
});


it('should allow setting of transformed type as default', () => {
const builder = EnvironmentBuilder.create('a').optionals('c', 'd')
.transform(s => s === 'true', 'a')
.defaults({ a: true })
expect(builder.environment({ d: '123'})).toEqual({a: true, d: '123'});
expect(builder.environment({ d: '123', a: 'xyz'})).toEqual({a: false, d: '123'});
expect(builder.environment({ d: '123', a: 'true'})).toEqual({a: true, d: '123'});
});
})

0 comments on commit ecd882d

Please sign in to comment.