Files
CT/src/Model/Utility/utility.ts
T

282 lines
7.4 KiB
TypeScript
Raw Normal View History

import { checkIsValidObject, checkMatchesObjectStructure } from "./typeSafety";
export interface Stringifiable {
2026-04-01 08:29:39 +00:00
toString(): string;
}
2024-07-27 17:41:54 +02:00
// crypto
export function generateRandomToken(length: number): string {
2026-04-01 08:29:39 +00:00
const array = new Uint8Array(length);
crypto.getRandomValues(array);
const string = array.join("");
return string.substring(0, length);
2024-07-27 17:41:54 +02:00
}
// date
export function createTimestamp(): string {
2026-04-01 08:29:39 +00:00
return new Date().toISOString();
}
2024-07-25 14:15:00 +02:00
// filters etc
export type StringEntryObject = { [key: string]: Stringifiable | undefined };
export function filterObjectsByStringEntries<T>(
2026-04-01 08:29:39 +00:00
reference: StringEntryObject,
converter: (object: T) => StringEntryObject,
objects: T[],
): Set<T> {
2026-04-01 08:29:39 +00:00
const matches: Set<T> = new Set();
object_loop: for (const object of objects) {
const doesMatch: boolean = checkDoesObjectMatchReference(
reference,
converter(object),
);
if (doesMatch) matches.add(object);
}
2026-04-01 08:29:39 +00:00
return matches;
}
2024-07-25 01:50:49 +02:00
export function checkDoesObjectMatchReference(
2026-04-01 08:29:39 +00:00
reference: StringEntryObject,
stringEntryObject: StringEntryObject,
explicitEmptyValue: boolean = false,
): boolean {
2026-04-01 08:29:39 +00:00
reference_entry_loop: for (const referenceEntry of Object.entries(
reference,
)) {
const [referenceKey, referenceValue] = referenceEntry;
const stringEntryObjectValue: Stringifiable | undefined =
stringEntryObject[referenceKey];
if (referenceValue == undefined) return false;
if (referenceValue[0] == "-") {
const strippedReferenceValue: string = referenceValue
.toString()
.substring(1);
// property may not exist
if (
strippedReferenceValue == "" &&
stringEntryObjectValue != undefined &&
stringEntryObjectValue != ""
) {
return false;
}
// property may not match
if (stringEntryObjectValue == strippedReferenceValue) {
return false;
}
} else {
if (explicitEmptyValue == false) {
// property must exist but be anything
if (
referenceValue == "" &&
(stringEntryObjectValue == undefined ||
stringEntryObjectValue == "")
) {
return false;
} else if (referenceValue == "") {
continue reference_entry_loop;
}
2026-04-01 08:29:39 +00:00
}
// property must match
if (stringEntryObjectValue != referenceValue) {
return false;
}
}
}
2026-04-01 08:29:39 +00:00
return true;
}
export function collectObjectValuesForKey<T>(
2026-04-01 08:29:39 +00:00
key: string,
converter: (object: T) => StringEntryObject,
objects: T[],
): string[] {
2026-04-01 08:29:39 +00:00
const values: Set<string> = new Set();
2026-04-01 08:29:39 +00:00
for (const object of objects) {
const stringEntryObject: StringEntryObject = converter(object);
const stringEntryObjectValue: Stringifiable | undefined =
stringEntryObject[key];
if (stringEntryObjectValue == undefined) continue;
2026-04-01 08:29:39 +00:00
values.add(stringEntryObjectValue.toString());
}
2026-04-01 08:29:39 +00:00
return [...values.values()];
}
2024-07-25 14:15:00 +02:00
export function filterObjectsByWords<T>(
2026-04-01 08:29:39 +00:00
query: string,
getStringsOfObject: (object: T) => string[],
objects: T[],
2024-07-25 14:15:00 +02:00
): Set<T> {
2026-04-01 08:29:39 +00:00
const matches: Set<T> = new Set();
object_loop: for (const object of objects) {
const doesMatch: boolean = checkDoesObjectMatchSearch(
query,
getStringsOfObject,
object,
);
if (doesMatch) matches.add(object);
}
2024-07-25 14:15:00 +02:00
2026-04-01 08:29:39 +00:00
return matches;
2024-07-25 14:15:00 +02:00
}
2024-07-25 15:31:27 +02:00
export function checkDoesObjectMatchSearch<T>(
2026-04-01 08:29:39 +00:00
query: string,
getStringsOfObject: (object: T) => string[],
object: T,
2024-07-25 14:15:00 +02:00
): boolean {
2026-04-01 08:29:39 +00:00
if (query == "") return true;
const stringsInObject: string[] = getStringsOfObject(object);
const wordsInObject: string[] = [];
for (const string of stringsInObject) {
const lowercaseWordsInString = string
.toString()
.toLowerCase()
.split(" ")
.filter((word) => word != "");
wordsInObject.push(...lowercaseWordsInString);
}
const lowercaseWordsInQuery = query
.toLowerCase()
.split(" ")
.filter((word) => word != "");
for (const queryWord of lowercaseWordsInQuery) {
if (queryWord[0] == "-") {
// exclusion
const wordContent = queryWord.substring(1);
if (wordsInObject.includes(wordContent)) {
return false;
}
} else {
if (wordsInObject.includes(queryWord) == false) {
return false;
}
}
2024-07-25 14:15:00 +02:00
}
2026-04-01 08:29:39 +00:00
return true;
2024-07-25 14:15:00 +02:00
}
// handlers
export type Handler<T> = (item: T) => void;
export class HandlerManager<T> {
2026-04-01 08:29:39 +00:00
handlers: Set<Handler<T>> = new Set();
// manage
addHandler = (handler: Handler<T>): void => {
this.handlers.add(handler);
};
deleteHandler = (handler: Handler<T>): void => {
this.handlers.delete(handler);
};
// trigger
trigger = (item: T): void => {
for (const handler of this.handlers) {
handler(item);
}
};
2024-07-25 14:15:00 +02:00
}
2024-07-23 14:15:01 +02:00
// sorting
export class IndexManager<T> {
2026-04-01 08:29:39 +00:00
private itemToString: (item: T) => string;
2024-07-23 14:15:01 +02:00
2026-04-01 08:29:39 +00:00
sortedStrings: string[] = [];
2024-07-23 14:15:01 +02:00
2026-04-01 08:29:39 +00:00
// methods
update = (items: T[]): void => {
this.sortedStrings = [];
2024-07-23 14:15:01 +02:00
2026-04-01 08:29:39 +00:00
let strings: string[] = [];
for (const item of items) {
const string: string = this.itemToString(item);
strings.push(string);
}
2024-07-23 14:15:01 +02:00
2026-04-01 08:29:39 +00:00
this.sortedStrings = strings.sort(localeCompare);
};
2024-07-23 14:15:01 +02:00
2026-04-01 08:29:39 +00:00
getIndex = (item: T): number => {
const string: string = this.itemToString(item);
const index: number = this.sortedStrings.indexOf(string);
return index;
};
2024-07-23 14:15:01 +02:00
2026-04-01 08:29:39 +00:00
// init
constructor(itemToString: (item: T) => string) {
this.itemToString = itemToString;
}
2024-07-23 14:15:01 +02:00
}
// storage
export function getLocalStorageItemAndClear(key: string): string | null {
2026-04-01 08:29:39 +00:00
const value: string | null = localStorage.getItem(key);
localStorage.removeItem(key);
if (value != null) localStorage.setItem(`_${key}`, value);
return value;
}
// string & parsing
2024-07-19 17:56:18 +02:00
export function stringify(data: any): string {
2026-04-01 08:29:39 +00:00
return JSON.stringify(data, null, 4);
2024-07-19 17:56:18 +02:00
}
2024-07-24 14:34:28 +02:00
export function padZero(string: string | undefined, length: number): string {
2026-04-01 08:29:39 +00:00
return (string ?? "").padStart(length, "0");
2024-07-24 14:34:28 +02:00
}
2024-07-19 17:56:18 +02:00
export function parse(string: string): any {
2026-04-01 08:29:39 +00:00
try {
return JSON.parse(string);
} catch {
return {};
}
}
export function parseValidObject<T>(string: string, reference: T): T | null {
2026-04-01 08:29:39 +00:00
const parsed: any = parse(string);
if (checkIsValidObject(parsed) == false) return null;
2026-04-01 08:29:39 +00:00
const doesMatchReference: boolean = checkMatchesObjectStructure(
parsed,
reference,
);
if (doesMatchReference == false) return null;
2026-04-01 08:29:39 +00:00
return parsed;
}
2024-07-20 18:58:25 +02:00
export function parseOrFallback(inputString: string): any {
2026-04-01 08:29:39 +00:00
try {
return JSON.parse(inputString);
} catch {
return inputString;
}
}
export function parseArray(inputString: string): any[] {
2026-04-01 08:29:39 +00:00
const parsed: any = parseOrFallback(inputString);
if (Array.isArray(parsed) == false) return [];
return parsed;
}
2024-07-20 18:58:25 +02:00
// sort
export function localeCompare(a: string, b: string): number {
2026-04-01 08:29:39 +00:00
return a.localeCompare(b);
}