@ncoderz/superenum
Version:
Simple, typesafe enums in TypeScript, fully compatible with standard JavaScript
361 lines (184 loc) • 8.13 kB
Markdown
[**@ncoderz/superenum**](../README.md)
***
[@ncoderz/superenum](../globals.md) / Superenum
# Interface: Superenum\<K, V, T\>
Defined in: [superenum.ts:75](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L75)
## Type Parameters
### K
`K` *extends* `EnumKey` = `EnumKey`
### V
`V` *extends* `EnumValue` = `EnumValue`
### T
`T` *extends* `ObjectEnum`\<`K`, `V`\> = `ObjectEnum`\<`K`, `V`\>
## Methods
### fromValue()
> **fromValue**(`value`, `options?`): `undefined` \| [`EnumType`](../type-aliases/EnumType.md)\<`T`\>
Defined in: [superenum.ts:93](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L93)
Validate a possible enum value, returning the enum value if valid, otherwise undefined.
Since an enum value is just the value, then all this function does is check to see if the value exists on the enum, and if
so returns it cast to the enum type, otherwise it returns undefined.
Note: If the enum has duplicate values when lower-cased and
[FromValueOptions.ignoreCase](FromValueOptions.md#ignorecase) is true, the data returned when when values clash will be indeterminate.
#### Parameters
##### value
`unknown`
the enum value to validate
##### options?
[`FromValueOptions`](FromValueOptions.md)
options for the function
#### Returns
`undefined` \| [`EnumType`](../type-aliases/EnumType.md)\<`T`\>
the enum value, or undefined if the value cannot be matched to the enum
***
### fromKey()
> **fromKey**(`key`, `options?`): `undefined` \| [`EnumType`](../type-aliases/EnumType.md)\<`T`\>
Defined in: [superenum.ts:105](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L105)
Get an enum value from its key, returning the value if key valid, otherwise undefined.
Note: If the enum has duplicate keys when lower-cased and
[FromKeyOptions.ignoreCase](FromKeyOptions.md#ignorecase) is true, the data returned when when keys clash will be indeterminate.
#### Parameters
##### key
the enum key to convert to enum value
`undefined` | `null` | `string` | `number`
##### options?
[`FromKeyOptions`](FromKeyOptions.md)
options for the function
#### Returns
`undefined` \| [`EnumType`](../type-aliases/EnumType.md)\<`T`\>
the enum represented by the key, or undefined if the key cannot be matched to the enum
***
### keyFromValue()
> **keyFromValue**(`value`, `options?`): `undefined` \| `string`
Defined in: [superenum.ts:120](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L120)
Get an enum key from its value, returning the key if value valid, otherwise undefined.
Note: If the enum has duplicate values when lower-cased and
[FromValueOptions.ignoreCase](FromValueOptions.md#ignorecase) is true, the data returned when when values clash will be indeterminate.
#### Parameters
##### value
`unknown`
the enum value to convert to enum key
##### options?
[`KeyFromValueOptions`](KeyFromValueOptions.md)
options for the function
#### Returns
`undefined` \| `string`
the enum key represented by the value, or undefined if the value cannot be matched to the enum
***
### hasValue()
> **hasValue**(`value`, `options?`): `boolean`
Defined in: [superenum.ts:135](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L135)
Check if an enum has a value, returning true if yes, otherwise false.
Note: If the enum has duplicate values (or duplicate values when lower-cased if
[HasValueOptions.ignoreCase](HasValueOptions.md#ignorecase) is true), the data returned when when values clash will be indeterminate.
#### Parameters
##### value
the enum value to check
`undefined` | `null` | [`EnumType`](../type-aliases/EnumType.md)\<`T`\>
##### options?
[`HasValueOptions`](HasValueOptions.md)
options for the function
#### Returns
`boolean`
true if the enum has the value, otherwise false
***
### hasKey()
> **hasKey**(`key`, `options?`): `boolean`
Defined in: [superenum.ts:147](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L147)
Check if an enum has a key, returning true if yes, otherwise false.
Note: If the enum has duplicate keys when lower-cased and
[FromKeyOptions.ignoreCase](FromKeyOptions.md#ignorecase) is true, the data returned when when keys clash will be indeterminate.
#### Parameters
##### key
the enum key to check
`undefined` | `null` | `string`
##### options?
[`HasKeyOptions`](HasKeyOptions.md)
options for the function
#### Returns
`boolean`
true if the enum has the key, otherwise false
***
### values()
> **values**(): readonly [`EnumType`](../type-aliases/EnumType.md)\<`T`\>[]
Defined in: [superenum.ts:154](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L154)
Get an array of the enum values.
#### Returns
readonly [`EnumType`](../type-aliases/EnumType.md)\<`T`\>[]
iterator over the enum values
***
### keys()
> **keys**(): readonly keyof `T`[]
Defined in: [superenum.ts:161](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L161)
Get an array of the enum keys.
#### Returns
readonly keyof `T`[]
iterator over the enum values
***
### entries()
> **entries**(): readonly \[keyof `T`, [`EnumType`](../type-aliases/EnumType.md)\<`T`\>\][]
Defined in: [superenum.ts:168](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L168)
Get an array of the enum entries.
#### Returns
readonly \[keyof `T`, [`EnumType`](../type-aliases/EnumType.md)\<`T`\>\][]
iterator over the enum values
***
### \[iterator\]()
> **\[iterator\]**(): `IterableIterator`\<[`EnumType`](../type-aliases/EnumType.md)\<`T`\>\>
Defined in: [superenum.ts:175](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L175)
An iterator that iterates the enum values.
#### Returns
`IterableIterator`\<[`EnumType`](../type-aliases/EnumType.md)\<`T`\>\>
iterator over the enum values
***
### setAllLabels()
> **setAllLabels**(`allLabels`): `void`
Defined in: [superenum.ts:182](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L182)
Set i18n labels for all enum values.
#### Parameters
##### allLabels
an object containing i18n labels for each enum value
#### Returns
`void`
***
### setLabels()
> **setLabels**(`value`, `labels`): `void`
Defined in: [superenum.ts:190](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L190)
Set i18n labels for a specific enum value.
#### Parameters
##### value
[`EnumType`](../type-aliases/EnumType.md)\<`T`\>
the enum value to set i18n labels for
##### labels
[`Labels`](Labels.md)
an object containing i18n labels for the enum value
#### Returns
`void`
***
### getLabels()
> **getLabels**(`value`): [`Labels`](Labels.md)
Defined in: [superenum.ts:198](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L198)
Get i18n labels for a specific enum value.
#### Parameters
##### value
[`EnumType`](../type-aliases/EnumType.md)\<`T`\>
the enum value to get i18n labels for
#### Returns
[`Labels`](Labels.md)
an object containing i18n labels for the enum value
***
### getLabel()
> **getLabel**(`value`, `locale?`): `string`
Defined in: [superenum.ts:210](https://github.com/ncoderz/superenum/blob/08c1c2419d89a58d6deeaad343b246571a3c3674/src/superenum.ts#L210)
Get a label for a specific enum value in a specific locale.
If no locale is provided, it will return the first label that was set.
If no label is found for the value, it will return the value as a string.
#### Parameters
##### value
[`EnumType`](../type-aliases/EnumType.md)\<`T`\>
the enum value to get the label for
##### locale?
`string`
the locale to get the label for
#### Returns
`string`
the label for the enum value in the specified locale