yaml-types
Useful JavaScript types for YAML
Last updated 2 years ago by eemeli .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install yaml-types 
SYNC missed versions from official npm registry.

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

  • bigint (!bigint) - JavaScript BigInt values. Note: in order to use this effectively, a function must be provided as customTags in order to prepend the bigint tag, or else the built-in !!int tags will take priority. See bigint.test.ts for examples.
  • classTag (!class) - JavaScript Class values
  • error (!error) - JavaScript Error objects
  • functionTag (!function) - JavaScript Function values (will also be used to stringify Class values, unless the classTag tag is loaded ahead of functionTag)
  • nullobject (!nullobject) - Object with anull` prototype
  • regexp (!re) - RegExp values, using their default /foo/flags string representation.
  • sharedSymbol (!symbol/shared) - Shared Symbols, i.e. ones created with Symbol.for()
  • symbol (!symbol) - Unique Symbols

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/regexp instead of using a local !re tag for RegExp values:

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

const myregexp = { ...regexp, tag: 'tag:yaml.org,2002:js/regexp' }
stringify(/fo./m, { customTags: [myregexp] })
!!js/regexp /fo./m

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

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

const myregexp = { ...regexp, tag: 'tag:js:regexp' }
const doc = new Document(/fo./m, { customTags: [myregexp] })
doc.directives.tags['!js!'] = 'tag:js:'
doc.toString()
%TAG !js! tag:js:
---
!js!regexp /fo./m

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.

Current Tags

  • 0.3.0                                ...           latest (2 years ago)

3 Versions

  • 0.3.0                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (5)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |