YAML Types for JavaScript
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.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'match')
cause Attempting to call `.match()` on a RegExp object that was not correctly parsed from YAML due to missing `regexp` custom tag.fixEnsure `regexp` is included in the `customTags` array when calling `parse` from `yaml`: `parse(yamlString, { customTags: [regexp] })`. -
Error: Expected custom tag to be a plain object with tag and resolve properties, or an array of tags
cause Incorrectly passing a string or non-object value where a tag object is expected in the `customTags` array.fixVerify 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.
Warnings
- breaking Version 0.2.0 introduced a peer dependency requirement for `yaml` version `^2.3.0`. Users on older `yaml` versions will encounter peer dependency warnings or errors during installation.
- gotcha When using the `bigint` tag, it is crucial to ensure it is placed correctly within the `customTags` array, typically at the beginning, to prevent the default `!!int` tag from `yaml` from incorrectly parsing or stringifying BigInt values as standard integers if their string representation looks like a regular number.
- gotcha The `!function` and `!class` tags do not replicate executable code. Instead, they produce no-op function/class values with matching names and `toString` properties. This means any methods or logic within the original function/class will not be preserved or restored.
- gotcha The `functionTag` can stringify `Class` values if `classTag` is not loaded ahead of it. To correctly distinguish between functions and classes in stringification, ensure `classTag` is listed before `functionTag` in your `customTags` array.
- gotcha Modifying default tag identifiers requires creating a new tag object with the desired `tag` property. This is especially relevant for using YAML 1.1 tags with different local prefixes or integrating into custom tag namespaces.
Install
-
npm install yaml-types -
yarn add yaml-types -
pnpm add yaml-types
Imports
- regexp
const regexp = require('yaml-types').regexpimport { regexp } from 'yaml-types' - parse
import { parse } from 'yaml' - bigint
import { bigint } from 'yaml-types'
Quickstart
import { parse, stringify } from 'yaml';
import { regexp, bigint, sharedSymbol, error, timestamp } from 'yaml-types';
// Example 1: Parsing a RegExp
const yamlRegExp = '!re /test/gi';
const parsedRegExp = parse(yamlRegExp, { customTags: [regexp] });
console.log('Parsed RegExp:', parsedRegExp); // Outputs a RegExp object
console.log('RegExp match:', 'Testing123'.match(parsedRegExp));
// Example 2: Stringifying a BigInt
const myBigInt = 123456789012345678901234567890n;
// Note: bigint must be explicitly prioritized for accurate representation
const yamlBigInt = stringify(myBigInt, { customTags: [bigint] });
console.log('Stringified BigInt:', yamlBigInt);
// Example 3: Parsing a shared Symbol and a timestamp
const yamlComplex = `
- !symbol/shared mySharedSymbol
- !error "Something went wrong"
- !!timestamp 2023-10-27T10:00:00Z
`;
const parsedComplex = parse(yamlComplex, { customTags: [sharedSymbol, error, timestamp] });
console.log('Parsed complex data:', parsedComplex);
console.log('Is first item a shared symbol?', Symbol.for('mySharedSymbol') === parsedComplex[0]);
console.log('Error message:', parsedComplex[1].message);