mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
Move duplicate isObject, asString, asNumber, and safeStringify functions from multiple modules into a centralized shared/src/utils.ts module and update imports across cli, server, and web packages. This eliminates code duplication and improves maintainability.
22 lines
688 B
TypeScript
22 lines
688 B
TypeScript
export function isObject(value: unknown): value is Record<string, unknown> {
|
|
return Boolean(value) && typeof value === 'object'
|
|
}
|
|
|
|
export function asString(value: unknown): string | null {
|
|
return typeof value === 'string' ? value : null
|
|
}
|
|
|
|
export function asNumber(value: unknown): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) ? value : null
|
|
}
|
|
|
|
export function safeStringify(value: unknown): string {
|
|
if (typeof value === 'string') return value
|
|
try {
|
|
const stringified = JSON.stringify(value, null, 2)
|
|
return typeof stringified === 'string' ? stringified : String(value)
|
|
} catch {
|
|
return String(value)
|
|
}
|
|
}
|