Skip to content

codecs @vuqs/core ​

A namespace of built-in codecs and codec factories. Every codec's parse returns undefined for absent or invalid input.

MemberKindValue typeNotes
codecs.stringcodecstringEmpty/whitespace-only → absent.
codecs.integercodecnumberNumber or base-10 string; serialize truncates toward zero.
codecs.floatcodecnumberNon-finite → absent.
codecs.booleancodecbooleanBoolean or 'true'/'false'.
codecs.indexcodecnumber1-based URL ⇄ 0-based value.
codecs.hexcodecnumberNon-negative; serialize pads to even length.
codecs.timestampcodecDateMilliseconds since epoch; eq by valueOf.
codecs.isoDatecodecDateYYYY-MM-DD, midnight UTC.
codecs.isoDateTimecodecDateFull ISO-8601.
codecs.arrayOf(codec)factoryT[]Repeated-key arrays; drops invalid items.
codecs.literal(values)factorystring or number unionOutside the set → absent.
codecs.enum(enumObject)factoryenum membersTS enum; outside it → absent.
codecs.json(options?)factoryTJSON text or parsed value; optional validate.

Codec ​

ts
interface Codec<T> {
  parse: (raw: ParsedQueryValue) => T | undefined
  serialize: (value: T) => ParsedQueryValue
  eq: (a: T, b: T) => boolean
  readonly defaultValue?: T
  withDefault: (defaultValue: T) => CodecWithDefault<T>
  nullable: () => Codec<T | null>
}

CodecWithDefault ​

ts
interface CodecWithDefault<T> extends Codec<T> {
  readonly defaultValue: T
  nullable: () => CodecWithDefault<T | null>
}

Codec.withDefault ​

Returns a variant of a codec carrying defaultValue. The codec's parse stays raw (undefined when absent or invalid); the param that binds the codec resolves the default, so it applies in one place rather than being baked into parse.

ts
function withDefault<T>(defaultValue: T): CodecWithDefault<T>

Parameters ​

  • defaultValue: T
    • The value an absent or invalid key reads back as.

Return value ​

  • codec: CodecWithDefault<T>
    • A codec exposing defaultValue. Bound refs read as T, resolving absence to the default. A written value equal to the default is omitted from the URL (clearOnDefault).

Usage ​

ts
import { codecs } from '@vuqs/core'

codecs.integer // Codec<number>            → ref is number | undefined
codecs.integer.withDefault(1) // CodecWithDefault<number> → ref is number

Codec.nullable ​

ts
interface Codec<T> {
  nullable: () => Codec<T | null>
}

interface CodecWithDefault<T> extends Codec<T> {
  nullable: () => CodecWithDefault<T | null>
}

Return value ​

  • codec: Codec<T | null> | CodecWithDefault<T | null>
    • A new codec accepting null. A defaulted codec returns a CodecWithDefault<T | null> carrying the same default.
    • serialize(null) returns undefined, omitting the param from the URL. Other values use the original serializer.
    • parse is unchanged, including absence and invalid-input handling.
    • eq treats two null values as equal and null versus a non-null value as unequal. Other comparisons use the original equality function.
    • Add .withDefault(null) to read absence as null.

Usage ​

ts
import { codecs, useQueryState } from '@vuqs/core'

const state = useQueryState('state', codecs.string.nullable().withDefault(null))
//    ^? QueryStateRef<string | null>

state.set('active')
state.set(null) // remove the param; state reads null

Released under the MIT License. Inspired by nuqs.