LuaObjects - Maps, Arrays, and Sets all in Roblox

What is LuaObjects?

A deceptive title, as this does not implement Objects as seen from JavaScript into Lua. It was either LuaObjects or LuaMaps-Sets-and-Arrays. I think you can see why I chose to opt to use this name instead.

LuaObjects is an implementation of Arrays, Maps, and Sets – all in 90% parity with the Arrays, Maps, and Sets seen in JavaScript.

Missing Features
  • The prototype and __proto__ tags
  • Symbol.species and Symbol.iterator
  • .toString.call()
  • @@unscopables
  • entries(), keys(), values() are missing on all. I don’t doubt that this could be added in the future, I just didn’t see a need to do them.
  • Array.group()
  • All inheritance from JavaScript Function and Object.

Examples

Arrays
local Array = require(game.ReplicatedStorage.Packages.LuaObjects).Array

-- An array constructor accepts any iterable value; including strings, maps, and other arrays
-- A number could also be provided to specify its max length
Array.new()
Array.new(3) -- max length of 3
Array.new("hi!") -- ["h", "i", "!"]
Array.new({5, 12}) -- [5, 12]
Array.of(5, 12) -- [5, 12]

-- Just like JavaScript, using new is not required.
Array()

local newLength = array:push(7) -- 3
local mappedArray = array:map(function(num)
    return num * 2
end) -- [10, 24, 14]

-- To get a value, you can just use array[key]
print(array[2]) -- 24
if mappedArray:includes(10) then
    print(mappedArray:indexOf(10)) -- 2
end
Maps
local Map = require(game.ReplicatedStorage.Packages.LuaObjects).Maps

-- A map constructor accepts arrays with key-value pairs and generic tables
Map.new()
Map.new({ hi = "Hello World!" })

local array = Array.new({{"key", "value"}, {"lorem", "ipsum"}})
Map.new(array)

map:set("key", "value")
map:forEach(function(value, key)
    print(key, value)
end)

-- To get a value or set a value, you can just use map[key]
print(map.key) -- "value"
map["ping"] = "pong"
Sets
local Set = require(game.ReplicatedStorage.Packages.LuaObjects).Sets

-- A set constructor accepts arrays, maps, tables, and strings
Set.new("hi!")
Set.new(Array.of(5, 12))
Set.new(Map.new({answer = 42})
Set.new({answer = "its not 42!"})
Set()

set:add("value")
set:add("value")
print(set.size) -- 1 -- No duplicates can be added to sets
set:delete("value")
Extras
local util = require(game.ReplicatedStorage.Packages.LuaObjects).util

util.shallowCopy({"hi", {"bye"}}) -- {"hi", table0x0FF}
util.deepCopy({"hi", {"bye"}}) -- {"hi", {"bye"}}
util.depthCopy -- allows you to decide how many "levels" deep in the table you want to copy

util.stringify({"hi", "how are you", "good"}) -- "hi, how are you, good"
util.iterateString("hello") -- {"h", "e", "l", "l", "o"}

If for whatever reason you may be using my Import module, you can require it like this:

local Array = import "@wally/luaObjects/array"

Installing LuaObjects

LuaObjects is a Wally Package. If you rather download it from GitHub you can from its source.

Using Wally

[dependencies]
LuaObjects = "alexinite/maps-arrays-sets@0.1.3"
1 Like

Ok, cool, any chance you could provide us with visuals, if not, its still pretty good anyway

I provided examples, the code is also open source.
It is also not really ideal to visualize a module that implements strict functionality to the already built-in tables.