mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-12 11:48:29 +01:00
15 lines
573 B
TypeScript
15 lines
573 B
TypeScript
import { failure, type Result } from './result.ts'
|
|
|
|
// Each reader's refusal of input that is no string, `format` naming what it reads.
|
|
export function notAString<T>(value: unknown, format: string): Result<T> {
|
|
return failure('not-a-string', `${format} is a string: found ${typeName(value)}`, [])
|
|
}
|
|
|
|
// `typeof` alone, since anything more throws on a revoked Proxy.
|
|
function typeName(value: unknown): string {
|
|
if (value === null) return 'null'
|
|
const type = typeof value
|
|
if (type === 'undefined') return type
|
|
return type === 'object' ? 'an object' : `a ${type}`
|
|
}
|