Refuse the mark spellings that cannot open or close where they sit
CI / gate (push) Successful in 4s

This commit is contained in:
2026-08-25 09:45:58 +02:00
parent 4e449ebb25
commit cccbfd3f2d
7 changed files with 76 additions and 21 deletions
+9 -4
View File
@@ -1,17 +1,22 @@
import { largestNesting } from './nesting.ts'
export type JsonValue = JsonValue[] | boolean | null | number | string | { [key: string]: JsonValue }
export function isJsonValue(value: unknown): value is JsonValue {
const pending: unknown[] = [value]
const pending: { depth: number; item: unknown }[] = [{ depth: 0, item: value }]
while (pending.length > 0) {
const item = pending.pop()
const entry = pending.pop()
if (entry === undefined) continue
const { depth, item } = entry
if (depth > largestNesting) return false
if (item === null || typeof item === 'boolean' || typeof item === 'string') continue
if (typeof item === 'number') {
if (!Number.isFinite(item)) return false
continue
}
// A hole is not a JSON value, and Array.prototype methods skip holes — spreading materialises them.
if (Array.isArray(item)) pending.push(...item)
else if (typeof item === 'object') pending.push(...Object.values(item))
if (Array.isArray(item)) for (const child of [...item]) pending.push({ depth: depth + 1, item: child })
else if (typeof item === 'object') for (const child of Object.values(item)) pending.push({ depth: depth + 1, item: child })
else return false
}
return true