Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,31 +579,30 @@ export function printNumbersAsHexSequence(numbers: number[], hexLength: number):
return numbers.map((v) => v.toString(16).padStart(hexLength, "0")).join(":");
}

// biome-ignore lint/suspicious/noExplicitAny: ignored using `--suppress`
// biome-ignore lint/suspicious/noExplicitAny: generic object
export function assertObject<T extends Record<string, any>>(value: unknown, property?: string): asserts value is T {
const isObject = typeof value === "object" && !Array.isArray(value) && value !== null;
if (!isObject) {
if (typeof value !== "object" || Array.isArray(value) || value === null) {
throw new Error(`${property} is not a object, got ${typeof value} (${JSON.stringify(value)})`);
}
}

export function assertArray(value: unknown, property?: string): asserts value is Array<unknown> {
// biome-ignore lint/style/noParameterAssign: ignored using `--suppress`
property = property ? `'${property}'` : "Value";
if (!Array.isArray(value)) throw new Error(`${property} is not an array, got ${typeof value} (${value.toString()})`);
if (!Array.isArray(value)) {
throw new Error(`${property ? `'${property}'` : "Value"} is not an array, got ${typeof value} (${value.toString()})`);
}
}

export function assertString(value: unknown, property?: string): asserts value is string {
// biome-ignore lint/style/noParameterAssign: ignored using `--suppress`
property = property ? `'${property}'` : "Value";
if (typeof value !== "string") throw new Error(`${property} is not a string, got ${typeof value} (${value.toString()})`);
if (typeof value !== "string") {
throw new Error(`${property ? `'${property}'` : "Value"} is not a string, got ${typeof value} (${value.toString()})`);
}
}

export function isNumber(value: unknown): value is number {
return typeof value === "number";
}

// biome-ignore lint/suspicious/noExplicitAny: ignored using `--suppress`
// biome-ignore lint/suspicious/noExplicitAny: generic object
export function isObject(value: unknown): value is {[s: string]: any} {
return typeof value === "object" && !Array.isArray(value);
Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isObject function doesn't check for null values, which means null will return true since typeof null === "object". This is inconsistent with the assertObject function which explicitly excludes null. Add && value !== null to maintain consistency.

Suggested change
return typeof value === "object" && !Array.isArray(value);
return typeof value === "object" && value !== null && !Array.isArray(value);

Copilot uses AI. Check for mistakes.
}
Expand All @@ -617,18 +616,16 @@ export function isBoolean(value: unknown): value is boolean {
}

export function assertNumber(value: unknown, property?: string): asserts value is number {
// biome-ignore lint/style/noParameterAssign: ignored using `--suppress`
property = property ? `'${property}'` : "Value";
if (typeof value !== "number" || Number.isNaN(value)) throw new Error(`${property} is not a number, got ${typeof value} (${value?.toString()})`);
if (typeof value !== "number" || Number.isNaN(value)) {
throw new Error(`${property ? `'${property}'` : "Value"} is not a number, got ${typeof value} (${value?.toString()})`);
}
}

export function toNumber(value: unknown, property?: string): number {
// biome-ignore lint/style/noParameterAssign: ignored using `--suppress`
property = property ? `'${property}'` : "Value";
// @ts-expect-error ignore
const result = Number.parseFloat(value);
if (Number.isNaN(result)) {
throw new Error(`${property} is not a number, got ${typeof value} (${value.toString()})`);
throw new Error(`${property ? `'${property}'` : "Value"} is not a number, got ${typeof value} (${value.toString()})`);
}
return result;
}
Expand Down