Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/corecii/lua-enum-simple
A super-simple Enum-like object for Roblox Lua
https://github.com/corecii/lua-enum-simple
Last synced: 12 days ago
JSON representation
A super-simple Enum-like object for Roblox Lua
- Host: GitHub
- URL: https://github.com/corecii/lua-enum-simple
- Owner: Corecii
- License: mit
- Created: 2021-11-11T05:10:41.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-13T22:35:48.000Z (about 3 years ago)
- Last Synced: 2024-10-28T02:30:41.989Z (2 months ago)
- Language: Lua
- Size: 750 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LuaEnum
A super-simple enum type and a super-simple enum collection type.
[Documentation](https://corecii.github.io/lua-enum-simple/)
Install with [wally](https://wally.run):
```toml
# wally.toml
[dependencies]
LuaEnum = "corecii/[email protected]"
```[or use a packaged release model](https://github.com/Corecii/lua-enum-simple/releases/latest)
---
Think Roblox's enums, not Rust's enums.
I couldn't find a good mix between serializability, debugability, and simplicity, so this module lets you choose between:
* "Id" representation, `"Animal.Dog"`
* "ValueId" representation, `"Animal.1"`
* "Name" representation, `"Dog"`
* "Value" representation, `1`
* "Symbol" representation, a userdata with `tostring(symbol) == ""`It defaults to `Id` representation.
Additionally, this module provides a way to turn a tree of modulescripts and folders into a tree of EnumCollections and Enums.
---
LuaEnum Example:
```lua
local Animals = LuaEnum.new("Animals", {
Dog = 1,
Cat = 2,
Mouse = 3,
})print(Animals.Dog) --> "Animals.Dog"
print(LuaEnum.is(Animals)) --> true
print(LuaEnum.isItem(Animals, Animals.Dog)) --> true
print(LuaEnum.isItem(Animals, "x")) --> falselocal function speak(animal)
assert(LuaEnum.isItem(Animals, animal))if animal == Animals.Dog then
print("woof")
elseif animal == Animals.Cat then
print("meow")
elseif animal == Animals.Mouse then
print("sque")
end
endspeak(Animals.Cat) --> "meow"
-- Errors:
local x = Animals.Lizard --> Error: Item "Lizard" does not exist in LuaEnum Animals
Animals.Cat = 5 --> Error: Attempt to modify a readonly table
```