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.
|
---|
4 | This 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 |
|
---|
11 | The library is released under the ISC open source license, and the code is [available on GitHub](https://github.com/eemeli/yaml/).
|
---|
12 | It has no external dependencies and runs on Node.js as well as modern browsers.
|
---|
13 |
|
---|
14 | For the purposes of versioning, any changes that break any of the documented endpoints or APIs will be considered semver-major breaking changes.
|
---|
15 | Undocumented library internals may change between minor versions, and previous APIs may be deprecated (but not removed).
|
---|
16 |
|
---|
17 | The minimum supported TypeScript version of the included typings is 3.9;
|
---|
18 | for use in earlier versions you may need to set `skipLibCheck: true` in your config.
|
---|
19 | This requirement may be updated between minor versions of the library.
|
---|
20 |
|
---|
21 | For more information, see the project's documentation site: [**eemeli.org/yaml**](https://eemeli.org/yaml/)
|
---|
22 |
|
---|
23 | To install:
|
---|
24 |
|
---|
25 | ```sh
|
---|
26 | npm 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 |
|
---|
31 | ## API Overview
|
---|
32 |
|
---|
33 | The 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).
|
---|
34 | The 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.
|
---|
35 |
|
---|
36 | ```js
|
---|
37 | import { parse, stringify } from 'yaml'
|
---|
38 | // or
|
---|
39 | import YAML from 'yaml'
|
---|
40 | // or
|
---|
41 | const YAML = require('yaml')
|
---|
42 | ```
|
---|
43 |
|
---|
44 | ### Parse & Stringify
|
---|
45 |
|
---|
46 | - [`parse(str, reviver?, options?): value`](https://eemeli.org/yaml/#yaml-parse)
|
---|
47 | - [`stringify(value, replacer?, options?): string`](https://eemeli.org/yaml/#yaml-stringify)
|
---|
48 |
|
---|
49 | ### Documents
|
---|
50 |
|
---|
51 | - [`Document`](https://eemeli.org/yaml/#documents)
|
---|
52 | - [`constructor(value, replacer?, options?)`](https://eemeli.org/yaml/#creating-documents)
|
---|
53 | - [`#anchors`](https://eemeli.org/yaml/#working-with-anchors)
|
---|
54 | - [`#contents`](https://eemeli.org/yaml/#content-nodes)
|
---|
55 | - [`#directives`](https://eemeli.org/yaml/#stream-directives)
|
---|
56 | - [`#errors`](https://eemeli.org/yaml/#errors)
|
---|
57 | - [`#warnings`](https://eemeli.org/yaml/#errors)
|
---|
58 | - [`isDocument(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
59 | - [`parseAllDocuments(str, options?): Document[]`](https://eemeli.org/yaml/#parsing-documents)
|
---|
60 | - [`parseDocument(str, options?): Document`](https://eemeli.org/yaml/#parsing-documents)
|
---|
61 |
|
---|
62 | ### Content Nodes
|
---|
63 |
|
---|
64 | - [`isAlias(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
65 | - [`isCollection(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
66 | - [`isMap(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
67 | - [`isNode(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
68 | - [`isPair(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
69 | - [`isScalar(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
70 | - [`isSeq(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
|
---|
71 | - [`new Scalar(value)`](https://eemeli.org/yaml/#scalar-values)
|
---|
72 | - [`new YAMLMap()`](https://eemeli.org/yaml/#collections)
|
---|
73 | - [`new YAMLSeq()`](https://eemeli.org/yaml/#collections)
|
---|
74 | - [`doc.createAlias(node, name?): Alias`](https://eemeli.org/yaml/#working-with-anchors)
|
---|
75 | - [`doc.createNode(value, options?): Node`](https://eemeli.org/yaml/#creating-nodes)
|
---|
76 | - [`doc.createPair(key, value): Pair`](https://eemeli.org/yaml/#creating-nodes)
|
---|
77 | - [`visit(node, visitor)`](https://eemeli.org/yaml/#modifying-nodes)
|
---|
78 |
|
---|
79 | ### Parsing YAML
|
---|
80 |
|
---|
81 | - [`new Lexer().lex(src)`](https://eemeli.org/yaml/#lexer)
|
---|
82 | - [`new Parser(onNewLine?).parse(src)`](https://eemeli.org/yaml/#parser)
|
---|
83 | - [`new Composer(options?).compose(tokens)`](https://eemeli.org/yaml/#composer)
|
---|
84 |
|
---|
85 | ## YAML.parse
|
---|
86 |
|
---|
87 | ```yaml
|
---|
88 | # file.yml
|
---|
89 | YAML:
|
---|
90 | - A human-readable data serialization language
|
---|
91 | - https://en.wikipedia.org/wiki/YAML
|
---|
92 | yaml:
|
---|
93 | - A complete JavaScript implementation
|
---|
94 | - https://www.npmjs.com/package/yaml
|
---|
95 | ```
|
---|
96 |
|
---|
97 | ```js
|
---|
98 | import fs from 'fs'
|
---|
99 | import YAML from 'yaml'
|
---|
100 |
|
---|
101 | YAML.parse('3.14159')
|
---|
102 | // 3.14159
|
---|
103 |
|
---|
104 | YAML.parse('[ true, false, maybe, null ]\n')
|
---|
105 | // [ true, false, 'maybe', null ]
|
---|
106 |
|
---|
107 | const file = fs.readFileSync('./file.yml', 'utf8')
|
---|
108 | YAML.parse(file)
|
---|
109 | // { YAML:
|
---|
110 | // [ 'A human-readable data serialization language',
|
---|
111 | // 'https://en.wikipedia.org/wiki/YAML' ],
|
---|
112 | // yaml:
|
---|
113 | // [ 'A complete JavaScript implementation',
|
---|
114 | // 'https://www.npmjs.com/package/yaml' ] }
|
---|
115 | ```
|
---|
116 |
|
---|
117 | ## YAML.stringify
|
---|
118 |
|
---|
119 | ```js
|
---|
120 | import YAML from 'yaml'
|
---|
121 |
|
---|
122 | YAML.stringify(3.14159)
|
---|
123 | // '3.14159\n'
|
---|
124 |
|
---|
125 | YAML.stringify([true, false, 'maybe', null])
|
---|
126 | // `- true
|
---|
127 | // - false
|
---|
128 | // - maybe
|
---|
129 | // - null
|
---|
130 | // `
|
---|
131 |
|
---|
132 | YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
|
---|
133 | // `number: 3
|
---|
134 | // plain: string
|
---|
135 | // block: |
|
---|
136 | // two
|
---|
137 | // lines
|
---|
138 | // `
|
---|
139 | ```
|
---|
140 |
|
---|
141 | ---
|
---|
142 |
|
---|
143 | Browser testing provided by:
|
---|
144 |
|
---|
145 | <a href="https://www.browserstack.com/open-source">
|
---|
146 | <img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" />
|
---|
147 | </a>
|
---|