Skip to content

Commit

Permalink
Merge pull request #4 from howmanysmall/V2.2.0
Browse files Browse the repository at this point in the history
V2.2.0
  • Loading branch information
howmanysmall authored Apr 13, 2021
2 parents 6414fa7 + 2e05e8f commit fdfb360
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.2.0] - 2021-04-12

### Added
- Added `Enumerator.getEnumeratorItems()`, which returns an array of every EnumeratorItem.
- Added `place.project.json` which allows serving directly to a place.

### Changed
- Changed the blacklisted values asserts to a single assert for readability reasons.

## [2.1.0] - 2021-04-09
### Added
- **[BREAKING]** Added `Enumerator.cast` to the enumerator function list, replacing `castToEnumerator`.
Expand All @@ -25,6 +34,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- All the code.

[2.1.0]: https://github.com/howmanysmall/enumerator/compare/v1.0.0...v2.0.0
<!-- [2.1.0]: https://github.com/howmanysmall/enumerator/compare/v1.0.0...v2.0.0 -->
<!-- [1.1.0]: https://github.com/howmanysmall/enumerator/compare/v1.0.0...v1.1.0 -->
[1.0.0]: https://github.com/howmanysmall/enumerator/releases/tag/1.0.0
<!-- [1.0.0]: https://github.com/howmanysmall/enumerator/releases/tag/1.0.0 -->
4 changes: 2 additions & 2 deletions foreman.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[tools]
rojo = { source = "rojo-rbx/rojo", version = "6.0.0-rc.4" }
run-in-roblox = { source = "rojo-rbx/run-in-roblox", version = "0.3.0" }
rojo = {source = "rojo-rbx/rojo", version = "6.1.0"}
run-in-roblox = {source = "rojo-rbx/run-in-roblox", version = "0.3.0"}
14 changes: 14 additions & 0 deletions place.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "enumerator",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"$ignoreUnknownInstances": true,
"enumerator": {
"$path": "src/enumerator",
"$ignoreUnknownInstances": true
}
}
}
}
2 changes: 1 addition & 1 deletion rotriever.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "howmanysmall/enumerator"
version = "2.0.0"
version = "2.2.0"
author = "howmanysmall"
dependencies_root = "./src/enumerator/"
content_root = "./src/enumerator"
Expand Down
35 changes: 29 additions & 6 deletions src/enumerator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ local ALREADY_USED_NAME_ERROR = "Already used %q as a value name in enum %q."
local ALREADY_USED_VALUE_ERROR = "Already used %q as a value in enum %q."
local INVALID_MEMBER_ERROR = "%q (%s) is not a valid member of %s"
local INVALID_VALUE_ERROR = "Couldn't cast value %q (%s) to enumerator %q"
local CANNOT_USE_ERROR = "Cannot use '%s' as a value"

local BLACKLISTED_VALUES = {
cast = true,
fromRawValue = true,
getEnumeratorItems = true,
isEnumValue = true,
}

local enumeratorTuple = t.tuple(
t.string,
Expand Down Expand Up @@ -47,6 +55,7 @@ local function enumerator(enumName, enumValues)
local enum = newproxy(true)
local internal = {}
local rawValues = {}
local totalEnums = 0

function internal.fromRawValue(rawValue)
return rawValues[rawValue]
Expand Down Expand Up @@ -89,11 +98,25 @@ local function enumerator(enumName, enumValues)
end
end

--[[**
Returns an array of the enumerator items.
@returns [t:array] An array of the items.
**--]]
function internal.getEnumeratorItems()
local enumItems = table.create(totalEnums)
local length = 0

for _, value in pairs(rawValues) do
length += 1
enumItems[length] = value
end

return enumItems
end

if enumValues[1] then
for _, valueName in ipairs(enumValues) do
assert(valueName ~= "fromRawValue", "Cannot use 'fromRawValue' as a value")
assert(valueName ~= "isEnumValue", "Cannot use 'isEnumValue' as a value")
assert(valueName ~= "cast", "Cannot use 'cast' as a value")
assert(not BLACKLISTED_VALUES[valueName], string.format(CANNOT_USE_ERROR, tostring(valueName)))
assert(internal[valueName] == nil, string.format(ALREADY_USED_NAME_ERROR, valueName, enumName))
assert(rawValues[valueName] == nil, string.format(ALREADY_USED_VALUE_ERROR, valueName, enumName))

Expand Down Expand Up @@ -124,12 +147,11 @@ local function enumerator(enumName, enumValues)

internal[valueName] = value
rawValues[valueName] = value
totalEnums += 1
end
else
for valueName, rawValue in pairs(enumValues) do
assert(valueName ~= "fromRawValue", "Cannot use 'fromRawValue' as a value")
assert(valueName ~= "isEnumValue", "Cannot use 'isEnumValue' as a value")
assert(valueName ~= "cast", "Cannot use 'cast' as a value")
assert(not BLACKLISTED_VALUES[valueName], string.format(CANNOT_USE_ERROR, tostring(valueName)))
assert(internal[valueName] == nil, string.format(ALREADY_USED_NAME_ERROR, valueName, enumName))
assert(rawValues[valueName] == nil, string.format(ALREADY_USED_VALUE_ERROR, valueName, enumName))

Expand Down Expand Up @@ -160,6 +182,7 @@ local function enumerator(enumName, enumValues)

internal[valueName] = value
rawValues[rawValue] = value
totalEnums += 1
end
end

Expand Down
15 changes: 15 additions & 0 deletions src/enumerator/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type describeSKIP = (string) -> nil

return function()
local enumerator = require(script.Parent)
local t = require(script.Parent.t)

-- describe = describe :: describe
-- expect = expect :: expect
Expand Down Expand Up @@ -211,6 +212,20 @@ return function()
end)
end)

describe("Enum.getEnumeratorItems", function()
local MyListEnum = enumerator("MyListEnum", {"ValueOne", "ValueTwo"})
it("should return an array", function()
expect(MyListEnum.getEnumeratorItems()).to.be.a("table")
expect(t.array(t.any)(MyListEnum.getEnumeratorItems())).to.equal(true)
end)

it("should contain every value", function()
for _, enum in ipairs(MyListEnum.getEnumeratorItems()) do
expect(MyListEnum.isEnumValue(enum)).to.equal(true)
end
end)
end)

it("should error when creating an enum with a non-string name", function()
expect(function()
enumerator(1, {"ValueOne"})
Expand Down

0 comments on commit fdfb360

Please sign in to comment.