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.
npm install serialize-typst-value
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")
// )
Values are serialized in the following manner:
null
is serialized asnone
boolean
is serialized astrue
orfalse
number
is serialized as either anint
or afloat
NaN
is serialized asfloat.nan
Infinity
is serialized asfloat.inf
-Infinity
is serialized as-float.inf
BigInt
is serialized as anint
string
are serialized as astr
Function
andSymbol
will throw anUnexpectedValueError
undefined
will be ignored in object values, and will throw anUnexpectedValueError
in any other placeTemporal.PlainDate
,Temporal.PlainDateTime
, andTemporal.PlainTime
is serialized asdatetime
Date
,Map
,Set
, andRegExp
is not yet supported, but might be included in a future minor version, it's currently undefined behavior to try and serialize them