|
| 1 | +# yaml-types |
| 2 | + |
| 3 | +Additional useful types for [`yaml`](https://github.com/eemeli/yaml). |
| 4 | + |
| 5 | +## Installation and Usage |
| 6 | + |
| 7 | +``` |
| 8 | +npm install yaml yaml-types |
| 9 | +``` |
| 10 | + |
| 11 | +Each type (a.k.a. "tag") is available as a named export of `'yaml-types'`. |
| 12 | +These may then be used as [custom tags](https://eemeli.org/yaml/#writing-custom-tags): |
| 13 | + |
| 14 | +```js |
| 15 | +import { parse } from 'yaml' |
| 16 | +import { regexp } from 'yaml-types' |
| 17 | + |
| 18 | +const re = parse('!re /fo./g', { customTags: [regexp] }) |
| 19 | +'foxbarfoo'.match(re) // [ 'fox', 'foo' ] |
| 20 | +``` |
| 21 | + |
| 22 | +## Available Types |
| 23 | + |
| 24 | +- `regexp` (`!re`) - [RegExp] values, |
| 25 | + using their default `/foo/flags` string representation. |
| 26 | + |
| 27 | +[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions |
| 28 | + |
| 29 | +## Customising Tag Names |
| 30 | + |
| 31 | +To use one of the types with a different tag identifier, set its `tag` value accordingly. |
| 32 | +For example, to extend the default namespace with `!!js/regexp` instead of using a locale `!re` tag for RegExp values: |
| 33 | + |
| 34 | +```js |
| 35 | +import { stringify } from 'yaml' |
| 36 | +import { regexp } from 'yaml-types' |
| 37 | + |
| 38 | +const myregexp = { ...regexp, tag: 'tag:yaml.org,2002:js/regexp' } |
| 39 | +const str = stringify(/fo./m, { customTags: [myregexp] }) |
| 40 | +// '!!js/regexp /fo./m\n' |
| 41 | +``` |
| 42 | + |
| 43 | +## Contributing |
| 44 | + |
| 45 | +Additions to this library are very welcome! |
| 46 | +Many data types are useful beyond any single project, |
| 47 | +and while the core `yaml` library is mostly limited to the YAML spec, |
| 48 | +no such restriction applies here. |
| 49 | + |
| 50 | +The source code is written in [TypeScript], and the tests use [Node-Tap]. |
| 51 | +When submitting a PR for a new type, tests and documentation are required, |
| 52 | +as well as satisfying [Prettier]. |
| 53 | + |
| 54 | +[TypeScript]: https://www.typescriptlang.org/ |
| 55 | +[Node-Tap]: https://node-tap.org/ |
| 56 | +[Prettier]: https://prettier.io/ |
0 commit comments