UNPKG

6.32 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<p align="center" width="100%">
34 <a href="https://www.scipress.io/"
35 ><img
36 width="150"
37 align="top"
38 src="https://eemeli.org/yaml/images/scipress.svg"
39 alt="Scipress"
40 /></a>
41 &nbsp; &nbsp;
42 <a href="https://manifest.build/"
43 ><img
44 width="150"
45 align="top"
46 src="https://eemeli.org/yaml/images/manifest.svg"
47 alt="Manifest"
48 /></a>
49</p>
50
51## API Overview
52
53The 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).
54The 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.
55
56A [command-line tool](https://eemeli.org/yaml/#command-line-tool) is also included.
57
58```js
59import { parse, stringify } from 'yaml'
60// or
61import YAML from 'yaml'
62// or
63const YAML = require('yaml')
64```
65
66### Parse & Stringify
67
68- [`parse(str, reviver?, options?): value`](https://eemeli.org/yaml/#yaml-parse)
69- [`stringify(value, replacer?, options?): string`](https://eemeli.org/yaml/#yaml-stringify)
70
71### Documents
72
73- [`Document`](https://eemeli.org/yaml/#documents)
74 - [`constructor(value, replacer?, options?)`](https://eemeli.org/yaml/#creating-documents)
75 - [`#anchors`](https://eemeli.org/yaml/#working-with-anchors)
76 - [`#contents`](https://eemeli.org/yaml/#content-nodes)
77 - [`#directives`](https://eemeli.org/yaml/#stream-directives)
78 - [`#errors`](https://eemeli.org/yaml/#errors)
79 - [`#warnings`](https://eemeli.org/yaml/#errors)
80- [`isDocument(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
81- [`parseAllDocuments(str, options?): Document[]`](https://eemeli.org/yaml/#parsing-documents)
82- [`parseDocument(str, options?): Document`](https://eemeli.org/yaml/#parsing-documents)
83
84### Content Nodes
85
86- [`isAlias(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
87- [`isCollection(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
88- [`isMap(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
89- [`isNode(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
90- [`isPair(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
91- [`isScalar(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
92- [`isSeq(foo): boolean`](https://eemeli.org/yaml/#identifying-node-types)
93- [`new Scalar(value)`](https://eemeli.org/yaml/#scalar-values)
94- [`new YAMLMap()`](https://eemeli.org/yaml/#collections)
95- [`new YAMLSeq()`](https://eemeli.org/yaml/#collections)
96- [`doc.createAlias(node, name?): Alias`](https://eemeli.org/yaml/#working-with-anchors)
97- [`doc.createNode(value, options?): Node`](https://eemeli.org/yaml/#creating-nodes)
98- [`doc.createPair(key, value): Pair`](https://eemeli.org/yaml/#creating-nodes)
99- [`visit(node, visitor)`](https://eemeli.org/yaml/#finding-and-modifying-nodes)
100
101### Parsing YAML
102
103- [`new Lexer().lex(src)`](https://eemeli.org/yaml/#lexer)
104- [`new Parser(onNewLine?).parse(src)`](https://eemeli.org/yaml/#parser)
105- [`new Composer(options?).compose(tokens)`](https://eemeli.org/yaml/#composer)
106
107## YAML.parse
108
109```yaml
110# file.yml
111YAML:
112 - A human-readable data serialization language
113 - https://en.wikipedia.org/wiki/YAML
114yaml:
115 - A complete JavaScript implementation
116 - https://www.npmjs.com/package/yaml
117```
118
119```js
120import fs from 'fs'
121import YAML from 'yaml'
122
123YAML.parse('3.14159')
124// 3.14159
125
126YAML.parse('[ true, false, maybe, null ]\n')
127// [ true, false, 'maybe', null ]
128
129const file = fs.readFileSync('./file.yml', 'utf8')
130YAML.parse(file)
131// { YAML:
132// [ 'A human-readable data serialization language',
133// 'https://en.wikipedia.org/wiki/YAML' ],
134// yaml:
135// [ 'A complete JavaScript implementation',
136// 'https://www.npmjs.com/package/yaml' ] }
137```
138
139## YAML.stringify
140
141```js
142import YAML from 'yaml'
143
144YAML.stringify(3.14159)
145// '3.14159\n'
146
147YAML.stringify([true, false, 'maybe', null])
148// `- true
149// - false
150// - maybe
151// - null
152// `
153
154YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
155// `number: 3
156// plain: string
157// block: |
158// two
159// lines
160// `
161```
162
163---
164
165Browser testing provided by:
166
167<a href="https://www.browserstack.com/open-source">
168<img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" alt="BrowserStack" />
169</a>