Enum
A Swift-inspired enum implementation for JavaScript.
Basics
First, install from npm
:
npm install swift-enum
Then, create a subclass of the Enum
default export and add cases (and, optionally, freeze the enum to prevent the addition of addition cases):
; {}Theme; console; // {name: Midnight, rawValue: Midnight}console; // trueconsole; // Midnight ; // Error: can't instantiate instances of an enum class
API
Enum Classes
-
.type
sets the type of therawValue
for an enum. You can set it to be eitherString
(default, sets therawValue
to be the same as the name of the case) orNumber
(so that cases are automatically given a zero-indexed number as theirrawValue
).static type = Number;Day;console; // 0 -
.case()
adds additional cases. You can pass a simple list of string case names (which will be given the defaultrawValue
given the specifiedtype
of the enum), or an object where the keys are the names of the enum cases and the values are therawValue
s.static type = Number;Day;console // {name: Monday, rawValue: 0}{}Color;console; // {name: Blue, rawValue: #527364} -
.freeze()
prevents additional cases from being added to the enum.{}Color;Color;Color; // Error -
.cases
returns an array with all cases for the enum.{}console; // []Color;console; // [Color.Red, Color.Blue] -
Symbol.iterator
allows iteration over all cases of an enum in afor of
loop.{}Color;for const color of Colorconsole; // Color.Red, Color.Blue
Enum Cases
Enums have two properties by default:
-
#name
: The name of the case, which will always be the string you used to create the enum case. -
#rawValue
: The value backing the enum case. ForString
enums, this will be the same as the name. ForNumber
enums, this will be a zero-indexed number. You can also provide a customrawValue
by passing an object to the enum's.case()
method.
For example, for the following case:
{}Color;
The following properties will be set on the cases:
console; // {name: 'Blue', rawValue: '#5273C4'}console; // {name: 'Red', rawValue: '#E13737'}
In addition to these default properties, you can set additional properties and methods by adding them to the custom Enum
subclass.
{ } { } Convention;console; // trueconsole; // falseconsole; // Convention.CamelCase
Other Exports
The index file for this project also provides two named exports:
-
NamingConvention
: This is an enum that has a few cases for common enum naming conventions:ScreamingSnakeCase
,PascalCase
, andNoRules
. You can use theEnumClass
getter on any of these cases to create a newEnum
base class that will throw an error if you try to add a case that does not match the specified naming convention.;ScreamingSnakeCaseEnumClass {}Color;Color; // ErrorPascalCaseEnumClass {}Color; // ErrorColor;NoRulesEnumClass {}Color;Color;This can be great to export for use within your project to ensure all enum cases are being declared consistently.
-
SimpleEnum
: This function can be used to create a very simple enum. You can provide to it any arguments you can provide toEnum.case
. When using the function, the resulting enum will be frozen for you. Note that, althoughinstanceof
will continue to work correctly, thetoString()
value of cases will not have the name of the enum as it does for enums subclassingEnum
. You also can't change the type of the enum (but can provide customrawValue
s by passing an object).;const Theme = ;Theme; // Errorconsole; // {name: Midnight, rawValue: Midnight}console; // trueconsole; // Midnightconsole; // Enum.Midnight; // Error