/
init.lua
101 lines (88 loc) · 1.85 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-- EnumList
-- Stephen Leitnick
-- January 08, 2021
type EnumNames = { string }
--[=[
@interface EnumItem
.Name string
.Value number
.EnumType EnumList
@within EnumList
]=]
export type EnumItem = {
Name: string,
Value: number,
EnumType: any,
}
local LIST_KEY = newproxy()
local NAME_KEY = newproxy()
local function CreateEnumItem(name: string, value: number, enum: any): EnumItem
local enumItem = {
Name = name,
Value = value,
EnumType = enum,
}
table.freeze(enumItem)
return enumItem
end
--[=[
@class EnumList
Defines a new Enum.
]=]
local EnumList = {}
EnumList.__index = EnumList
--[=[
@param name string
@param enums {string}
@return EnumList
Constructs a new EnumList.
```lua
local directions = EnumList.new("Directions", {
"Up",
"Down",
"Left",
"Right",
})
local direction = directions.Up
```
]=]
function EnumList.new(name: string, enums: EnumNames)
assert(type(name) == "string", "Name string required")
assert(type(enums) == "table", "Enums table required")
local self = {}
self[LIST_KEY] = {}
self[NAME_KEY] = name
for i, enumName in ipairs(enums) do
assert(type(enumName) == "string", "Enum name must be a string")
local enumItem = CreateEnumItem(enumName, i, self)
self[enumName] = enumItem
table.insert(self[LIST_KEY], enumItem)
end
return table.freeze(setmetatable(self, EnumList))
end
--[=[
@param obj any
@return boolean
Returns `true` if `obj` belongs to the EnumList.
]=]
function EnumList:BelongsTo(obj: any): boolean
return type(obj) == "table" and obj.EnumType == self
end
--[=[
Returns an array of all enum items.
@return {EnumItem}
@since v2.0.0
]=]
function EnumList:GetEnumItems()
return self[LIST_KEY]
end
--[=[
Get the name of the enum.
@return string
@since v2.0.0
]=]
function EnumList:GetName()
return self[NAME_KEY]
end
export type EnumList = typeof(EnumList.new(...))
return EnumList