/serialize-typst-value

Serialize JavaScript objects to Typst values

Primary LanguageJavaScript

Serialize Typst Value

This is a simple package that provides a way to serialize JavaScript objects to Typst values. It is useful for converting JavaScript objects into a format that can be inserted into Typst documents.

Installation

npm install serialize-typst-value

Usage

import serializeTypstValue from 'serialize-typst-value'

const obj = {
  name: 'John Doe',
  age: 30,
  isActive: true,
  hobbies: ['reading', 'gaming'],
  address: {
    street: '123 Main St',
    city: 'Anytown',
    country: 'USA'
  }
}

console.log(serializeTypstValue(obj))
// With added whitespace:
// (
//   "name": "John Doe",
//   "age": 30,
//   "isActive": true,
//   "hobbies": ("reading", "gaming"),
//   "address": ("street": "123 Main St", "city": "Anytown", "country": "USA")
// )

Details

Values are serialized in the following manner:

  • null is serialized as none
  • boolean is serialized as true or false
  • number is serialized as either an int or a float
  • BigInt is serialized as an int
  • string are serialized as a str
  • Function and Symbol will throw an UnexpectedValueError
  • undefined will be ignored in object values, and will throw an UnexpectedValueError in any other place
  • Temporal.PlainDate, Temporal.PlainDateTime, and Temporal.PlainTime is serialized as datetime
  • Date, Map, Set, and RegExp is not yet supported, but might be included in a future minor version, it's currently undefined behavior to try and serialize them