react-code-dataset / wp-calypso /client /jetpack-cloud /sections /partner-portal /lib /value-to-enum.ts
react-code-dataset / wp-calypso /client /jetpack-cloud /sections /partner-portal /lib /value-to-enum.ts
Download wp-calypso/client/jetpack-cloud/sections/partner-portal/lib/value-to-enum.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 750 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/jetpack-cloud/sections/partner-portal/lib/value-to-enum.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/jetpack-cloud/sections/partner-portal/lib/value-to-enum.ts
-
curl -L -o value-to-enum.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/jetpack-cloud/sections/partner-portal/lib/value-to-enum.ts
750 Bytes
| /** | |
| * Convert a value to a the enum member with that value or a fallback. | |
| * This is a hack around TypeScript's poor support of enums as types. | |
| * @example const enumMember = valueToEnum< SomeEnumType >( SomeEnumType, 'foo', SomeEnumType.SomeMember ); | |
| * @template T | |
| * @param {Record< string, * >} enumType Enum type to search in. | |
| * @param {*} value The enum value we are looking to get the member for. | |
| * @param {*} fallback The fallback value in case value is not a member of enumType. | |
| * @returns {T} T for value or fallback | |
| */ | |
| export default function valueToEnum< T >( | |
| enumType: Record< string, unknown >, | |
| value: unknown, | |
| fallback: unknown | |
| ): T { | |
| return Object.values( enumType ).includes( value ) ? ( value as T ) : ( fallback as T ); | |
| } | |