Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Refactor Array.includes and partial Dictionary reimplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cxmeel committed Mar 12, 2024
1 parent 293274c commit 79d4052
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Array/includes.luau
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ local find = require("./find")
```
]=]
local function includes<T>(array: { T }, value: T, from: number?): boolean
return find(array, value, from) ~= nil
local index = find(array, value, from)
return index ~= nil
end

return includes
13 changes: 13 additions & 0 deletions src/Dictionary/copy.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--[=[
@within Dictionary
@function copy
@param dictionary { [K]: V }
@return { [K]: V }
Returns a shallow copy of the dictionary. Use [Dictionary.copyDeep] to copy nested arrays.
```lua
copy({ a = 1, b = 2 }) -- { a = 1, b = 2 }
```
]=]
return table.clone
25 changes: 25 additions & 0 deletions src/Dictionary/copyDeep.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--[=[
@within Dictionary
Returns a deep copy of the given dictionary. This means that all nested tables are also copied.
```lua
local dictionary = { a = 1, b = { c = 2 } }
copyDeep(dictionary) -- { a = 1, b = { c = 2 } }
```
]=]
local function copyDeep<K, V>(dictionary: { [K]: V }): { [K]: V }
local out: { [K]: V } = {}

for key, value in dictionary do
if typeof(value) == "table" then
out[key] = copyDeep(value) :: any
else
out[key] = value
end
end

return out
end

return copyDeep
File renamed without changes.
21 changes: 21 additions & 0 deletions src/Dictionary/flip.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--[=[
@within Dictionary
Returns a new dictionary with the keys and values of the input dictionary swapped.
```lua
local dictionary = { a = "apple", b = "banana" }
flip(dictionary) -- { apple = "a", banana = "b" }
```
]=]
local function flip<K, V>(dictionary: { [K]: V }): { [V]: K }
local out = {}

for key, value in dictionary do
out[value] = key
end

return out
end

return flip
Empty file removed src/Dictionary/flip.luau.todo
Empty file.
20 changes: 20 additions & 0 deletions src/Dictionary/freeze.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local copy = require("./copy")

--[=[
@within Dictionary
Returns a new dictionary that is a shallow copy of the input dictionary, but is frozen. This means that the dictionary cannot be modified in any way. This is useful for ensuring that a dictionary is not modified after it has been created. Use [Dictionary.freezeDeep] to freeze a dictionary and all of its nested dictionaries.
```lua
local frozen = freeze({ queen = "elsa", princess = "anna" })
frozen.princess = "elsa" -- error!
frozen.spirit = "bruni" -- error!
```
]=]
local function freeze<K, V>(dictionary: { [K]: V }): { [K]: V }
local out = copy(dictionary)
return table.freeze(out)
end

return freeze
Empty file removed src/Dictionary/freeze.luau.todo
Empty file.
24 changes: 24 additions & 0 deletions src/Dictionary/get.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--[=[
@within Dictionary
]=]
local function get<K, V>(dictionary: { [K]: V }, key: K, dotSeparated: boolean?): V?
if not dotSeparated then
return dictionary[key]
end

local parts: { string } = (key :: any):split(".")
local value: any = dictionary

while #parts > 0 do
local part = table.remove(parts, 1)
value = value[part]

if value == nil then
return nil
end
end

return value
end

return get
14 changes: 14 additions & 0 deletions src/Dictionary/has.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--[=[
@within Dictionary
Returns whether the given dictionary contains the given key.
```lua
has({ a = 1, b = 2 }, "a") -- true
```
]=]
local function has<K, V>(dictionary: { [K]: V }, key: K): boolean
return dictionary[key] ~= nil
end

return has
Empty file removed src/Dictionary/has.luau.todo
Empty file.
20 changes: 20 additions & 0 deletions src/Dictionary/includes.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--[=[
@within Dictionary
Returns whether the dictionary includes a certain value or not. If the value is found, the function also returns the key associated with the value.
```lua
includes({ a = 1, b = 3 }, 2) -- false
```
]=]
local function includes<K, V>(dictionary: { [K]: V }, value: V): (boolean, K?)
for key, val in dictionary do
if val == value then
return true, key
end
end

return false, nil
end

return includes
Empty file removed src/Dictionary/includes.luau.todo
Empty file.
20 changes: 20 additions & 0 deletions src/Dictionary/keys.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--[=[
@within Dictionary
Returns an array of all the keys in a dictionary.
```lua
keys({ a = 1, b = 2, c = 3 }) -- { "a", "b", "c" }
```
]=]
local function keys<K, V>(dictionary: { [K]: V }): { K }
local out = {}

for key in dictionary do
table.insert(out, key)
end

return out
end

return keys
Empty file removed src/Dictionary/keys.luau.todo
Empty file.
19 changes: 19 additions & 0 deletions src/Dictionary/set.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local copy = require("./copy")

--[=[
@within Dictionary
Returns a new dictionary with the given key set to the given value.
```lua
set({ a = 1 }, "b", 2) -- { a = 1, b = 2 }
```
]=]
local function set<K, V>(dictionary: { [K]: V }, key: K, value: V): { [K]: V }
local out = copy(dictionary)
out[key] = value

return out
end

return set
Empty file removed src/Dictionary/set.luau.todo
Empty file.
20 changes: 20 additions & 0 deletions src/Dictionary/values.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--[=[
@within Dictionary
Returns an array of all the values in a dictionary.
```lua
values({ a = 1, b = 2, c = 3 }) -- { 1, 2, 3 }
```
]=]
local function values<K, V>(dictionary: { [K]: V }): { V }
local out = {}

for _, value in dictionary do
table.insert(out, value)
end

return out
end

return values
Empty file removed src/Dictionary/values.luau.todo
Empty file.

0 comments on commit 79d4052

Please sign in to comment.