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

151 lines
3.6 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);
}
};
}
// filters
export type ComparableObject = { [key: string]: string };
export function filterObjectsByStringEntries<T>(
reference: ComparableObject,
converter: (T) => ComparableObject,
objects: T[]
): any[] {
const matches: any[] = [];
object_loop: for (const object of objects) {
reference_entry_loop: for (const referenceEntry of Object.entries(
reference
)) {
const [referenceKey, referenceValue] = referenceEntry;
const comparableObject: ComparableObject = converter(object);
const comparableObjectValue: string = comparableObject[referenceKey];
if (referenceValue[0] == "-") {
const strippedReferenceValue: string = referenceValue.substring(1);
// property may not exist
if (
strippedReferenceValue == "" &&
comparableObjectValue != undefined &&
comparableObjectValue != ""
) {
continue object_loop;
}
// property may not match
if (comparableObjectValue == strippedReferenceValue) {
continue object_loop;
}
} else {
// property must exist but be anything
if (
referenceValue == "" &&
(comparableObjectValue == undefined || comparableObjectValue == "")
) {
continue object_loop;
} else if (referenceValue == "") {
continue reference_entry_loop;
}
// property must match
if (comparableObjectValue != referenceValue) {
continue object_loop;
}
}
}
matches.push(object);
}
return matches;
}
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);
}