mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-23 19:49:56 +01:00
common: add utils for debounce, random & string
This commit is contained in:
committed by
Abdullah Atta
parent
86a7fd9b93
commit
19eb512198
20
packages/common/src/hooks/index.ts
Normal file
20
packages/common/src/hooks/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./use-time-ago";
|
||||
@@ -18,10 +18,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./database";
|
||||
|
||||
export * from "./utils/file";
|
||||
export * from "./utils/number";
|
||||
export * from "./utils/total-notes";
|
||||
export * from "./utils/time";
|
||||
|
||||
export * from "./hooks/use-time-ago";
|
||||
export * from "./utils";
|
||||
export * from "./hooks";
|
||||
|
||||
71
packages/common/src/utils/debounce.ts
Normal file
71
packages/common/src/utils/debounce.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
interface DebouncedFunction<
|
||||
Args extends any[],
|
||||
F extends (...args: Args) => any
|
||||
> {
|
||||
(this: ThisParameterType<F>, ...args: Args & Parameters<F>): void;
|
||||
}
|
||||
|
||||
interface DebouncedFunctionWithId<
|
||||
Args extends any[],
|
||||
F extends (...args: Args) => any
|
||||
> {
|
||||
(
|
||||
this: ThisParameterType<F>,
|
||||
id: string | number,
|
||||
...args: Args & Parameters<F>
|
||||
): void;
|
||||
}
|
||||
|
||||
export function debounce<Args extends any[], F extends (...args: Args) => void>(
|
||||
func: F,
|
||||
waitFor: number
|
||||
): DebouncedFunction<Args, F> {
|
||||
let timeout: number | null;
|
||||
|
||||
return (...args: Args) => {
|
||||
if (timeout) clearTimeout(timeout);
|
||||
timeout = setTimeout(() => func(...args), waitFor) as unknown as number;
|
||||
};
|
||||
}
|
||||
|
||||
export function debounceWithId<
|
||||
Args extends any[],
|
||||
F extends (...args: Args) => void
|
||||
>(func: F, waitFor: number): DebouncedFunctionWithId<Args, F> {
|
||||
let timeout: number | null;
|
||||
let debounceId: string | number | null = null;
|
||||
|
||||
return (id: string | number, ...args: Parameters<F>) => {
|
||||
if (timeout && id === debounceId) clearTimeout(timeout);
|
||||
debounceId = id;
|
||||
timeout = setTimeout(() => {
|
||||
func(...args);
|
||||
}, waitFor) as unknown as number;
|
||||
};
|
||||
}
|
||||
|
||||
const DEBOUNCE_TIMEOUTS: Record<string, NodeJS.Timeout> = {};
|
||||
export function inlineDebounce(id: string, func: () => void, waitFor: number) {
|
||||
clearTimeout(DEBOUNCE_TIMEOUTS[id]);
|
||||
DEBOUNCE_TIMEOUTS[id] = setTimeout(func, waitFor);
|
||||
}
|
||||
26
packages/common/src/utils/index.ts
Normal file
26
packages/common/src/utils/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./file";
|
||||
export * from "./number";
|
||||
export * from "./total-notes";
|
||||
export * from "./time";
|
||||
export * from "./debounce";
|
||||
export * from "./random";
|
||||
export * from "./string";
|
||||
27
packages/common/src/utils/random.ts
Normal file
27
packages/common/src/utils/random.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
function getRandom(min: number, max: number) {
|
||||
return Math.round(Math.random() * (max - min) + min);
|
||||
}
|
||||
|
||||
function getRandomArbitrary(min: number, max: number) {
|
||||
return Math.random() * (max - min) + min;
|
||||
}
|
||||
export { getRandom, getRandomArbitrary };
|
||||
83
packages/common/src/utils/string.ts
Normal file
83
packages/common/src/utils/string.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export type TitleCase<
|
||||
T extends string,
|
||||
D extends string = " "
|
||||
> = string extends T
|
||||
? never
|
||||
: T extends `${infer F}${D}${infer R}`
|
||||
? `${Capitalize<F>}${D}${TitleCase<R, D>}`
|
||||
: Capitalize<T>;
|
||||
|
||||
type Separator = " " | "-" | "_";
|
||||
|
||||
export type CamelCase<T extends string> =
|
||||
T extends `${Separator}${infer Suffix}`
|
||||
? CamelCase<Suffix>
|
||||
: T extends `${infer Prefix}${Separator}`
|
||||
? CamelCase<Prefix>
|
||||
: T extends `${infer Prefix}${Separator}${infer Suffix}`
|
||||
? CamelCase<`${Prefix}${Capitalize<Suffix>}`>
|
||||
: T;
|
||||
|
||||
type KebabCaseHelper<
|
||||
S,
|
||||
Acc extends string = ""
|
||||
> = S extends `${infer C}${infer T}`
|
||||
? KebabCaseHelper<
|
||||
T extends Uncapitalize<T> ? T : Uncapitalize<T>,
|
||||
`${Acc}${Lowercase<C>}${T extends Uncapitalize<T> ? "" : "-"}`
|
||||
>
|
||||
: Acc;
|
||||
|
||||
export type KebabCase<S extends string> = KebabCaseHelper<S>;
|
||||
|
||||
const SingularPluralPairs = {
|
||||
note: "notes",
|
||||
notebook: "notebooks",
|
||||
topic: "topics",
|
||||
attachment: "attachments",
|
||||
reminder: "reminders",
|
||||
file: "files",
|
||||
tag: "tags",
|
||||
item: "items"
|
||||
};
|
||||
|
||||
export function pluralize(
|
||||
count: number | null | undefined | false,
|
||||
key: keyof typeof SingularPluralPairs
|
||||
): string {
|
||||
return !count || count > 1
|
||||
? `${count} ${SingularPluralPairs[key]}`
|
||||
: `${count} ${key}`;
|
||||
}
|
||||
|
||||
export function toTitleCase<T extends string>(str: T): TitleCase<T> | "" {
|
||||
if (!str || !str[0]) {
|
||||
return "";
|
||||
}
|
||||
return (str[0].toUpperCase() + str.substring(1)) as TitleCase<T>;
|
||||
}
|
||||
|
||||
export function toCamelCase<T extends string>(str: T): CamelCase<T> {
|
||||
return str.replace(/-(.{1})/gm, (_str, letter) => {
|
||||
return letter.toUpperCase();
|
||||
}) as CamelCase<T>;
|
||||
}
|
||||
Reference in New Issue
Block a user