Skip to content

Commit ccbd48a

Browse files
authored
Update README.md
1 parent c350b09 commit ccbd48a

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
S
1+
Custom Enum Library for Roblox
2+
Overview
3+
The Custom Enum Library for Roblox allows you to define and manage custom enumerations in your Roblox games, providing an efficient and flexible way to handle constant values associated with specific categories or types. This library leverages hashing and metatables to ensure that enum items are uniquely identified and easy to use while offering excellent performance. You can create custom enums, check valid enum items, and retrieve values by name or hash.
4+
5+
Features
6+
Custom Enum Creation: Define and register your own enums with unique names and values.
7+
Efficient Hashing: Each enum and enum item is hashed for quick access and uniqueness.
8+
Read-only Enums: Ensures that once an enum is created, its values cannot be modified, enforcing immutability.
9+
Strong Type Safety: Ensures enums and enum items are handled correctly with type-checking functions.
10+
Error Handling: Provides meaningful error messages when invalid operations or values are encountered.
11+
Usage
12+
Create a Custom Enum
13+
You can create a custom enum by using the newEnum function. Define your enum name and its items.
14+
15+
```lua
16+
local MyEnum = Enum.new("MyEnum", {
17+
Item1 = 1,
18+
Item2 = 2,
19+
Item3 = 3
20+
})
21+
```
22+
Access Enum Items
23+
To access a specific enum item by its name:
24+
25+
```lua
26+
local item = MyEnum.Item1
27+
print(item) -- Output: the corresponding value of Item1 (1)
28+
```
29+
Check if a Value is a Valid Enum Item
30+
You can check if a value is a valid enum item:
31+
32+
```lua
33+
if Enum.isEnumItem(MyEnum.Item2) then
34+
print("Item2 is a valid enum item!")
35+
end
36+
```
37+
Get Enum Item by Value
38+
You can retrieve an enum item using its numeric value:
39+
40+
```lua
41+
local item = MyEnum:FromValue(2)
42+
print(item) -- Output: the corresponding enum item for value 2
43+
```
44+
Get Enum Name from Value
45+
You can get the name of an enum item based on its value:
46+
47+
```lua
48+
local name = MyEnum:GetNameFromValue(2)
49+
print(name) -- Output: Item2
50+
```
51+
Enum Item Count
52+
You can get the total number of enum items in an enum:
53+
54+
```lua
55+
local count = #MyEnum
56+
print(count) -- Output: the number of items in the enum
57+
```
58+
Get All Registered Enums
59+
To get a list of all registered enums:
60+
61+
```lua
62+
local enums = Enum.getRegisteredEnums()
63+
for _, enum in ipairs(enums) do
64+
print(enum)
65+
end
66+
```
67+
API Documentation
68+
- **newEnum(enumName: string, enumItems: table):**
69+
Creates a new enum with the given name and items. Each item should have a unique value, either a number or a string.
70+
71+
- **Enum:FromValue(value: number):**
72+
Returns the enum item corresponding to the given value. Throws an error if the value is invalid.
73+
74+
- **Enum:GetNameFromValue(value: number):**
75+
Returns the name of the enum item associated with the provided value.
76+
77+
- **Enum:GetEnumItems():**
78+
Returns a list of all enum item names in the enum.
79+
80+
- **Enum.isEnum(enum: Enum):**
81+
Checks if the provided object is a valid enum.
82+
83+
- **Enum.isEnumItem(enumItem: EnumItem):**
84+
Checks if the provided object is a valid enum item.
85+
86+
- **__len():**
87+
Returns the number of enum items in the enum.
88+
89+
- **__tostring():**
90+
Returns the string representation of the enum name.
91+
92+
License
93+
This library is open-source and licensed under the MIT License.

0 commit comments

Comments
 (0)