yaml-types is a utility library that provides a collection of JavaScript types as custom YAML tags for the `yaml` parser/stringifier library. It extends YAML's capabilities by allowing serialization and deserialization of common JavaScript primitives and objects like `RegExp`, `BigInt`, `Symbol`, `Error`, `Function`, and `Class`, as well as YAML 1.1 specific tags such as `!!binary`, `!!omap`, `!!pairs`, `!!set`, and `!!timestamp`. The current stable version is 0.4.0. The package has a relatively slow release cadence, with major features being added incrementally rather than on a strict schedule. Its key differentiator is providing pre-built tag definitions that are compatible with the `eemeli/yaml` library, simplifying the process of handling complex JavaScript types in YAML documents compared to manually defining custom tags for each type.
npm install yaml-typesVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing and stringifying various JavaScript types like RegExp, BigInt, Symbol, Error, and Date using custom YAML tags provided by `yaml-types`.
Upgrade your `yaml` package to version `^2.3.0` or higher (`npm install yaml@^2.3.0`).
When using `customTags`, explicitly include `bigint` and ensure it's prioritized, e.g., `parse(data, { customTags: [bigint, ...otherTags] })` or `stringify(data, { customTags: [bigint, ...otherTags] })`.Be aware that parsing `!function` or `!class` tags results in non-functional placeholders. If you need to serialize actual code logic, this library is not suitable; consider alternative serialization strategies or explicitly documenting this limitation.
Order your custom tags as `[classTag, functionTag]` when defining them for `parse` or `stringify` options to ensure correct type inference.
To change a tag's identifier, spread the original tag and override the `tag` property, e.g., `const customSymbolTag = { ...symbol, tag: 'tag:yaml.org,2002:js/symbol' };`. For named tag handles, you'll also need to configure `doc.directives.tags`.Ensure `regexp` is included in the `customTags` array when calling `parse` from `yaml`: `parse(yamlString, { customTags: [regexp] })`.Verify that all items in the `customTags` array are valid tag objects imported from `yaml-types` (e.g., `regexp`, `bigint`) or custom tag definitions that conform to the expected structure.