added format command and added trailing commas to reduce conflicts

This commit is contained in:
dswbx
2025-02-26 20:06:03 +01:00
parent 88b5359f1c
commit 7743f71a11
414 changed files with 3622 additions and 3610 deletions

View File

@@ -20,7 +20,7 @@ export const hash = {
sha256: async (input: string, salt?: string, pepper?: string) =>
digest("SHA-256", input, salt, pepper),
sha1: async (input: string, salt?: string, pepper?: string) =>
digest("SHA-1", input, salt, pepper)
digest("SHA-1", input, salt, pepper),
};
export async function checksum(s: any) {

View File

@@ -14,7 +14,7 @@ export function isObject(value: unknown): value is Record<string, unknown> {
export function omitKeys<T extends object, K extends keyof T>(
obj: T,
keys_: readonly K[]
keys_: readonly K[],
): Omit<T, Extract<K, keyof T>> {
const keys = new Set(keys_);
const result = {} as Omit<T, Extract<K, keyof T>>;
@@ -47,7 +47,7 @@ export function keepChanged<T extends object>(origin: T, updated: T): Partial<T>
}
return acc;
},
{} as Partial<T>
{} as Partial<T>,
);
}
@@ -66,13 +66,13 @@ export function objectKeysPascalToKebab(obj: any, ignoreKeys: string[] = []): an
acc[kebabKey] = objectKeysPascalToKebab(obj[key], ignoreKeys);
return acc;
},
{} as Record<string, any>
{} as Record<string, any>,
);
}
export function filterKeys<Object extends { [key: string]: any }>(
obj: Object,
keysToFilter: string[]
keysToFilter: string[],
): Object {
const result = {} as Object;
@@ -92,7 +92,7 @@ export function filterKeys<Object extends { [key: string]: any }>(
export function transformObject<T extends Record<string, any>, U>(
object: T,
transform: (value: T[keyof T], key: keyof T) => U | undefined
transform: (value: T[keyof T], key: keyof T) => U | undefined,
): { [K in keyof T]: U } {
return Object.entries(object).reduce(
(acc, [key, value]) => {
@@ -102,20 +102,20 @@ export function transformObject<T extends Record<string, any>, U>(
}
return acc;
},
{} as { [K in keyof T]: U }
{} as { [K in keyof T]: U },
);
}
export const objectTransform = transformObject;
export function objectEach<T extends Record<string, any>, U>(
object: T,
each: (value: T[keyof T], key: keyof T) => U
each: (value: T[keyof T], key: keyof T) => U,
): void {
Object.entries(object).forEach(
([key, value]) => {
each(value, key);
},
{} as { [K in keyof T]: U }
{} as { [K in keyof T]: U },
);
}
@@ -291,7 +291,7 @@ export function isEqual(value1: any, value2: any): boolean {
return "plainObject";
if (value instanceof Function) return "function";
throw new Error(
`deeply comparing an instance of type ${value1.constructor?.name} is not supported.`
`deeply comparing an instance of type ${value1.constructor?.name} is not supported.`,
);
};
@@ -336,7 +336,7 @@ export function isEqual(value1: any, value2: any): boolean {
export function getPath(
object: object,
_path: string | (string | number)[],
defaultValue = undefined
defaultValue = undefined,
): any {
const path = typeof _path === "string" ? _path.split(/[.\[\]\"]+/).filter((x) => x) : _path;

View File

@@ -161,11 +161,11 @@ const FILE_SIGNATURES: Record<string, string> = {
FFF9: "audio/aac",
"52494646????41564920": "audio/wav",
"52494646????57415645": "audio/wave",
"52494646????415550": "audio/aiff"
"52494646????415550": "audio/aiff",
};
async function detectMimeType(
input: ReadableStream | ArrayBuffer | ArrayBufferView | string | Blob | File | null
input: ReadableStream | ArrayBuffer | ArrayBufferView | string | Blob | File | null,
): Promise<string | undefined> {
if (!input) return;
@@ -202,7 +202,7 @@ async function detectMimeType(
export async function blobToFile(
blob: Blob | File | unknown,
overrides: FilePropertyBag & { name?: string } = {}
overrides: FilePropertyBag & { name?: string } = {},
): Promise<File> {
if (isFile(blob)) return blob;
if (!isBlob(blob)) throw new Error("Not a Blob");
@@ -215,7 +215,7 @@ export async function blobToFile(
return new File([blob], name, {
type: type || guess(name),
lastModified: Date.now()
lastModified: Date.now(),
});
}
@@ -340,5 +340,5 @@ export const enum HttpStatus {
INSUFFICIENT_STORAGE = 507,
LOOP_DETECTED = 508,
NOT_EXTENDED = 510,
NETWORK_AUTHENTICATION_REQUIRED = 511
NETWORK_AUTHENTICATION_REQUIRED = 511,
}

View File

@@ -28,7 +28,7 @@ export function getRuntimeKey(): string {
const features = {
// supports the redirect of not full qualified addresses
// not supported in nextjs
redirects_non_fq: true
redirects_non_fq: true,
};
export function runtimeSupports(feature: keyof typeof features) {

View File

@@ -2,17 +2,17 @@ type ConsoleSeverity = "log" | "warn" | "error";
const _oldConsoles = {
log: console.log,
warn: console.warn,
error: console.error
error: console.error,
};
export async function withDisabledConsole<R>(
fn: () => Promise<R>,
severities: ConsoleSeverity[] = ["log", "warn", "error"]
severities: ConsoleSeverity[] = ["log", "warn", "error"],
): Promise<R> {
const _oldConsoles = {
log: console.log,
warn: console.warn,
error: console.error
error: console.error,
};
disableConsoleLog(severities);
const enable = () => {
@@ -57,6 +57,6 @@ export function formatMemoryUsage() {
rss: usage.rss / 1024 / 1024,
heapUsed: usage.heapUsed / 1024 / 1024,
external: usage.external / 1024 / 1024,
arrayBuffers: usage.arrayBuffers / 1024 / 1024
arrayBuffers: usage.arrayBuffers / 1024 / 1024,
};
}

View File

@@ -56,6 +56,7 @@ const IsSArray = (value: unknown): value is SArray =>
!Type.ValueGuard.IsArray(value.items) &&
Type.ValueGuard.IsObject(value.items);
const IsSConst = (value: unknown): value is SConst =>
// biome-ignore lint/complexity/useLiteralKeys: <explanation>
Type.ValueGuard.IsObject(value) && Type.ValueGuard.IsObject(value["const"]);
const IsSString = (value: unknown): value is SString =>
Type.ValueGuard.IsObject(value) && IsExact(value.type, "string");
@@ -68,7 +69,7 @@ const IsSBoolean = (value: unknown): value is SBoolean =>
const IsSNull = (value: unknown): value is SBoolean =>
Type.ValueGuard.IsObject(value) && IsExact(value.type, "null");
const IsSProperties = (value: unknown): value is SProperties => Type.ValueGuard.IsObject(value);
// prettier-ignore
// biome-ignore format: keep
const IsSObject = (value: unknown): value is SObject => Type.ValueGuard.IsObject(value) && IsExact(value.type, 'object') && IsSProperties(value.properties) && (value.required === undefined || Type.ValueGuard.IsArray(value.required) && value.required.every((value: unknown) => Type.ValueGuard.IsString(value)))
type SValue = string | number | boolean;
type SEnum = Readonly<{ enum: readonly SValue[] }>;
@@ -88,8 +89,9 @@ type SNull = Readonly<{ type: "null" }>;
// ------------------------------------------------------------------
// FromRest
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromRest<T extends readonly unknown[], Acc extends Type.TSchema[] = []> = (
// biome-ignore lint/complexity/noUselessTypeConstraint: <explanation>
T extends readonly [infer L extends unknown, ...infer R extends unknown[]]
? TFromSchema<L> extends infer S extends Type.TSchema
? TFromRest<R, [...Acc, S]>
@@ -102,7 +104,7 @@ function FromRest<T extends readonly unknown[]>(T: T): TFromRest<T> {
// ------------------------------------------------------------------
// FromEnumRest
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromEnumRest<T extends readonly SValue[], Acc extends Type.TSchema[] = []> = (
T extends readonly [infer L extends SValue, ...infer R extends SValue[]]
? TFromEnumRest<R, [...Acc, Type.TLiteral<L>]>
@@ -114,7 +116,7 @@ function FromEnumRest<T extends readonly SValue[]>(T: T): TFromEnumRest<T> {
// ------------------------------------------------------------------
// AllOf
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromAllOf<T extends SAllOf> = (
TFromRest<T['allOf']> extends infer Rest extends Type.TSchema[]
? Type.TIntersectEvaluated<Rest>
@@ -126,7 +128,7 @@ function FromAllOf<T extends SAllOf>(T: T): TFromAllOf<T> {
// ------------------------------------------------------------------
// AnyOf
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromAnyOf<T extends SAnyOf> = (
TFromRest<T['anyOf']> extends infer Rest extends Type.TSchema[]
? Type.TUnionEvaluated<Rest>
@@ -138,7 +140,7 @@ function FromAnyOf<T extends SAnyOf>(T: T): TFromAnyOf<T> {
// ------------------------------------------------------------------
// OneOf
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromOneOf<T extends SOneOf> = (
TFromRest<T['oneOf']> extends infer Rest extends Type.TSchema[]
? Type.TUnionEvaluated<Rest>
@@ -150,7 +152,7 @@ function FromOneOf<T extends SOneOf>(T: T): TFromOneOf<T> {
// ------------------------------------------------------------------
// Enum
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromEnum<T extends SEnum> = (
TFromEnumRest<T['enum']> extends infer Elements extends Type.TSchema[]
? Type.TUnionEvaluated<Elements>
@@ -162,33 +164,33 @@ function FromEnum<T extends SEnum>(T: T): TFromEnum<T> {
// ------------------------------------------------------------------
// Tuple
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromTuple<T extends STuple> = (
TFromRest<T['items']> extends infer Elements extends Type.TSchema[]
? Type.TTuple<Elements>
: Type.TTuple<[]>
)
// prettier-ignore
// biome-ignore format: keep
function FromTuple<T extends STuple>(T: T): TFromTuple<T> {
return Type.Tuple(FromRest(T.items), T) as never
}
// ------------------------------------------------------------------
// Array
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromArray<T extends SArray> = (
TFromSchema<T['items']> extends infer Items extends Type.TSchema
? Type.TArray<Items>
: Type.TArray<Type.TUnknown>
)
// prettier-ignore
// biome-ignore format: keep
function FromArray<T extends SArray>(T: T): TFromArray<T> {
return Type.Array(FromSchema(T.items), T) as never
}
// ------------------------------------------------------------------
// Const
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
type TFromConst<T extends SConst> = (
Type.Ensure<Type.TLiteral<T['const']>>
)
@@ -202,13 +204,13 @@ type TFromPropertiesIsOptional<
K extends PropertyKey,
R extends string | unknown,
> = unknown extends R ? true : K extends R ? false : true;
// prettier-ignore
// biome-ignore format: keep
type TFromProperties<T extends SProperties, R extends string | unknown> = Type.Evaluate<{
-readonly [K in keyof T]: TFromPropertiesIsOptional<K, R> extends true
? Type.TOptional<TFromSchema<T[K]>>
: TFromSchema<T[K]>
}>
// prettier-ignore
// biome-ignore format: keep
type TFromObject<T extends SObject> = (
TFromProperties<T['properties'], Exclude<T['required'], undefined>[number]> extends infer Properties extends Type.TProperties
? Type.TObject<Properties>
@@ -217,11 +219,11 @@ type TFromObject<T extends SObject> = (
function FromObject<T extends SObject>(T: T): TFromObject<T> {
const properties = globalThis.Object.getOwnPropertyNames(T.properties).reduce((Acc, K) => {
return {
// biome-ignore lint/performance/noAccumulatingSpread: <explanation>
...Acc,
[K]:
T.required && T.required.includes(K)
? FromSchema(T.properties[K])
: Type.Optional(FromSchema(T.properties[K])),
[K]: T.required?.includes(K)
? FromSchema(T.properties[K])
: Type.Optional(FromSchema(T.properties[K])),
};
}, {} as Type.TProperties);
return Type.Object(properties, T) as never;
@@ -229,7 +231,7 @@ function FromObject<T extends SObject>(T: T): TFromObject<T> {
// ------------------------------------------------------------------
// FromSchema
// ------------------------------------------------------------------
// prettier-ignore
// biome-ignore format: keep
export type TFromSchema<T> = (
T extends SAllOf ? TFromAllOf<T> :
T extends SAnyOf ? TFromAnyOf<T> :
@@ -248,7 +250,7 @@ export type TFromSchema<T> = (
)
/** Parses a TypeBox type from raw JsonSchema */
export function FromSchema<T>(T: T): TFromSchema<T> {
// prettier-ignore
// biome-ignore format: keep
return (
IsSAllOf(T) ? FromAllOf(T) :
IsSAnyOf(T) ? FromAnyOf(T) :

View File

@@ -12,13 +12,13 @@ import {
type TSchema,
type TString,
Type,
TypeRegistry
TypeRegistry,
} from "@sinclair/typebox";
import {
DefaultErrorFunction,
Errors,
SetErrorFunction,
type ValueErrorIterator
type ValueErrorIterator,
} from "@sinclair/typebox/errors";
import { Check, Default, Value, type ValueError } from "@sinclair/typebox/value";
@@ -45,7 +45,7 @@ export class TypeInvalidError extends Error {
constructor(
public schema: TSchema,
public data: unknown,
message?: string
message?: string,
) {
//console.warn("errored schema", JSON.stringify(schema, null, 2));
super(message ?? `Invalid: ${JSON.stringify(data)}`);
@@ -66,7 +66,7 @@ export class TypeInvalidError extends Error {
message: this.message,
schema: this.schema,
data: this.data,
errors: this.errors
errors: this.errors,
};
}
}
@@ -95,7 +95,7 @@ export function mark(obj: any, validated = true) {
export function parse<Schema extends TSchema = TSchema>(
schema: Schema,
data: RecursivePartial<Static<Schema>>,
options?: ParseOptions
options?: ParseOptions,
): Static<Schema> {
if (!options?.forceParse && typeof data === "object" && validationSymbol in data) {
if (options?.useDefaults === false) {
@@ -124,7 +124,7 @@ export function parse<Schema extends TSchema = TSchema>(
export function parseDecode<Schema extends TSchema = TSchema>(
schema: Schema,
data: RecursivePartial<StaticDecode<Schema>>
data: RecursivePartial<StaticDecode<Schema>>,
): StaticDecode<Schema> {
//console.log("parseDecode", schema, data);
const parsed = Default(schema, data);
@@ -140,7 +140,7 @@ export function parseDecode<Schema extends TSchema = TSchema>(
export function strictParse<Schema extends TSchema = TSchema>(
schema: Schema,
data: Static<Schema>,
options?: ParseOptions
options?: ParseOptions,
): Static<Schema> {
return parse(schema, data as any, options);
}
@@ -157,7 +157,7 @@ export const StringEnum = <const T extends readonly string[]>(values: T, options
[Kind]: "StringEnum",
type: "string",
enum: values,
...options
...options,
});
// key value record compatible with RJSF and typebox inference
@@ -175,7 +175,7 @@ export const Const = <T extends TLiteralValue = TLiteralValue>(value: T, options
export const StringIdentifier = Type.String({
pattern: "^[a-zA-Z_][a-zA-Z0-9_]*$",
minLength: 2,
maxLength: 150
maxLength: 150,
});
SetErrorFunction((error) => {
@@ -202,5 +202,5 @@ export {
Value,
Default,
Errors,
Check
Check,
};