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

Commit

Permalink
Fix Dictionary.update type (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: cxmeel <[email protected]>
  • Loading branch information
brinkokevin and cxmeel authored May 2, 2024
1 parent dcdb17c commit 139e24a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 57 deletions.
5 changes: 0 additions & 5 deletions .husky/pre-commit

This file was deleted.

4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
Expand Down
26 changes: 2 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rbxts/sift",
"version": "0.0.8",
"version": "0.0.9",
"description": "Immutable data library for Luau",
"main": "out/init.lua",
"types": "out/index.d.ts",
Expand All @@ -10,8 +10,7 @@
"scripts": {
"build": "rbxtsc --type=package --rojo=''",
"watch": "rbxtsc -w",
"prepublishOnly": "npm run build",
"prepare": "husky install"
"prepublishOnly": "npm run build"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,8 +40,7 @@
"@rbxts/types": "^1.0.515",
"rbxts-transform-debug": "^1.0.0-rc.1",
"rbxts-transform-env": "^1.0.0-rc.0",
"typescript": "^4.3.5",
"roblox-ts": "^1.2.7",
"husky": "^8.0.0"
"typescript": "^4.3.5"
}
}
25 changes: 8 additions & 17 deletions src/Dictionary/update.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
--!strict
local copy = require(script.Parent.copy)

type Callback<K, V> = (key: K) -> V
type Updater<K, V> = (value: V, key: K) -> V

local function call<K, V>(callback: Callback<K, V>, key: K)
if type(callback) == "function" then
return callback(key)
end
end

--[=[
@function update
@within Dictionary
@param dictionary {[K]: V} -- The dictionary to update.
@param dictionary {[K]: V?} -- The dictionary to update.
@param key K -- The key to update.
@param updater? (value: V, key: K) -> U -- The updater function.
@param callback? (key: K) -> C -- The callback function.
@return {[K]: V & U & C } -- The updated dictionary.
@return {[K]: V | U | C} -- The updated dictionary.
Updates a value in a dictionary at the given key. If the value at the given key does not exist, `callback` will be called, and its return value will be used as the value at the given key.
Expand All @@ -37,20 +28,20 @@ end
```
]=]
local function update<K, V, U, C>(
dictionary: { [K]: V },
dictionary: { [K]: V? },
key: K,
updater: ((value: V, key: K) -> U)?,
callback: ((key: K) -> C)?
): { [K]: V & U & C }
local result = copy(dictionary)
): { [K]: V | U | C }
local result: { [K]: any } = copy(dictionary)

if result[key] ~= nil then
if result[key] then
if updater then
result[key] = updater(result[key], key)
end
else
if callback then
result[key] = call(callback, key)
if typeof(callback) == "function" then
result[key] = callback(key)
end
end

Expand Down
5 changes: 1 addition & 4 deletions wally.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
[package]
name = "csqrl/sift"
description = "Immutable data library for Luau"
version = "0.0.8"
version = "0.0.9"
license = "MIT"
author = "csqrl (https://csqrl.dev)"
registry = "https://github.com/upliftgames/wally-index"
homepage = "https://csqrl.github.io/sift"
repo = "https://github.com/csqrl/sift"
realm = "shared"
exclude = ["**"]
include = ["src", "src/**", "wally.toml", "wally.lock", "default.project.json"]

0 comments on commit 139e24a

Please sign in to comment.