Skip to content

Commit

Permalink
fix: return type signatures for JSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Jan 22, 2025
1 parent 65df5fc commit c782a49
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dist/index.cjs",
"dist/index.global.js",
"dist/index.umd.js",
"dist/lib/**/*.d.ts",
"dist/lib/**/*",
"package.json",
"README-summary.md",
"CHANGELOG.md",
Expand Down
18 changes: 9 additions & 9 deletions lib/Debouncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,54 +61,54 @@ export class Debouncer<TArgs> extends NanoEmitter<DebouncerEventMap<TArgs>> {
//#region listeners

/** Adds a listener function that will be called on timeout */
public addListener(fn: DebouncerFunc<TArgs>) {
public addListener(fn: DebouncerFunc<TArgs>): void {
this.listeners.push(fn);
}

/** Removes the listener with the specified function reference */
public removeListener(fn: DebouncerFunc<TArgs>) {
public removeListener(fn: DebouncerFunc<TArgs>): void {
const idx = this.listeners.findIndex((l) => l === fn);
idx !== -1 && this.listeners.splice(idx, 1);
}

/** Removes all listeners */
public removeAllListeners() {
public removeAllListeners(): void {
this.listeners = [];
}

//#region timeout

/** Sets the timeout for the debouncer */
public setTimeout(timeout: number) {
public setTimeout(timeout: number): void {
this.emit("change", this.timeout = timeout, this.type);
}

/** Returns the current timeout */
public getTimeout() {
public getTimeout(): number {
return this.timeout;
}

/** Whether the timeout is currently active, meaning any latest call to the {@linkcode call()} method will be queued */
public isTimeoutActive() {
public isTimeoutActive(): boolean {
return typeof this.activeTimeout !== "undefined";
}

//#region type

/** Sets the edge type for the debouncer */
public setType(type: DebouncerType) {
public setType(type: DebouncerType): void {
this.emit("change", this.timeout, this.type = type);
}

/** Returns the current edge type */
public getType() {
public getType(): DebouncerType {
return this.type;
}

//#region call

/** Use this to call the debouncer with the specified arguments that will be passed to all listener functions registered with {@linkcode addListener()} */
public call(...args: TArgs[]) {
public call(...args: TArgs[]): void {
/** When called, calls all registered listeners */
const cl = (...a: TArgs[]) => {
this.queuedCall = undefined;
Expand Down
19 changes: 11 additions & 8 deletions lib/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function trFor<TTrKey extends string = string>(language: string, key: TTrKey, ..
* t("hello", "John"); // "Hello, John!"
* ```
*/
function useTr<TTrKey extends string = string>(language: string) {
function useTr<TTrKey extends string = string>(language: string): (key: TTrKey, ...args: (Stringifiable | Record<string, Stringifiable>)[]) => ReturnType<typeof trFor<TTrKey>> {
return (key: TTrKey, ...args: (Stringifiable | Record<string, Stringifiable>)[]) =>
translate<TTrKey>(language, key, ...args);
}
Expand Down Expand Up @@ -350,7 +350,7 @@ function deleteTransform(patternOrFn: RegExp | string | TransformFn): boolean {
* t("greeting", { user: "John" }); // "Hello, John!\nYou have ${notifs} notifications."
* ```
*/
const templateLiteralTransform = [
const templateLiteralTransform: TransformTuple<string> = [
/\$\{([a-zA-Z0-9$_-]+)\}/gm,
({ matches, trArgs, trValue }) => {
const patternStart = "${",
Expand Down Expand Up @@ -392,7 +392,7 @@ const templateLiteralTransform = [

return str;
},
] as const satisfies TransformTuple<string>;
] as const;

/**
* This transform will replace `%n` placeholders with the value of the passed arguments.
Expand All @@ -414,7 +414,7 @@ const templateLiteralTransform = [
* t("greeting", "John"); // "Hello, John!\nYou have %2 notifications."
* ```
*/
const percentTransform = [
const percentTransform: TransformTuple<string> = [
/\$\{([a-zA-Z0-9$_-]+)\}/gm,
({ matches, trArgs, trValue }) => {
let str = String(trValue);
Expand All @@ -427,14 +427,17 @@ const percentTransform = [

return str;
},
] as const satisfies TransformTuple<string>;
] as const;

//#region exports

const tr = {
for: <TTrKey extends string = string>(...params: Parameters<typeof trFor<TTrKey>>) => trFor<TTrKey>(...params as Parameters<typeof trFor<TTrKey>>),
use: <TTrKey extends string = string>(...params: Parameters<typeof useTr<TTrKey>>) => useTr<TTrKey>(...params as Parameters<typeof useTr<TTrKey>>),
hasKey: <TTrKey extends string = string>(language = fallbackLang ?? "", key: TTrKey) => hasKey<TTrKey>(language, key),
for: <TTrKey extends string = string>(...params: Parameters<typeof trFor<TTrKey>>): ReturnType<typeof trFor<TTrKey>> =>
trFor<TTrKey>(...params as Parameters<typeof trFor<TTrKey>>),
use: <TTrKey extends string = string>(...params: Parameters<typeof useTr<TTrKey>>): ReturnType<typeof useTr<TTrKey>> =>
useTr<TTrKey>(...params as Parameters<typeof useTr<TTrKey>>),
hasKey: <TTrKey extends string = string>(language = fallbackLang ?? "", key: TTrKey): ReturnType<typeof hasKey<TTrKey>> =>
hasKey<TTrKey>(language, key),
addTranslations,
getTranslations,
deleteTranslations,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"update-jsr-version": "node --import tsx ./tools/update-jsr-version.mts",
"publish-package": "changeset publish",
"publish-package-jsr": "pnpm update-jsr-version && npx jsr publish --allow-dirty",
"check-jsr": "npx jsr publish --allow-dirty --dry-run",
"change": "changeset",
"test-serve": "node --import tsx ./test/TestPage/server.mts",
"test-dev": "cd test/TestScript && pnpm dev",
Expand Down

0 comments on commit c782a49

Please sign in to comment.