diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml deleted file mode 100644 index 72f49aa..0000000 --- a/.idea/jsLibraryMappings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/packages/backend/src/util/functions.ts b/packages/backend/src/util/functions.ts index 852bc5c..5f28d65 100644 --- a/packages/backend/src/util/functions.ts +++ b/packages/backend/src/util/functions.ts @@ -1,4 +1,42 @@ +/* + * Copyright (C) 2024 Aleksander WilczyƄski (aleks@alekswilc.dev) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * See LICENSE for more. + */ + +// TODO: typings +export const removeProperties = (data: any, names: string[]) => +{ + for (const name of names) + { + delete data[ name ]; + } + return data as T; +}; + + // https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex export const escapeRegexString = (str: string) => { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); -} \ No newline at end of file +} + +export const isTruthyAndGreaterThanZero = (data: number) => { + if (!data) return false; + return data > 0; +} + +export const arrayGroupBy = (array: T[], predicate: (value: T, index: number, array: T[]) => string) => + Object.values((array.reduce((acc, value, index, array) => { + (acc[predicate(value, index, array)] ||= []).push(value); + return acc; + }, {} as { [key: string]: T[] }))).flat(); \ No newline at end of file