UNPKG

6.06 kBMarkdownView Raw
1# YAML <a href="https://www.npmjs.com/package/yaml"><img align="right" src="https://badge.fury.io/js/yaml.svg" title="npm package" /></a>
2
3`yaml` is a definitive library for [YAML](https://yaml.org/), the human friendly data serialization standard.
4This library:
5
6- Supports both YAML 1.1 and YAML 1.2 and all common data schemas,
7- Passes all of the [yaml-test-suite](https://github.com/yaml/yaml-test-suite) tests,
8- Can accept any string as input without throwing, parsing as much YAML out of it as it can, and
9- Supports parsing, modifying, and writing YAML comments and blank lines.
10
11The library is released under the ISC open source license, and the code is [available on GitHub](https://github.com/eemeli/yaml/).
12It has no external dependencies and runs on Node.js as well as modern browsers.
13
14For the purposes of versioning, any changes that break any of the documented endpoints or APIs will be considered semver-major breaking changes.
15Undocumented library internals may change between minor versions, and previous APIs may be deprecated (but not removed).
16
17The minimum supported TypeScript version of the included typings is 3.9;
18for use in earlier versions you may need to set `skipLibCheck: true` in your config.
19This requirement may be updated between minor versions of the library.
20
21For more information, see the project's documentation site: [**eemeli.org/yaml**](https://eemeli.org/yaml/)
22
23To install:
24
25```sh
26npm install yaml
27```
28
29**Note:** These docs are for `yaml@2`. For v1, see the [v1.10.0 tag](https://github.com/eemeli/yaml/tree/v1.10.0) for the source and [eemeli.org/yaml/v1](https://eemeli.org/yaml/v1/) for the documentation.
30
31The development and maintenance of this library is [sponsored](https://github.com/sponsors/eemeli) by:
32
33<a href="https://www.scipress.io/">
34<img width=150 src="https://eemeli.org/yaml/images/scipress.svg" alt="Scipress" />
35</a>
36
37## API Overview
38
39The API provided by `yaml` has three layers, depending on how deep you need to go: [Parse & Stringify](https://eemeli.org/yaml/#parse-amp-stringify), [Documents](https://eemeli.org/yaml/#documents), and the underlying [Lexer/Parser/Composer](https://eemeli.org/yaml/#parsing-yaml).
40The first has the simplest API and "just works", the second gets you all the bells and whistles supported by the library along with a decent [AST](https://eemeli.org/yaml/#content-nodes), and the third lets you get progressively closer to YAML source, if that's your thing.
41
42A [command-line tool](https://eemeli.org/yaml/#command-line-tool) is also included.
43
44```js
45import { parse, stringify } from 'yaml'
46// or
47import YAML from 'yaml'
48// or
49const YAML = require('yaml')
50```
51
52### Parse & Stringify
53
54- [`parse(str, reviver?, options?): value`](https://eemeli.org/yaml/#yaml-parse)
55- [`stringify(value, replacer?, options?): string`](https://eemeli.org/yaml/#yaml-stringify)
56
57### Documents
58
59- [`Document`](https://eemeli.org/yaml/#documents)
60 - [`constructor(value, replacer?, options?)`](https://eemeli.org/yaml/#creating-documents)
61 - [`#anchors`](https://eemeli.org/yaml/#working-with-anchors)
62 - [`#contents`](https://eemeli.org/yaml/#content-nodes)
63 - [`#directives`](https://eemeli.org/yaml/#stream-directives)
64 - [`#errors`](https://eemeli.org/yaml/#errors)
65 - [`#warnings`](https://eemeli.org/yaml/#errors)
66- [`isDocument(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
67- [`parseAllDocuments(str, options?): Document[]`](https://eemeli.org/yaml/#parsing-documents)
68- [`parseDocument(str, options?): Document`](https://eemeli.org/yaml/#parsing-documents)
69
70### Content Nodes
71
72- [`isAlias(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
73- [`isCollection(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
74- [`isMap(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
75- [`isNode(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
76- [`isPair(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
77- [`isScalar(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
78- [`isSeq(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
79- [`new Scalar(value)`](https://eemeli.org/yaml/#scalar-values)
80- [`new YAMLMap()`](https://eemeli.org/yaml/#collections)
81- [`new YAMLSeq()`](https://eemeli.org/yaml/#collections)
82- [`doc.createAlias(node, name?): Alias`](https://eemeli.org/yaml/#working-with-anchors)
83- [`doc.createNode(value, options?): Node`](https://eemeli.org/yaml/#creating-nodes)
84- [`doc.createPair(key, value): Pair`](https://eemeli.org/yaml/#creating-nodes)
85- [`visit(node, visitor)`](https://eemeli.org/yaml/#finding-and-modifying-nodes)
86
87### Parsing YAML
88
89- [`new Lexer().lex(src)`](https://eemeli.org/yaml/#lexer)
90- [`new Parser(onNewLine?).parse(src)`](https://eemeli.org/yaml/#parser)
91- [`new Composer(options?).compose(tokens)`](https://eemeli.org/yaml/#composer)
92
93## YAML.parse
94
95```yaml
96# file.yml
97YAML:
98 - A human-readable data serialization language
99 - https://en.wikipedia.org/wiki/YAML
100yaml:
101 - A complete JavaScript implementation
102 - https://www.npmjs.com/package/yaml
103```
104
105```js
106import fs from 'fs'
107import YAML from 'yaml'
108
109YAML.parse('3.14159')
110// 3.14159
111
112YAML.parse('[ true, false, maybe, null ]\n')
113// [ true, false, 'maybe', null ]
114
115const file = fs.readFileSync('./file.yml', 'utf8')
116YAML.parse(file)
117// { YAML:
118// [ 'A human-readable data serialization language',
119// 'https://en.wikipedia.org/wiki/YAML' ],
120// yaml:
121// [ 'A complete JavaScript implementation',
122// 'https://www.npmjs.com/package/yaml' ] }
123```
124
125## YAML.stringify
126
127```js
128import YAML from 'yaml'
129
130YAML.stringify(3.14159)
131// '3.14159\n'
132
133YAML.stringify([true, false, 'maybe', null])
134// `- true
135// - false
136// - maybe
137// - null
138// `
139
140YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
141// `number: 3
142// plain: string
143// block: |
144// two
145// lines
146// `
147```
148
149---
150
151Browser testing provided by:
152
153<a href="https://www.browserstack.com/open-source">
154<img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" alt="BrowserStack" />
155</a>