https://github.com/danser-freelancer/javascript-enum-model
A model or 'feature prototype' for vanilla javascript enums alive at runtime
https://github.com/danser-freelancer/javascript-enum-model
c-sharp enum es2022 javascript rust
Last synced: 23 days ago
JSON representation
A model or 'feature prototype' for vanilla javascript enums alive at runtime
- Host: GitHub
- URL: https://github.com/danser-freelancer/javascript-enum-model
- Owner: DANser-freelancer
- License: bsd-3-clause
- Created: 2024-11-17T03:00:31.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-11-20T12:44:18.000Z (7 months ago)
- Last Synced: 2025-05-13T11:05:54.153Z (23 days ago)
- Topics: c-sharp, enum, es2022, javascript, rust
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# About
This here is a relatively small and simple model of what a javascript enum could be.
I'm unaware of any edge cases. Need help with testing in a real environment.I don't write compiler code so I had to cobble together a system that would do the compilers job, out of available javascript mechanisms.
This model is technically functional but ideally I would want to see most of the work and sugar syntax done by a runtime/typescript compiler.
I would honestly like to pitch this to the typescript team, however I have no idea how to do that.
Contact me if you think this works much better than their enums, and I'll explain it in more detail.I have borrowed some sensible design decisions from other languages, and I hope I got it right.
The enums come in several flavors:- **C# enum**, an object listing of named constants, each associated with a hidden integer value
- usually the compiler would replace all references to enum fields with corresponding integers
- **C# `[Flags]` enum**, similar to C#, but in my model it forces the enum to have bit specific fields
- **Rust enum**, a symbolic enum
- an enum that when declared acts as a namespace of predefined constructors
- fields in this kind of enum can represent simple and complex values (like objects or integers) or act as unique constants
- you can create new `EnumVariant` instances that will follow the structure defined in the enum field
- comes with a pattern matching function
- this flavor was the most painfully complicated one and probably has the most bugsAlso all `Enum` and `EnumVariant` instances _should be_ immutable
# Authors
- [Dan](https://github.com/DANser-freelancer): Code
### Documentation
- [Typescript implementation]()
- [JS runtime implementation]()
- [Use in raw form](https://github.com/DANser-freelancer/javascript-enum-model/blob/main/raw.md)