A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
common: add utils for debounce, random & string
Abdullah Atta19eb512198b2
diff --git a/packages/common/src/hooks/index.ts b/packages/common/src/hooks/index.tsnew file mode 100644index 000000000..e6c2ba36b--- /dev/null+++ b/packages/common/src/hooks/index.ts@@ -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";diff --git a/packages/common/src/index.ts b/packages/common/src/index.tsindex 33c0564df..768b5a19a 100644--- a/packages/common/src/index.ts+++ b/packages/common/src/index.ts@@ -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";diff --git a/packages/common/src/utils/debounce.ts b/packages/common/src/utils/debounce.tsnew file mode 100644index 000000000..19658081d--- /dev/null+++ b/packages/common/src/utils/debounce.ts@@ -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);+}diff --git a/packages/common/src/utils/index.ts b/packages/common/src/utils/index.tsnew file mode 100644index 000000000..d2a7cd928--- /dev/null+++ b/packages/common/src/utils/index.ts@@ -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";diff --git a/packages/common/src/utils/random.ts b/packages/common/src/utils/random.tsnew file mode 100644index 000000000..f7949e4f5--- /dev/null+++ b/packages/common/src/utils/random.ts@@ -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 };diff --git a/packages/common/src/utils/string.ts b/packages/common/src/utils/string.tsnew file mode 100644index 000000000..3e8702b7a--- /dev/null+++ b/packages/common/src/utils/string.ts@@ -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>;+}