mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
16 lines
832 B
TypeScript
16 lines
832 B
TypeScript
import type { JsonValue } from '../json-value.ts'
|
|
|
|
// Letters and digits alone, after an optional `#`, so no value can end the declaration or call `url()`.
|
|
const colour = /^#?[0-9a-zA-Z]+$/
|
|
|
|
// `property:colour`, `undefined` where the value is no colour the declaration can hold.
|
|
export function colourDeclaration(property: string, value: JsonValue | undefined): string | undefined {
|
|
return typeof value === 'string' && colour.test(value) ? `${property}:${value}` : undefined
|
|
}
|
|
|
|
// The colour a `property:colour` declaration holds, `undefined` where it holds another property or no such colour.
|
|
export function readColour(property: string, declaration: string): string | undefined {
|
|
const value = declaration.slice(property.length + 1)
|
|
return declaration.startsWith(`${property}:`) && colour.test(value) ? value : undefined
|
|
}
|