@quenty/enums 1.1.0-canary.644.d3040d7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/LICENSE.md +21 -0
- package/README.md +23 -0
- package/default.project.json +14 -0
- package/package.json +38 -0
- package/src/Shared/SimpleEnum.lua +157 -0
- package/src/node_modules.project.json +7 -0
- package/test/default.project.json +19 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 1.1.0-canary.644.d3040d7.0 (2026-01-16)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add SimpleEnum package and interface, to simplify enum usage across Nevermore ([d3040d7](https://github.com/Quenty/NevermoreEngine/commit/d3040d7a07ae7b2586bb982399b401fe19f1eb3f))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2026 James Onnen (Quenty)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## Enums
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
5
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://discord.gg/mhtGUS8">
|
|
8
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
11
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
|
12
|
+
</a>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
A very simple enum implementation that makes typechecking boilerplate simpler.
|
|
16
|
+
|
|
17
|
+
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/SimpleEnum">View docs →</a></div>
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
npm install @quenty/enums --save
|
|
23
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quenty/enums",
|
|
3
|
+
"version": "1.1.0-canary.644.d3040d7.0",
|
|
4
|
+
"description": "Custom Luau enum implementation",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Roblox",
|
|
7
|
+
"Nevermore",
|
|
8
|
+
"Lua",
|
|
9
|
+
"enums"
|
|
10
|
+
],
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/Quenty/NevermoreEngine/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/Quenty/NevermoreEngine.git",
|
|
17
|
+
"directory": "src/enums/"
|
|
18
|
+
},
|
|
19
|
+
"funding": {
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://www.patreon.com/quenty"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"preinstall": "npx only-allow pnpm"
|
|
26
|
+
},
|
|
27
|
+
"contributors": [
|
|
28
|
+
"Quenty"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@quenty/loader": "10.9.3",
|
|
32
|
+
"@quentystudios/t": "^3.0.0"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "d3040d7a07ae7b2586bb982399b401fe19f1eb3f"
|
|
38
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
A very simple enum implementation that makes typechecking boilerplate simpler.
|
|
4
|
+
|
|
5
|
+
```lua
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local SimpleEnum = require("SimpleEnum")
|
|
9
|
+
|
|
10
|
+
export type MyEnumType = "none" | "always"
|
|
11
|
+
|
|
12
|
+
return SimpleEnum.new({
|
|
13
|
+
NONE = "none" :: "none",
|
|
14
|
+
ALWAYS = "always" :: "always",
|
|
15
|
+
})
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
@class SimpleEnum
|
|
19
|
+
]=]
|
|
20
|
+
|
|
21
|
+
local require = require(script.Parent.loader).load(script)
|
|
22
|
+
|
|
23
|
+
local t = require("t")
|
|
24
|
+
|
|
25
|
+
local SimpleEnum = {}
|
|
26
|
+
SimpleEnum.ClassName = "SimpleEnum"
|
|
27
|
+
|
|
28
|
+
export type EnumValue = any
|
|
29
|
+
|
|
30
|
+
export type SimpleEnum<EnumMembers> = EnumMembers & {
|
|
31
|
+
GetKeys: (self: SimpleEnum<EnumMembers>) -> { string },
|
|
32
|
+
GetValues: (self: SimpleEnum<EnumMembers>) -> { EnumValue },
|
|
33
|
+
GetMap: (self: SimpleEnum<EnumMembers>) -> EnumMembers,
|
|
34
|
+
IsValue: (self: SimpleEnum<EnumMembers>, value: any) -> (boolean, string?),
|
|
35
|
+
GetInterface: (self: SimpleEnum<EnumMembers>) -> (value: any) -> (boolean, string?),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type PrivateSimpleEnum<EnumMembers> =
|
|
39
|
+
typeof(setmetatable(
|
|
40
|
+
{} :: {
|
|
41
|
+
_members: { [string]: EnumValue },
|
|
42
|
+
},
|
|
43
|
+
{} :: typeof({ __index = SimpleEnum })
|
|
44
|
+
))
|
|
45
|
+
& SimpleEnum<EnumMembers>
|
|
46
|
+
|
|
47
|
+
--[=[
|
|
48
|
+
Creates a new SimpleEnum. This is indexable like a normal table, BUT it is also
|
|
49
|
+
type-checkable via `IsValue` and `GetInterface` and more.
|
|
50
|
+
]=]
|
|
51
|
+
function SimpleEnum.new<EnumMembers>(members: EnumMembers): SimpleEnum<EnumMembers>
|
|
52
|
+
assert(type(members) == "table", "Members must be a table")
|
|
53
|
+
|
|
54
|
+
local self: SimpleEnum<EnumMembers> = setmetatable({
|
|
55
|
+
_members = table.freeze(members),
|
|
56
|
+
}, SimpleEnum) :: any
|
|
57
|
+
|
|
58
|
+
for key, value in pairs(members :: any) do
|
|
59
|
+
rawset(self, key, value)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
return self
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
--[=[
|
|
66
|
+
Returns the list of enum keys.
|
|
67
|
+
]=]
|
|
68
|
+
function SimpleEnum.GetKeys<EnumMembers>(self: SimpleEnum<EnumMembers>): { string }
|
|
69
|
+
local members: any = rawget(self, "_members")
|
|
70
|
+
local keys = {}
|
|
71
|
+
for key, _ in pairs(members) do
|
|
72
|
+
table.insert(keys, key)
|
|
73
|
+
end
|
|
74
|
+
return keys
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
--[=[
|
|
78
|
+
Returns the list of enum values.
|
|
79
|
+
]=]
|
|
80
|
+
function SimpleEnum.GetValues<EnumMembers>(self: SimpleEnum<EnumMembers>): { EnumValue }
|
|
81
|
+
local members: any = rawget(self, "_members")
|
|
82
|
+
local values = {}
|
|
83
|
+
for _, value in pairs(members) do
|
|
84
|
+
table.insert(values, value)
|
|
85
|
+
end
|
|
86
|
+
return values
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
--[=[
|
|
90
|
+
Returns the map of enum members.
|
|
91
|
+
]=]
|
|
92
|
+
function SimpleEnum.GetMap<EnumMembers>(self: SimpleEnum<EnumMembers>): EnumMembers
|
|
93
|
+
return rawget(self, "_members") :: EnumMembers
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
--[=[
|
|
97
|
+
Returns whether the value is a valid enum value.
|
|
98
|
+
|
|
99
|
+
```lua
|
|
100
|
+
local MyEnum = SimpleEnum.new({
|
|
101
|
+
FOO = "foo",
|
|
102
|
+
BAR = "bar",
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
print(MyEnum:IsValue("foo")) --> true
|
|
106
|
+
print(MyEnum:IsValue("baz")) --> false, "Expected one of: foo, bar; got baz"
|
|
107
|
+
```
|
|
108
|
+
]=]
|
|
109
|
+
function SimpleEnum.IsValue<EnumMembers>(self: PrivateSimpleEnum<EnumMembers>, value: any): (boolean, string?)
|
|
110
|
+
return self:GetInterface()(value)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
--[=[
|
|
114
|
+
Returns a type-checking function for the enum.
|
|
115
|
+
|
|
116
|
+
```lua
|
|
117
|
+
local MyEnum = SimpleEnum.new({
|
|
118
|
+
FOO = "foo",
|
|
119
|
+
BAR = "bar",
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
local typeChecker = MyEnum:GetInterface()
|
|
123
|
+
|
|
124
|
+
print(typeChecker("foo")) --> true
|
|
125
|
+
print(typeChecker("baz")) --> false, "Expected one of: foo, bar; got baz"
|
|
126
|
+
```
|
|
127
|
+
]=]
|
|
128
|
+
function SimpleEnum.GetInterface<EnumMembers>(self: PrivateSimpleEnum<EnumMembers>): (value: any) -> (boolean, string?)
|
|
129
|
+
local found = rawget(self :: any, "_typeChecker")
|
|
130
|
+
if found then
|
|
131
|
+
return found
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
local typeChecker: any = t.valueOf(self:GetValues())
|
|
135
|
+
rawset(self :: any, "_typeChecker", typeChecker)
|
|
136
|
+
return typeChecker
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
SimpleEnum.__index = function<EnumMembers>(self: PrivateSimpleEnum<EnumMembers>, index: string)
|
|
140
|
+
if SimpleEnum[index] then
|
|
141
|
+
return SimpleEnum[index]
|
|
142
|
+
elseif self._members[index] then
|
|
143
|
+
return self._members[index]
|
|
144
|
+
else
|
|
145
|
+
error(`'{tostring(self)}' has no member '{index}'`)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
SimpleEnum.__iter = function<EnumMembers>(self: PrivateSimpleEnum<EnumMembers>)
|
|
150
|
+
return pairs(self._members)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
SimpleEnum.__newindex = function<EnumMembers>(self: PrivateSimpleEnum<EnumMembers>, index: string)
|
|
154
|
+
error(`Cannot assign to member '{index}' of enum '{tostring(self)}'`)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
return SimpleEnum
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "EnumsTest",
|
|
3
|
+
"globIgnorePaths": [
|
|
4
|
+
"**/.package-lock.json",
|
|
5
|
+
"**/.pnpm",
|
|
6
|
+
"**/.pnpm-workspace-state-v1.json",
|
|
7
|
+
"**/.modules.yaml",
|
|
8
|
+
"**/.ignored",
|
|
9
|
+
"**/.ignored_*"
|
|
10
|
+
],
|
|
11
|
+
"tree": {
|
|
12
|
+
"$className": "DataModel",
|
|
13
|
+
"ServerScriptService": {
|
|
14
|
+
"enums": {
|
|
15
|
+
"$path": ".."
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|