From a3a864c98f7c6a90a4828095db34ad8ce379350d Mon Sep 17 00:00:00 2001 From: adammabin Date: Thu, 15 Sep 2022 07:58:20 +0100 Subject: [PATCH] Add ToKeyValuePairs1D --- build.rbxlx | 11 ++++++++ build.rbxlx.lock | 5 ++++ src/Map/ToKeyValuePairs1D.lua | 14 ++++++++++ src/Map/ToKeyValuePairs1D.spec.lua | 41 ++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 build.rbxlx.lock create mode 100644 src/Map/ToKeyValuePairs1D.lua create mode 100644 src/Map/ToKeyValuePairs1D.spec.lua diff --git a/build.rbxlx b/build.rbxlx index 501340e..ecc3715 100644 --- a/build.rbxlx +++ b/build.rbxlx @@ -980,6 +980,11 @@ local function Merge1D(...: {T}): {T} for SubArrayIndex = 2, select("#", ...) do local SubArray = select(SubArrayIndex, ...) + + if (not SubArray) then + continue + end + local Size = #SubArray table.move(SubArray, 1, Size, Index, Result) Index += Size @@ -1848,6 +1853,12 @@ local function Merge1D(...) local Result = {} for Index = 1, select("#", ...) do + local Table = select(Index, ...) + + if (not Table) then + continue + end + for Key, Value in select(Index, ...) do Result[Key] = Value end diff --git a/build.rbxlx.lock b/build.rbxlx.lock new file mode 100644 index 0000000..0f3c041 --- /dev/null +++ b/build.rbxlx.lock @@ -0,0 +1,5 @@ +5284 +RobloxStudioBeta +ADAM-PC +8fadebc4-bf3a-4c4a-bb47-5300124d5ff3 + diff --git a/src/Map/ToKeyValuePairs1D.lua b/src/Map/ToKeyValuePairs1D.lua new file mode 100644 index 0000000..8cad132 --- /dev/null +++ b/src/Map/ToKeyValuePairs1D.lua @@ -0,0 +1,14 @@ +local function ToKeyValuePairs1D(Structure: {[K]: V}): {{Key: K, Value: V}} + local Result = {} + + for Key, Value in Structure do + table.insert(Result, { + Key = Key; + Value = Value; + }) + end + + return Result +end + +return ToKeyValuePairs1D \ No newline at end of file diff --git a/src/Map/ToKeyValuePairs1D.spec.lua b/src/Map/ToKeyValuePairs1D.spec.lua new file mode 100644 index 0000000..7ef01ea --- /dev/null +++ b/src/Map/ToKeyValuePairs1D.spec.lua @@ -0,0 +1,41 @@ +return function() + local ToKeyValuePairs1D = require(script.Parent.ToKeyValuePairs1D) + + describe("Map/ToKeyValuePairs1D", function() + it("should return a blank array given an empty table", function() + expect(#ToKeyValuePairs1D({})).to.equal(0) + end) + + it("should return a single key-value pair for a single item table", function() + local Result = ToKeyValuePairs1D({A = 1}) + expect(#Result).to.equal(1) + expect(Result[1].Key).to.equal("A") + expect(Result[1].Value).to.equal(1) + end) + + it("should return a key-value pair for each item in the table", function() + local Result = ToKeyValuePairs1D({A = 1, B = 2, C = 3}) + expect(#Result).to.equal(3) + + local function ArrayItemSatisfies(Condition) + for _, Pair in Result do + if Condition(Pair) then + return true + end + end + end + + expect(ArrayItemSatisfies(function(Pair) + return Pair.Key == "A" and Pair.Value == 1 + end)).to.equal(true) + + expect(ArrayItemSatisfies(function(Pair) + return Pair.Key == "B" and Pair.Value == 2 + end)).to.equal(true) + + expect(ArrayItemSatisfies(function(Pair) + return Pair.Key == "C" and Pair.Value == 3 + end)).to.equal(true) + end) + end) +end \ No newline at end of file