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.
feat(dictionary): add withKeys (#13)
Co-authored-by: csqrl <[email protected]>
- Loading branch information
1 parent
57e8dfb
commit e98b5db
Showing
7 changed files
with
73 additions
and
3 deletions.
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
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
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,29 @@ | ||
--!strict | ||
|
||
--[=[ | ||
@function withKeys | ||
@within Dictionary | ||
@param dictionary {[K]: V} -- The dictionary to select the keys from. | ||
@param keys ...K -- The keys to keep. | ||
@return {[K]: V} -- The dictionary with only the given keys. | ||
Returns a dictionary with the given keys. | ||
```lua | ||
local dictionary = { hello = "world", cat = "meow", dog = "woof", unicorn = "rainbow" } | ||
local withoutCatDog = WithKeys(dictionary, "cat", "dog") -- { cat = "meow", dog = "woof" } | ||
``` | ||
]=] | ||
local function withKeys<K, V>(dictionary: { [K]: V }, ...: K): { [K]: V } | ||
local result = {} | ||
|
||
for _, key in ipairs({ ... }) do | ||
result[key] = dictionary[key] | ||
end | ||
|
||
return result | ||
end | ||
|
||
return withKeys |
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,29 @@ | ||
return function() | ||
local WithKeys = require(script.Parent.withKeys) | ||
|
||
it("should return a new dictionary with the given keys kept", function() | ||
local dictionary = { hello = "world", cat = "meow", dog = "woof", unicorn = "rainbow" } | ||
|
||
local newDictionary = WithKeys(dictionary, "cat", "dog") | ||
|
||
expect(newDictionary).to.be.a("table") | ||
|
||
expect(newDictionary.hello).to.equal(nil) | ||
expect(newDictionary.cat).to.equal("meow") | ||
expect(newDictionary.dog).to.equal("woof") | ||
expect(newDictionary.unicorn).to.equal(nil) | ||
end) | ||
|
||
it("should not modify the original dictionary", function() | ||
local dictionary = { hello = "world", cat = "meow", dog = "woof", unicorn = "rainbow" } | ||
|
||
WithKeys(dictionary, "cat", "dog") | ||
|
||
expect(dictionary).to.be.a("table") | ||
|
||
expect(dictionary.hello).to.equal("world") | ||
expect(dictionary.cat).to.equal("meow") | ||
expect(dictionary.dog).to.equal("woof") | ||
expect(dictionary.unicorn).to.equal("rainbow") | ||
end) | ||
end |
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