/* * Deepkit Framework * Copyright (c) Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ import { stringify, v4 } from 'uuid'; export class NoTypeReceived extends Error { constructor(message: string = 'No type information received. Circular import or no runtime type available.') { super(message); } } /** * Returns a new UUID v4 as string. */ export function uuid(): string { return v4(); } /** * Writes a new uuid v4 into an existing buffer, and returns the same buffer. */ export function writeUuid(buffer: Uint8Array, offset: number = 0): Uint8Array { v4(undefined, buffer, offset); return buffer; } /** * Stringify an exising Uint8Array buffer. */ export function stringifyUuid(buffer: Uint8Array, offset: number = 0): string { return stringify(buffer, offset); } export type Binary = ArrayBuffer | Uint8Array | Int8Array | Uint8ClampedArray | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array; export type JSONPartial = T extends Date ? string : T extends Array ? Array> : // T extends TypedArrays ? string : T extends Binary ? string : T extends object ? JSONPartialObject : T extends string ? number | T : T extends boolean ? number | string | T : T extends bigint ? number | string | T : T extends number ? bigint | string | T : T; export type JSONPartialObject = { [name in keyof T]?: T[name] | null }; export type JSONSingle = T extends Date ? string | Date : T extends Array ? Array> : T extends Binary ? string : T extends object ? JSONEntity : T extends string ? string | number | boolean | undefined : T extends boolean ? T | number | string : T extends number ? T | string : T; export type JSONEntity = { [name in keyof T]: JSONSingle }; // export type AnyEntitySingle = // T extends Array ? Array> : // T extends TypedArrays ? any : // T extends ArrayBuffer ? any : // T extends object ? AnyEntity : // T extends string ? any : // T extends boolean ? any : // T extends number ? any : any; // export type AnyEntity = { [name in keyof T & string]: AnyEntitySingle> }; // // export type JSONPatch = { [name in keyof T & string]: JSONSingle } | { [name: string]: any }; // // export type FlattenIfArray = T extends Array ? T[0] : T; // // export type ExtractClassType = T extends ClassType ? K : // T extends ClassSchema ? K : A; // // export type PlainOrFullEntityFromClassTypeOrSchema = { [name: string]: any } | JSONPartial> | ExtractClassType; export function regExpFromString(v: string): RegExp { if (v[0] === '/') { const end = v.lastIndexOf('/'); const regexp = v.slice(1, end); const modifiers = v.slice(1 + end); return new RegExp(regexp, modifiers); } return new RegExp(v); }