mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-17 05:40:26 +01:00
35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
import type { AdfAttributes } from './document.ts'
|
|
import type { JsonValue } from '../json-value.ts'
|
|
|
|
export type AttributeKind = 'boolean' | 'json' | 'number' | 'string'
|
|
|
|
export type AttributeVocabulary = Readonly<Record<string, AttributeKind>>
|
|
|
|
export type VocabularyValue =
|
|
| { kind: 'boolean'; value: boolean }
|
|
| { kind: 'json'; value: JsonValue }
|
|
| { kind: 'number'; value: number }
|
|
| { kind: 'string'; value: string }
|
|
|
|
export type VocabularyPair = VocabularyValue & { key: string }
|
|
|
|
export function vocabularyPairs(attrs: AdfAttributes, vocabulary: AttributeVocabulary, spelledElsewhere: readonly string[]): VocabularyPair[] | undefined {
|
|
const pairs: VocabularyPair[] = []
|
|
for (const [key, value] of Object.entries(attrs)) {
|
|
if (spelledElsewhere.includes(key)) continue
|
|
const kind = Object.hasOwn(vocabulary, key) ? vocabulary[key] : undefined
|
|
if (kind === undefined) return undefined
|
|
const pair = vocabularyPair(key, value, kind)
|
|
if (pair === undefined) return undefined
|
|
pairs.push(pair)
|
|
}
|
|
return pairs
|
|
}
|
|
|
|
function vocabularyPair(key: string, value: JsonValue, kind: AttributeKind): VocabularyPair | undefined {
|
|
if (kind === 'boolean') return typeof value === 'boolean' ? { key, kind, value } : undefined
|
|
if (kind === 'number') return typeof value === 'number' ? { key, kind, value } : undefined
|
|
if (kind === 'string') return typeof value === 'string' ? { key, kind, value } : undefined
|
|
return { key, kind, value }
|
|
}
|