Skip to content

Commit

Permalink
Add ToKeyValuePairs1D
Browse files Browse the repository at this point in the history
  • Loading branch information
tpc9000 committed Sep 15, 2022
1 parent ea39938 commit a3a864c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions build.rbxlx
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,11 @@ local function Merge1D<T>(...: {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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions build.rbxlx.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
5284
RobloxStudioBeta
ADAM-PC
8fadebc4-bf3a-4c4a-bb47-5300124d5ff3

14 changes: 14 additions & 0 deletions src/Map/ToKeyValuePairs1D.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local function ToKeyValuePairs1D<K, V>(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
41 changes: 41 additions & 0 deletions src/Map/ToKeyValuePairs1D.spec.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a3a864c

Please sign in to comment.