Skip to content

Commit

Permalink
refactor: Remove unused feature flag
Browse files Browse the repository at this point in the history
Add (ugly) solution to verify matching of enum name and value
  • Loading branch information
denis-kralj-novu committed May 3, 2024
1 parent 8d55eaf commit 97236a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions libs/shared/src/types/feature-flags/flags.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ testFlagEnumValidity(ValidFlagsEnum);

enum InvalidKeyFlagsEnum {
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED',
INVALID_ENABLED = 'IS_INVALID_ENABLED',
INVALID_ENABLED = 'INVALID_ENABLED',
}
// @ts-expect-error - Invalid key - INVALID_ENABLED
testFlagEnumValidity(InvalidKeyFlagsEnum);
enum InvalidValueFlagsEnum {
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED',
IS_INVALID_ENABLED = 'INVALID_ENABLED',
INVALID_ENABLED = 'INVALID_ENABLED',
}
// @ts-expect-error - Invalid value on IS_INVALID_ENABLED: 'INVALID_ENABLED'
testFlagEnumValidity(InvalidValueFlagsEnum);
Expand Down
14 changes: 13 additions & 1 deletion libs/shared/src/types/feature-flags/flags.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ export type IFlagKey = `IS_${Uppercase<string>}_ENABLED`;
*/
export function testFlagEnumValidity<TEnum extends IFlags, IFlags = Record<IFlagKey, IFlagKey>>(
testEnum: TEnum & Record<Exclude<keyof TEnum, keyof IFlags>, ['Key must follow `IFlagKey` format']>
) {}
) {
for (const key in testEnum) {
if (testEnum.hasOwnProperty(key) && isIFlagKey(key) && isIFlagKey(testEnum[key])) {
if (key !== testEnum[key]) {
throw Error('Enum name must match the value');
}
}
}
}

function isIFlagKey(value: unknown): value is IFlagKey {
return typeof value === 'string';
}

0 comments on commit 97236a7

Please sign in to comment.