UNPKG

5.44 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
17For more information, see the project's documentation site: [**eemeli.org/yaml**](https://eemeli.org/yaml/)
18
19To install:
20
21```sh
22npm install yaml
23```
24
25**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.
26
27## API Overview
28
29The 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).
30The 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.
31
32```js
33import { parse, stringify } from 'yaml'
34// or
35import YAML from 'yaml'
36// or
37const YAML = require('yaml')
38```
39
40### Parse & Stringify
41
42- [`parse(str, reviver?, options?): value`](https://eemeli.org/yaml/#yaml-parse)
43- [`stringify(value, replacer?, options?): string`](https://eemeli.org/yaml/#yaml-stringify)
44
45### Documents
46
47- [`Document`](https://eemeli.org/yaml/#documents)
48 - [`constructor(value, replacer?, options?)`](https://eemeli.org/yaml/#creating-documents)
49 - [`#anchors`](https://eemeli.org/yaml/#working-with-anchors)
50 - [`#contents`](https://eemeli.org/yaml/#content-nodes)
51 - [`#directives`](https://eemeli.org/yaml/#stream-directives)
52 - [`#errors`](https://eemeli.org/yaml/#errors)
53 - [`#warnings`](https://eemeli.org/yaml/#errors)
54- [`isDocument(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
55- [`parseAllDocuments(str, options?): Document[]`](https://eemeli.org/yaml/#parsing-documents)
56- [`parseDocument(str, options?): Document`](https://eemeli.org/yaml/#parsing-documents)
57
58### Content Nodes
59
60- [`isAlias(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
61- [`isCollection(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
62- [`isMap(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
63- [`isNode(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
64- [`isPair(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
65- [`isScalar(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
66- [`isSeq(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
67- [`new Scalar(value)`](https://eemeli.org/yaml/#scalar-values)
68- [`new YAMLMap()`](https://eemeli.org/yaml/#collections)
69- [`new YAMLSeq()`](https://eemeli.org/yaml/#collections)
70- [`doc.createAlias(node, name?): Alias`](https://eemeli.org/yaml/#working-with-anchors)
71- [`doc.createNode(value, options?): Node`](https://eemeli.org/yaml/#creating-nodes)
72- [`doc.createPair(key, value): Pair`](https://eemeli.org/yaml/#creating-nodes)
73- [`visit(node, visitor)`](https://eemeli.org/yaml/#modifying-nodes)
74
75### Parsing YAML
76
77- [`new Lexer().lex(src)`](https://eemeli.org/yaml/#lexer)
78- [`new Parser(onNewLine?).parse(src)`](https://eemeli.org/yaml/#parser)
79- [`new Composer(options?).compose(tokens)`](https://eemeli.org/yaml/#composer)
80
81## YAML.parse
82
83```yaml
84# file.yml
85YAML:
86 - A human-readable data serialization language
87 - https://en.wikipedia.org/wiki/YAML
88yaml:
89 - A complete JavaScript implementation
90 - https://www.npmjs.com/package/yaml
91```
92
93```js
94import fs from 'fs'
95import YAML from 'yaml'
96
97YAML.parse('3.14159')
98// 3.14159
99
100YAML.parse('[ true, false, maybe, null ]\n')
101// [ true, false, 'maybe', null ]
102
103const file = fs.readFileSync('./file.yml', 'utf8')
104YAML.parse(file)
105// { YAML:
106// [ 'A human-readable data serialization language',
107// 'https://en.wikipedia.org/wiki/YAML' ],
108// yaml:
109// [ 'A complete JavaScript implementation',
110// 'https://www.npmjs.com/package/yaml' ] }
111```
112
113## YAML.stringify
114
115```js
116import YAML from 'yaml'
117
118YAML.stringify(3.14159)
119// '3.14159\n'
120
121YAML.stringify([true, false, 'maybe', null])
122// `- true
123// - false
124// - maybe
125// - null
126// `
127
128YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
129// `number: 3
130// plain: string
131// block: |
132// two
133// lines
134// `
135```
136
137---
138
139Browser testing provided by:
140
141<a href="https://www.browserstack.com/open-source">
142<img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" />
143</a>