From 97236a70058963ed444eb6748af3794d9b6f1b87 Mon Sep 17 00:00:00 2001 From: Denis Kralj Date: Fri, 3 May 2024 21:07:07 +0200 Subject: [PATCH] refactor: Remove unused feature flag Add (ugly) solution to verify matching of enum name and value --- .../src/types/feature-flags/flags.types.spec.ts | 4 ++-- libs/shared/src/types/feature-flags/flags.types.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libs/shared/src/types/feature-flags/flags.types.spec.ts b/libs/shared/src/types/feature-flags/flags.types.spec.ts index 9ecc2f93e032..b87bbb823ed0 100644 --- a/libs/shared/src/types/feature-flags/flags.types.spec.ts +++ b/libs/shared/src/types/feature-flags/flags.types.spec.ts @@ -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); diff --git a/libs/shared/src/types/feature-flags/flags.types.ts b/libs/shared/src/types/feature-flags/flags.types.ts index 274e73ae0b05..449431c2f10b 100644 --- a/libs/shared/src/types/feature-flags/flags.types.ts +++ b/libs/shared/src/types/feature-flags/flags.types.ts @@ -17,4 +17,16 @@ export type IFlagKey = `IS_${Uppercase}_ENABLED`; */ export function testFlagEnumValidity>( testEnum: TEnum & Record, ['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'; +}