Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/types #290

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type CPSFn<
Callbacks extends Array<(...args: unknown[]) => void>
> = (...args: Callbacks) => void

type ChainFns<
FromCallbacks extends Array<(...args: unknown[]) => void>,
ToCallbacks extends Array<(...args: unknown[]) => void>,
> = {
[T in keyof FromCallbacks]?: ((...args: Parameters<FromCallbacks[T]>) => CPSFn<ToCallbacks>) | undefined
}

type CPS<
Callbacks extends Array<(...args: unknown[]) => void>
> = CPSFn<Callbacks> & {
chain<
ToCallbacks extends Array<(...args: unknown[]) => void>
>(...fns: ChainFns<Callbacks, ToCallbacks>): CPS<ToCallbacks>
}

declare function CPS<
Callbacks extends Array<(...args: unknown[]) => void>
>(fn: CPSFn<Callbacks>): CPS<Callbacks>

Comment on lines +20 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Declaration: CPS

This function declaration is essential for creating CPS instances. However, the static analysis tool flagged a potential redeclaration issue.

Consider renaming this function or ensuring that it does not conflict with other declarations in the codebase.

-declare function CPS<
+declare function createCPS<
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
declare function CPS<
Callbacks extends Array<(...args: unknown[]) => void>
>(fn: CPSFn<Callbacks>): CPS<Callbacks>
declare function createCPS<
Callbacks extends Array<(...args: unknown[]) => void>
>(fn: CPSFn<Callbacks>): CPS<Callbacks>
Tools
Biome

[error] 20-20: Shouldn't redeclare 'CPS'. Consider to delete it or rename it.

'CPS' is defined here:

(lint/suspicious/noRedeclare)

export { CPS, CPSFn }
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"name": "cpsfy",
"description": "Tiny goodies for Continuation-Passing-Style functions",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"utils.js"
],
"repository": "https://github.com/dmitriz/cpsfy.git",
Expand Down
35 changes: 35 additions & 0 deletions type-test/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CPS, type CPSFn } from "../index"

const cpsFn: CPSFn<[
(a: number, b: number) => void,
(a: number, b: string) => void,
]> = (cb1, cb2) => { cb1(2,3); cb2(7, "bar") }

const f1 = (x: number, y: number): CPSFn<[
(x: number, y?: string) => void,
(x: number) => void,
]> => (cb1, cb2) => { cb1(x+y); cb2(x-y) }

const f2 = (x: number, y: string): CPSFn<[
(x: number, y?: string) => void,
]> => (cb1) => { cb1(x, y) }

const chainOneWay = CPS(cpsFn).chain(f1, f2)

const chainSecondWay = CPS(cpsFn).chain<[
(x: number, y?: string) => void,
(x: number) => void,
]>(
(x, y) => (cb1, cb2) => { cb1(x+y); cb2(x-y) },
(x, y) => (cb1) => { cb1(x, y) }
)

const chainThirdWay = CPS(cpsFn).chain(
(x: number, y: number) => (
cb1: (x: number, y?: string) => void,
cb2: (x: number) => void
) => { cb1(x+y); cb2(x-y) },
(x: number, y: string) => (
cb1: (x: number, y?: string) => void,
) => { cb1(x, y) }
)