Home

Awesome

yaml-types

Additional useful types for yaml.

Installation and Usage

npm install yaml yaml-types

Each type (a.k.a. "tag") is available as a named export of 'yaml-types'. These may then be used as custom tags:

import { parse } from 'yaml'
import { regexp } from 'yaml-types'

const re = parse('!re /fo./g', { customTags: [regexp] })
'foxbarfoo'.match(re) // [ 'fox', 'foo' ]

Available Types

The function and class values created by parsing !function and !class tags will not actually replicate running code, but rather no-op function/class values with matching name and toString properties.

Customising Tag Names

To use one of the types with a different tag identifier, set its tag value accordingly. For example, to extend the default tag namespace with !!js/symbol instead of using a local !symbol tag for Symbol values:

import { stringify } from 'yaml'
import { symbol } from 'yaml-types'

const mysymbol = { ...symbol, tag: 'tag:yaml.org,2002:js/symbol' }
stringify(Symbol('foo'), { customTags: [mysymbol] })
!!js/symbol foo

To use a named tag handle like !js!symbol, a few more steps are required:

import { Document } from 'yaml'
import { symbol } from 'yaml-types'

const mysymbol = { ...symbol, tag: 'tag:js:symbol' }
const doc = new Document(Symbol('foo'), { customTags: [mysymbol] })
doc.directives.tags['!js!'] = 'tag:js:'
doc.toString()
%TAG !js! tag:js:
---
!js!symbol foo

Contributing

Additions to this library are very welcome! Many data types are useful beyond any single project, and while the core yaml library is mostly limited to the YAML spec, no such restriction applies here.

The source code is written in TypeScript, and the tests use Node-Tap. When submitting a PR for a new type, tests and documentation are required, as well as satisfying Prettier.