From 5d1ecac03f94e33eb519bb1aaa29388ec8f7c2bc Mon Sep 17 00:00:00 2001 From: Cam <21029087+cxmeel@users.noreply.github.com> Date: Wed, 13 Mar 2024 18:23:26 +0000 Subject: [PATCH] Partial Dictionary reimplementations and new Dictionary methods --- luauspec.toml | 2 ++ src/Array/concat.luau | 2 +- src/Array/count.luau | 12 +++---- src/Array/flatten.luau | 2 +- src/Dictionary/count.luau | 30 ++++++++++++++++++ src/Dictionary/count.luau.todo | 0 src/Dictionary/entries.luau | 21 +++++++++++++ src/Dictionary/entries.luau.todo | 0 src/Dictionary/every.luau | 22 +++++++++++++ src/Dictionary/every.luau.todo | 0 src/Dictionary/expandKeys.luau | 45 +++++++++++++++++++++++++++ src/Dictionary/filter.luau | 27 ++++++++++++++++ src/Dictionary/filter.luau.todo | 0 src/Dictionary/flatten.luau | 30 ++++++++++++++++++ src/Dictionary/flatten.luau.todo | 0 src/Dictionary/flattenKeys.luau | 36 +++++++++++++++++++++ src/Dictionary/freezeDeep.luau | 34 ++++++++++++++++++++ src/Dictionary/freezeDeep.luau.todo | 0 src/Dictionary/fromArrays.luau | 23 ++++++++++++++ src/Dictionary/fromArrays.luau.todo | 0 src/Dictionary/fromEntries.luau | 21 +++++++++++++ src/Dictionary/fromEntries.luau.todo | 0 src/Dictionary/get.luau | 15 +++++++++ src/Dictionary/map.luau | 34 ++++++++++++++++++++ src/Dictionary/map.luau.todo | 0 src/Dictionary/merge.luau | 29 +++++++++++++++++ src/Dictionary/merge.luau.todo | 0 src/Dictionary/mergeDeep.luau | 44 ++++++++++++++++++++++++++ src/Dictionary/mergeDeep.luau.todo | 0 src/Dictionary/removeKey.luau | 17 ++++++++++ src/Dictionary/removeKey.luau.todo | 0 src/Dictionary/removeKeys.luau | 21 +++++++++++++ src/Dictionary/removeKeys.luau.todo | 0 src/Dictionary/removeValue.luau | 16 ++++++++++ src/Dictionary/removeValue.luau.todo | 0 src/Dictionary/removeValues.luau | 21 +++++++++++++ src/Dictionary/removeValues.luau.todo | 0 src/Dictionary/some.luau | 22 +++++++++++++ src/Dictionary/some.luau.todo | 0 src/Dictionary/withKeys.luau | 21 +++++++++++++ src/Dictionary/withKeys.luau.todo | 0 wally.toml | 2 -- 42 files changed, 539 insertions(+), 10 deletions(-) create mode 100644 luauspec.toml create mode 100644 src/Dictionary/count.luau delete mode 100644 src/Dictionary/count.luau.todo create mode 100644 src/Dictionary/entries.luau delete mode 100644 src/Dictionary/entries.luau.todo create mode 100644 src/Dictionary/every.luau delete mode 100644 src/Dictionary/every.luau.todo create mode 100644 src/Dictionary/expandKeys.luau create mode 100644 src/Dictionary/filter.luau delete mode 100644 src/Dictionary/filter.luau.todo create mode 100644 src/Dictionary/flatten.luau delete mode 100644 src/Dictionary/flatten.luau.todo create mode 100644 src/Dictionary/flattenKeys.luau create mode 100644 src/Dictionary/freezeDeep.luau delete mode 100644 src/Dictionary/freezeDeep.luau.todo create mode 100644 src/Dictionary/fromArrays.luau delete mode 100644 src/Dictionary/fromArrays.luau.todo create mode 100644 src/Dictionary/fromEntries.luau delete mode 100644 src/Dictionary/fromEntries.luau.todo create mode 100644 src/Dictionary/map.luau delete mode 100644 src/Dictionary/map.luau.todo create mode 100644 src/Dictionary/merge.luau delete mode 100644 src/Dictionary/merge.luau.todo create mode 100644 src/Dictionary/mergeDeep.luau delete mode 100644 src/Dictionary/mergeDeep.luau.todo create mode 100644 src/Dictionary/removeKey.luau delete mode 100644 src/Dictionary/removeKey.luau.todo create mode 100644 src/Dictionary/removeKeys.luau delete mode 100644 src/Dictionary/removeKeys.luau.todo create mode 100644 src/Dictionary/removeValue.luau delete mode 100644 src/Dictionary/removeValue.luau.todo create mode 100644 src/Dictionary/removeValues.luau delete mode 100644 src/Dictionary/removeValues.luau.todo create mode 100644 src/Dictionary/some.luau delete mode 100644 src/Dictionary/some.luau.todo create mode 100644 src/Dictionary/withKeys.luau delete mode 100644 src/Dictionary/withKeys.luau.todo diff --git a/luauspec.toml b/luauspec.toml new file mode 100644 index 0000000..f46a883 --- /dev/null +++ b/luauspec.toml @@ -0,0 +1,2 @@ +#! use("wally.toml") +name = "cxmeel/sift" diff --git a/src/Array/concat.luau b/src/Array/concat.luau index 1808944..52a61cb 100644 --- a/src/Array/concat.luau +++ b/src/Array/concat.luau @@ -10,7 +10,7 @@ concat(array1, array2) -- {1, 2, 3, 4, 5, 6} ``` ]=] -local function concat(...: { T }) +local function concat(...: { any }): { T } local out = {} for _, array in { ... } do diff --git a/src/Array/count.luau b/src/Array/count.luau index 07f6e93..dd25cc6 100644 --- a/src/Array/count.luau +++ b/src/Array/count.luau @@ -1,3 +1,5 @@ +local reduce = require("./reduce") + --[=[ @within Array @@ -13,15 +15,13 @@ ``` ]=] local function count(array: { T }, predicate: ((value: T, index: number) -> boolean)?): number - local counter = 0 - - for index, value in array do + return reduce(array, function(acc: number, value, index) if not predicate or predicate(value, index) then - counter += 1 + return acc + 1 end - end - return counter + return acc + end, 0) end return count diff --git a/src/Array/flatten.luau b/src/Array/flatten.luau index 88aaa2a..c4ff9d3 100644 --- a/src/Array/flatten.luau +++ b/src/Array/flatten.luau @@ -1,4 +1,4 @@ -local INF = newproxy(false) +local INF = newproxy() --[=[ @within Array diff --git a/src/Dictionary/count.luau b/src/Dictionary/count.luau new file mode 100644 index 0000000..a5a8660 --- /dev/null +++ b/src/Dictionary/count.luau @@ -0,0 +1,30 @@ +--[=[ + @within Dictionary + + Returns the number of elements in the dictionary. If a `predicate` is provided, it will only count the elements that satisfy the predicate. + + ```lua + local dictionary = { a = 1, b = 2, c = 3 } + + count(dictionary) -- 3 + count(dictionary, function(value) + return value > 1 + end) -- 2 + ``` +]=] +local function count( + dictionary: { [K]: V }, + predicate: ((value: V, key: K) -> boolean)? +): number + local counter = 0 + + for key, value in dictionary do + if not predicate or predicate(value, key) then + counter += 1 + end + end + + return counter +end + +return count diff --git a/src/Dictionary/count.luau.todo b/src/Dictionary/count.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/entries.luau b/src/Dictionary/entries.luau new file mode 100644 index 0000000..b56db2b --- /dev/null +++ b/src/Dictionary/entries.luau @@ -0,0 +1,21 @@ +--[=[ + @within Dictionary + + Returns an array of key-value pairs from a dictionary. + + ```lua + entries({ a = 1, b = 2, c = 3 }) + -- { { "a", 1 }, { "b", 2 }, { "c", 3 } } + ``` +]=] +local function entries(dictionary: { [K]: V }): { { K | V } } + local out = {} + + for key, value in dictionary do + table.insert(out, { key, value :: any }) + end + + return out +end + +return entries diff --git a/src/Dictionary/entries.luau.todo b/src/Dictionary/entries.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/every.luau b/src/Dictionary/every.luau new file mode 100644 index 0000000..f12a889 --- /dev/null +++ b/src/Dictionary/every.luau @@ -0,0 +1,22 @@ +--[=[ + @within Dictionary + + Returns `true` if all values in the dictionary pass the test implemented by the provided function. + + ```lua + every({ a = 1, b = 3, c = 5 }, function(value) + return value % 2 == 0 + }) -- false + ``` +]=] +local function every(dictionary: { [K]: V }, predicate: (value: V, key: K) -> boolean): boolean + for key, value in dictionary do + if not predicate(value, key) then + return false + end + end + + return true +end + +return every diff --git a/src/Dictionary/every.luau.todo b/src/Dictionary/every.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/expandKeys.luau b/src/Dictionary/expandKeys.luau new file mode 100644 index 0000000..eb3cfc2 --- /dev/null +++ b/src/Dictionary/expandKeys.luau @@ -0,0 +1,45 @@ +type ExpandedDictionary = { [string]: ExpandedDictionary | V } + +--[=[ + @within Dictionary + + Expands a dictionary with keys that contain a separator into a nested dictionary. + + ```lua + local dictionary = { + ["a"] = 1, + ["b.c"] = 2, + ["b.d"] = 3, + } + + expandKeys(dictionary) -- { a = 1, b = { c = 2, d = 3 } } + ``` +]=] +local function expandKeys( + dictionary: { [string]: V }, + separator: string? +): ExpandedDictionary + local withSeparator = separator or "." + local out = {} + + for key, value in dictionary do + local parts = key:split(withSeparator) + local current = out + + while #parts > 1 do + local part = table.remove(parts, 1) + + if not current[part] then + current[part] = {} + end + + current = current[part] + end + + current[parts[1]] = value :: any + end + + return out :: any +end + +return expandKeys diff --git a/src/Dictionary/filter.luau b/src/Dictionary/filter.luau new file mode 100644 index 0000000..cf6939f --- /dev/null +++ b/src/Dictionary/filter.luau @@ -0,0 +1,27 @@ +--[=[ + @within Dictionary + + Filters out elements from a dictionary based on a filter function. The filter function should return `true` to keep the element, and `false` to remove it. + + ```lua + filter({ a = 1, b = 2, c = 3 }, function(value) + return value > 1 + end) -- { b = 2, c = 3 } + ``` +]=] +local function filter( + dictionary: { [K]: V }, + filterer: (value: V, key: K) -> boolean +): { [K]: V } + local out = {} + + for key, value in dictionary do + if filterer(value, key) then + out[key] = value + end + end + + return out +end + +return filter diff --git a/src/Dictionary/filter.luau.todo b/src/Dictionary/filter.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/flatten.luau b/src/Dictionary/flatten.luau new file mode 100644 index 0000000..c0a83f9 --- /dev/null +++ b/src/Dictionary/flatten.luau @@ -0,0 +1,30 @@ +local INF = newproxy() + +--[=[ + @within Dictionary + + Flattens nested dictionaries into a single dictionary. The `depth` parameter specifies the depth of the flattening. If `depth` is not specified, the dictionary is flattened as much as possible. +]=] +local function flatten(dictionary: { [any]: any }, depth: number?): { [K]: V } + local withDepth = depth or INF + local out = {} + + for key, value in dictionary do + if typeof(value) == "table" and (withDepth == INF or withDepth > 0) then + local useDepth = withDepth == INF and INF or withDepth - 1 + local nested = flatten(value, useDepth) + + for nestedKey, nestedValue in nested do + out[nestedKey] = nestedValue + end + + continue + end + + out[key] = value + end + + return out +end + +return flatten diff --git a/src/Dictionary/flatten.luau.todo b/src/Dictionary/flatten.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/flattenKeys.luau b/src/Dictionary/flattenKeys.luau new file mode 100644 index 0000000..94fcae7 --- /dev/null +++ b/src/Dictionary/flattenKeys.luau @@ -0,0 +1,36 @@ +local INF = newproxy() + +type FlattenableDictionary = { [string]: FlattenableDictionary | V } + +--[=[ + @within Dictionary +]=] +local function flattenKeys( + dictionary: FlattenableDictionary, + depth: number?, + separator: string? +): { [string]: V } + local withSeparator = separator or "." + local withDepth = depth or INF + + local out: { [string]: V } = {} + + for key, value in dictionary do + if typeof(value) == "table" and depth > 0 then + local useDepth = withDepth == INF and INF or withDepth - 1 + local nested = flattenKeys(value, useDepth, withSeparator) + + for nestedKey, nestedValue in nested do + out[`{key}{withSeparator}{nestedKey}`] = nestedValue + end + + continue + end + + out[key] = value :: V + end + + return out +end + +return flattenKeys diff --git a/src/Dictionary/freezeDeep.luau b/src/Dictionary/freezeDeep.luau new file mode 100644 index 0000000..3c7359a --- /dev/null +++ b/src/Dictionary/freezeDeep.luau @@ -0,0 +1,34 @@ +--[=[ + @within Dictionary + + Recursively freezes a dictionary and all of its nested dictionaries. Using [Dictionary.freeze] will only freeze the top level of the dictionary, leaving nested dictionaries mutable. + + ```lua + local frozen = freezeDeep({ + a = "apple", + b = { c = "carrot" }, + }) + + frozen.a = "avocado" -- error! + frozen.b.c = "cabbage" -- error! + frozen.c = "coconut" -- error! + ``` +]=] +local function freezeDeep(dictionary: { [K]: V }): { [K]: V } + local out = {} + + for key, value in dictionary do + if typeof(value) == "table" then + local nested: any = freezeDeep(value) + out[key] = nested + + continue + end + + out[key] = value + end + + return table.freeze(out) +end + +return freezeDeep diff --git a/src/Dictionary/freezeDeep.luau.todo b/src/Dictionary/freezeDeep.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/fromArrays.luau b/src/Dictionary/fromArrays.luau new file mode 100644 index 0000000..7a0522a --- /dev/null +++ b/src/Dictionary/fromArrays.luau @@ -0,0 +1,23 @@ +--[=[ + @within Dictionary + + Constructs a dictionary from two arrays, one containing the keys and the other containing the values. + + ```lua + local keys = { "a", "b", "c" } + local values = { 1, 2, 3 } + + fromArrays(keys, values) -- { a = 1, b = 2, c = 3 } + ``` +]=] +local function fromArrays(keys: { K }, values: { V }): { [K]: V } + local out = {} + + for index, key in keys do + out[key] = values[index] + end + + return out +end + +return fromArrays diff --git a/src/Dictionary/fromArrays.luau.todo b/src/Dictionary/fromArrays.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/fromEntries.luau b/src/Dictionary/fromEntries.luau new file mode 100644 index 0000000..f2cb3b5 --- /dev/null +++ b/src/Dictionary/fromEntries.luau @@ -0,0 +1,21 @@ +--[=[ + @within Dictionary + + Constructs a dictionary from an array of key-value pairs. + + ```lua + fromEntries({ { "a", 1 }, { "b", 2 }, { "c", 3 } }) + -- { a = 1, b = 2, c = 3 } + ``` +]=] +local function fromEntries(entries: { { K | V } }): { [K]: V } + local out = {} + + for _, entry in entries do + out[entry[1] :: K] = entry[2] :: V + end + + return out +end + +return fromEntries diff --git a/src/Dictionary/fromEntries.luau.todo b/src/Dictionary/fromEntries.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/get.luau b/src/Dictionary/get.luau index 205c9c7..226a66b 100644 --- a/src/Dictionary/get.luau +++ b/src/Dictionary/get.luau @@ -1,5 +1,20 @@ --[=[ @within Dictionary + + Retrieves a value from a dictionary using a key. If the key is a dot-separated string and the `dotSeparated` parameter is `true`, the function will traverse the dictionary using the parts of the string as keys. If the key is not found, the function will return `nil`. + + ```lua + local dictionary = { + foo = { + bar = { + baz = "qux" + } + } + } + + get(dictionary, "foo.bar.baz") -- nil + get(dictionary, "foo.bar.baz", true) -- "qux" + ``` ]=] local function get(dictionary: { [K]: V }, key: K, dotSeparated: boolean?): V? if not dotSeparated then diff --git a/src/Dictionary/map.luau b/src/Dictionary/map.luau new file mode 100644 index 0000000..c698538 --- /dev/null +++ b/src/Dictionary/map.luau @@ -0,0 +1,34 @@ +local None = require("@Sift/None") + +--[=[ + @within Dictionary + + Processes each value and key of a dictionary with a mapper function and returns a new dictionary with the results. If no key is returned by the mapper, the original key is used. + + ```lua + map({ hello = "world" }, function(value, key) + return `{value}!`, key:upper() + end) -- { HELLO = "world!" } + + map({ hello = "world" }, function(value) + return value:upper() + end) -- { hello = "WORLD" } + ``` +]=] +local function map(dictionary: { [K]: V }, mapper: (value: V, key: K) -> (Y, X)): { [X]: Y } + local out: { [X]: Y } = {} + + for key, value in dictionary do + local mappedValue, mappedKey = mapper(value, key) + + if mappedKey == None or mappedValue == None then + continue + end + + out[mappedKey or key :: any] = mappedValue + end + + return out +end + +return map diff --git a/src/Dictionary/map.luau.todo b/src/Dictionary/map.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/merge.luau b/src/Dictionary/merge.luau new file mode 100644 index 0000000..3b758b3 --- /dev/null +++ b/src/Dictionary/merge.luau @@ -0,0 +1,29 @@ +local None = require("@Sift/None") + +--[=[ + @within Dictionary + + Merges multiple dictionaries into a single dictionary. If a key is present in multiple dictionaries, the value from the last dictionary in the list will be used. + + ```lua + local dictionary1 = { a = 1, b = 2 } + local dictionary2 = { b = 3, c = 4 } + + merge(dictionary1, dictionary2) -- { a = 1, b = 3, c = 4 } + ``` +]=] +local function merge(...: { [any]: any }): { [K]: V } + local out = {} + + for _, dictionary in { ... } do + for key, value in dictionary do + if key ~= None and value ~= None then + out[key] = value + end + end + end + + return out +end + +return merge diff --git a/src/Dictionary/merge.luau.todo b/src/Dictionary/merge.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/mergeDeep.luau b/src/Dictionary/mergeDeep.luau new file mode 100644 index 0000000..9b49f8e --- /dev/null +++ b/src/Dictionary/mergeDeep.luau @@ -0,0 +1,44 @@ +local None = require("@Sift/None") +local copyDeep = require("./copyDeep") + +--[=[ + @within Dictionary + + Combines multiple dictionaries into a single dictionary, including nested dictionaries. + + ```lua + local dictionary1 = { foo = "bar", deep = { foo = "bar" } } + local dictionary2 = { deep = { foo = "baz" } } + + mergeDeep(dictionary1, dictionary2) + -- { foo = "bar", deep = { foo = "baz" } } + ``` +]=] +local function mergeDeep(...: { [any]: any }): { [K]: V } + local out: any = {} + + for _, dictionary in { ... } do + for key, value in dictionary do + if value == None then + out[key] = nil + continue + end + + if typeof(value) == "table" then + if out[key] == nil or typeof(out[key]) ~= "table" then + out[key] = copyDeep(value) + continue + end + + out[key] = mergeDeep(out[key], value) + continue + end + + out[key] = value + end + end + + return out +end + +return mergeDeep diff --git a/src/Dictionary/mergeDeep.luau.todo b/src/Dictionary/mergeDeep.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/removeKey.luau b/src/Dictionary/removeKey.luau new file mode 100644 index 0000000..25b3956 --- /dev/null +++ b/src/Dictionary/removeKey.luau @@ -0,0 +1,17 @@ +local removeKeys = require("./removeKeys") + +--[=[ + @within Dictionary + + Returns a new dictionary with the specified key removed. + + ```lua + removeKey({ a = 1, b = 2, c = 3 }, "b") + -- { a = 1, c = 3 } + ``` +]=] +local function removeKey(dictionary: { [K]: V }, key: K): { [K]: V } + return removeKeys(dictionary, key) +end + +return removeKey diff --git a/src/Dictionary/removeKey.luau.todo b/src/Dictionary/removeKey.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/removeKeys.luau b/src/Dictionary/removeKeys.luau new file mode 100644 index 0000000..8d522b6 --- /dev/null +++ b/src/Dictionary/removeKeys.luau @@ -0,0 +1,21 @@ +local filter = require("./filter") + +--[=[ + @within Dictionary + + Returns a new dictionary with the specified keys removed. + + ```lua + removeKeys({ a = 1, b = 2, c = 3 }, "a", "c") + -- { b = 2 } + ``` +]=] +local function removeKeys(dictionary: { [K]: V }, ...: K): { [K]: V } + local keys = { ... } + + return filter(dictionary, function(_, key) + return table.find(keys, key) == nil + end) +end + +return removeKeys diff --git a/src/Dictionary/removeKeys.luau.todo b/src/Dictionary/removeKeys.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/removeValue.luau b/src/Dictionary/removeValue.luau new file mode 100644 index 0000000..5985c65 --- /dev/null +++ b/src/Dictionary/removeValue.luau @@ -0,0 +1,16 @@ +local removeValues = require("./removeValues") + +--[=[ + @within Dictionary + + Removes the specified value from the dictionary. This function will remove all instances of the value from the dictionary. + + ```lua + removeValue({ a = 1, b = 2, c = 1 }, 1) -- { b = 2 } + ``` +]=] +local function removeValue(dictionary: { [K]: V }, value: V): { [K]: V } + return removeValues(dictionary, value) +end + +return removeValue diff --git a/src/Dictionary/removeValue.luau.todo b/src/Dictionary/removeValue.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/removeValues.luau b/src/Dictionary/removeValues.luau new file mode 100644 index 0000000..b858de7 --- /dev/null +++ b/src/Dictionary/removeValues.luau @@ -0,0 +1,21 @@ +local filter = require("./filter") + +--[=[ + @within Dictionary + + Returns a new dictionary with all specified values removed. + + ```lua + removeValues({ a = 2, b = 1, c = 3, d = 2 }, 1, 2) + -- { c = 3 } + ``` +]=] +local function removeValues(dictionary: { [K]: V }, ...: V): { [K]: V } + local values = { ... } + + return filter(dictionary, function(value) + return table.find(values, value) == nil + end) +end + +return removeValues diff --git a/src/Dictionary/removeValues.luau.todo b/src/Dictionary/removeValues.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/some.luau b/src/Dictionary/some.luau new file mode 100644 index 0000000..12093dd --- /dev/null +++ b/src/Dictionary/some.luau @@ -0,0 +1,22 @@ +--[=[ + @within Dictionary + + Returns `true` if at least one value in the dictionary passes the test implemented by the provided function. + + ```lua + some({ a = 1, b = 2, c = 3 }, function(value) + return value > 2 + end) -- true + ``` +]=] +local function some(dictionary: { [K]: V }, predicate: (value: V, key: K) -> boolean): boolean + for key, value in dictionary do + if predicate(value, key) then + return true + end + end + + return false +end + +return some diff --git a/src/Dictionary/some.luau.todo b/src/Dictionary/some.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/Dictionary/withKeys.luau b/src/Dictionary/withKeys.luau new file mode 100644 index 0000000..8c27ee3 --- /dev/null +++ b/src/Dictionary/withKeys.luau @@ -0,0 +1,21 @@ +--[=[ + @within Dictionary + + Returns a new dictionary with only the specified keys. + + ```lua + local dict = { a = 1, b = 2, c = 3 } + withKeys(dict, "a", "c") -- { a = 1, c = 3 } + ``` +]=] +local function withKeys(dictionary: { [K]: V }, ...: K): { [K]: V } + local out = {} + + for _, key in { ... } do + out[key] = dictionary[key] + end + + return out +end + +return withKeys diff --git a/src/Dictionary/withKeys.luau.todo b/src/Dictionary/withKeys.luau.todo deleted file mode 100644 index e69de29..0000000 diff --git a/wally.toml b/wally.toml index e6588b6..0814065 100644 --- a/wally.toml +++ b/wally.toml @@ -4,8 +4,6 @@ description = "Immutable data library for Luau" version = "0.1.0" registry = "https://github.com/upliftgames/wally-index" realm = "shared" -exclude = ["**"] -include = ["src", "src/**", "wally.toml", "wally.lock", "default.project.json"] [dev-dependencies] Jest = "jsdotlua/jest@3.6.1-rc.2"