From 98fde4b60050b5f1a7cb4442ec2d3dbacc963dfe Mon Sep 17 00:00:00 2001 From: HowManySmall Date: Mon, 12 Apr 2021 18:01:10 -0600 Subject: [PATCH 1/4] Added to V2.2.0. --- CHANGELOG.md | 12 ++++++++++-- place.project.json | 14 ++++++++++++++ src/enumerator/init.lua | 35 +++++++++++++++++++++++++++++------ src/enumerator/init.spec.lua | 15 +++++++++++++++ 4 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 place.project.json diff --git a/CHANGELOG.md b/CHANGELOG.md index bf88220..25c2f80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ 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. + +### 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`. @@ -25,6 +33,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 + -[1.0.0]: https://github.com/howmanysmall/enumerator/releases/tag/1.0.0 \ No newline at end of file + \ No newline at end of file diff --git a/place.project.json b/place.project.json new file mode 100644 index 0000000..5539dec --- /dev/null +++ b/place.project.json @@ -0,0 +1,14 @@ +{ + "name": "enumerator", + "tree": { + "$className": "DataModel", + "ReplicatedStorage": { + "$className": "ReplicatedStorage", + "$ignoreUnknownInstances": true, + "enumerator": { + "$path": "src/enumerator", + "$ignoreUnknownInstances": true + } + } + } +} \ No newline at end of file diff --git a/src/enumerator/init.lua b/src/enumerator/init.lua index 0ebd2b5..fb38641 100644 --- a/src/enumerator/init.lua +++ b/src/enumerator/init.lua @@ -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, @@ -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] @@ -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)) @@ -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)) @@ -160,6 +182,7 @@ local function enumerator(enumName, enumValues) internal[valueName] = value rawValues[rawValue] = value + totalEnums += 1 end end diff --git a/src/enumerator/init.spec.lua b/src/enumerator/init.spec.lua index b534c77..93a662f 100644 --- a/src/enumerator/init.spec.lua +++ b/src/enumerator/init.spec.lua @@ -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 @@ -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"}) From 3d364637159c1a53d35a322006530a3a7e16cfe8 Mon Sep 17 00:00:00 2001 From: HowManySmall Date: Mon, 12 Apr 2021 18:01:38 -0600 Subject: [PATCH 2/4] Updated toml. --- rotriever.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rotriever.toml b/rotriever.toml index d64a946..cb50e93 100644 --- a/rotriever.toml +++ b/rotriever.toml @@ -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" From a03a372c5108bb53770ba4cac189db0273199b3d Mon Sep 17 00:00:00 2001 From: HowManySmall Date: Mon, 12 Apr 2021 18:02:09 -0600 Subject: [PATCH 3/4] Updated foreman. --- foreman.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/foreman.toml b/foreman.toml index e8315e0..a1cd2ef 100644 --- a/foreman.toml +++ b/foreman.toml @@ -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" } \ No newline at end of file +rojo = {source = "rojo-rbx/rojo", version = "6.1.0"} +run-in-roblox = {source = "rojo-rbx/run-in-roblox", version = "0.3.0"} \ No newline at end of file From 2e05e8fd0cf7e242bb66c3aea6e8c5c097899da0 Mon Sep 17 00:00:00 2001 From: HowManySmall Date: Mon, 12 Apr 2021 18:02:34 -0600 Subject: [PATCH 4/4] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25c2f80..15e2d1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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.