YAML Types for JavaScript

0.4.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates parsing and stringifying various JavaScript types like RegExp, BigInt, Symbol, Error, and Date using custom YAML tags provided by `yaml-types`.

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);

view raw JSON →