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

184 lines
4.3 KiB
TypeScript
Raw Normal View History

import { checkIsValidObject, checkMatchesObjectStructure } from "./typeSafety";
// date
export function createTimestamp(): string {
return new Date().toISOString();
}
// handlers
export type Handler<T> = (item: T) => void;
export class HandlerManager<T> {
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);
}
};
}
// objects
export type StringEntryObject = { [key: string]: string | undefined };
export function filterObjectsByStringEntries<T>(
reference: StringEntryObject,
converter: (T) => StringEntryObject,
objects: T[]
): Set<T> {
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);
}
return matches;
}
2024-07-25 01:50:49 +02:00
export function checkDoesObjectMatchReference(
reference: StringEntryObject,
stringEntryObject: StringEntryObject
): boolean {
reference_entry_loop: for (const referenceEntry of Object.entries(
reference
)) {
const [referenceKey, referenceValue] = referenceEntry;
const stringEntryObjectValue: string | undefined =
stringEntryObject[referenceKey];
if (referenceValue == undefined) return false;
if (referenceValue[0] == "-") {
const strippedReferenceValue: string = referenceValue.substring(1);
// property may not exist
if (
strippedReferenceValue == "" &&
stringEntryObjectValue != undefined &&
stringEntryObjectValue != ""
) {
return false;
}
// property may not match
if (stringEntryObjectValue == strippedReferenceValue) {
return false;
}
} else {
// property must exist but be anything
if (
referenceValue == "" &&
(stringEntryObjectValue == undefined || stringEntryObjectValue == "")
) {
return false;
} else if (referenceValue == "") {
continue reference_entry_loop;
}
// property must match
if (stringEntryObjectValue != referenceValue) {
return false;
}
}
}
return true;
}
export function collectObjectValuesForKey<T>(
key: string,
converter: (object: T) => StringEntryObject,
objects: T[]
): string[] {
const values: Set<string> = new Set();
for (const object of objects) {
const stringEntryObject: StringEntryObject = converter(object);
const stringEntryObjectValue: string | undefined = stringEntryObject[key];
if (stringEntryObjectValue == undefined || stringEntryObjectValue == "")
continue;
values.add(stringEntryObjectValue);
}
return [...values.values()].sort(localeCompare);
}
2024-07-23 14:15:01 +02:00
// sorting
export class IndexManager<T> {
private itemToString: (item: T) => string;
sortedStrings: string[] = [];
// methods
update = (items: T[]): void => {
this.sortedStrings = [];
let strings: string[] = [];
for (const item of items) {
const string: string = this.itemToString(item);
strings.push(string);
}
this.sortedStrings = strings.sort(localeCompare);
};
getIndex = (item: T): number => {
const string: string = this.itemToString(item);
const index: number = this.sortedStrings.indexOf(string);
return index;
};
// init
constructor(itemToString: (item: T) => string) {
this.itemToString = itemToString;
}
}
// string
2024-07-19 17:56:18 +02:00
export function stringify(data: any): string {
2024-07-20 20:42:43 +02: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 {
return (string ?? "").padStart(length, "0");
}
2024-07-19 17:56:18 +02:00
export function parse(string: string): any {
try {
return JSON.parse(string);
} catch {
return {};
}
}
export function parseValidObject<T>(string: string, reference: T): T | null {
const parsed: any = parse(string);
if (checkIsValidObject(parsed) == false) return null;
const doesMatchReference: boolean = checkMatchesObjectStructure(
parsed,
reference
);
if (doesMatchReference == false) return null;
return parsed;
}
2024-07-20 18:58:25 +02:00
// sort
export function localeCompare(a: string, b: string): number {
return a.localeCompare(b);
}