Skip to content

Commit

Permalink
feat(dictionary): add withKeys (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: csqrl <[email protected]>
  • Loading branch information
sasial-dev and cxmeel authored Apr 30, 2023
1 parent 57e8dfb commit e98b5db
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.0.6]

### Added

- `Dictionary.withKeys` by @sasial-dev to restrict what keys can appear in a given dictionary.

## [0.0.5]

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rbxts/sift",
"version": "0.0.5",
"version": "0.0.6",
"description": "Immutable data library for Luau",
"main": "out/init.lua",
"types": "out/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion src/Dictionary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ObjectFromKeyValueArrays,
ObjectKey,
ReadonlyDeep,
TryIndex,
TryIndex
} from "./Util"

declare namespace SiftDictionary {
Expand Down Expand Up @@ -154,6 +154,11 @@ declare namespace SiftDictionary {

export function values<T extends object>(dictionary: T): T[keyof T][]

export function withKeys<T extends object, K extends keyof T>(
dictionary: T,
...keys: K[]
): Pick<T, K>

// Aliases
export { merge as join, mergeDeep as joinDeep }
}
Expand Down
1 change: 1 addition & 0 deletions src/Dictionary/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ local Dictionary = {
some = require(script.some),
update = require(script.update),
values = require(script.values),
withKeys = require(script.withKeys),
}

Dictionary.join = Dictionary.merge
Expand Down
29 changes: 29 additions & 0 deletions src/Dictionary/withKeys.lua
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
29 changes: 29 additions & 0 deletions src/Dictionary/withKeys.spec.lua
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
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "csqrl/sift"
description = "Immutable data library for Luau"
version = "0.0.5"
version = "0.0.6"
license = "MIT"
author = "csqrl (https://csqrl.dev)"
registry = "https://github.com/upliftgames/wally-index"
Expand Down

0 comments on commit e98b5db

Please sign in to comment.