This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
Array.includes
and partial Dictionary reimplementation
- Loading branch information
Showing
20 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.