[6a3a178] | 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 JavaScript parser and stringifier for [YAML](http://yaml.org/), a human friendly data serialization standard. It supports both parsing and stringifying data using all versions of YAML, along with all common data schemas. As a particularly distinguishing feature, `yaml` fully supports reading and writing comments and blank lines in YAML documents.
|
---|
| 4 |
|
---|
| 5 | The library is released under the ISC open source license, and the code is [available on GitHub](https://github.com/eemeli/yaml/). It has no external dependencies and runs on Node.js 6 and later, and in browsers from IE 11 upwards.
|
---|
| 6 |
|
---|
| 7 | For the purposes of versioning, any changes that break any of the endpoints or APIs documented here will be considered semver-major breaking changes. Undocumented library internals may change between minor versions, and previous APIs may be deprecated (but not removed).
|
---|
| 8 |
|
---|
| 9 | For more information, see the project's documentation site: [**eemeli.org/yaml/v1**](https://eemeli.org/yaml/v1/)
|
---|
| 10 |
|
---|
| 11 | To install:
|
---|
| 12 |
|
---|
| 13 | ```sh
|
---|
| 14 | npm install yaml
|
---|
| 15 | ```
|
---|
| 16 |
|
---|
| 17 | **Note:** This is `yaml@1`. You may also be interested in the next version, currently available as [`yaml@next`](https://www.npmjs.com/package/yaml/v/next).
|
---|
| 18 |
|
---|
| 19 | ## API Overview
|
---|
| 20 |
|
---|
| 21 | The API provided by `yaml` has three layers, depending on how deep you need to go: [Parse & Stringify](https://eemeli.org/yaml/v1/#parse-amp-stringify), [Documents](https://eemeli.org/yaml/#documents), and the [CST Parser](https://eemeli.org/yaml/#cst-parser). 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 is the closest to YAML source, making it fast, raw, and crude.
|
---|
| 22 |
|
---|
| 23 | ```js
|
---|
| 24 | import YAML from 'yaml'
|
---|
| 25 | // or
|
---|
| 26 | const YAML = require('yaml')
|
---|
| 27 | ```
|
---|
| 28 |
|
---|
| 29 | ### Parse & Stringify
|
---|
| 30 |
|
---|
| 31 | - [`YAML.parse(str, options): value`](https://eemeli.org/yaml/v1/#yaml-parse)
|
---|
| 32 | - [`YAML.stringify(value, options): string`](https://eemeli.org/yaml/v1/#yaml-stringify)
|
---|
| 33 |
|
---|
| 34 | ### YAML Documents
|
---|
| 35 |
|
---|
| 36 | - [`YAML.createNode(value, wrapScalars, tag): Node`](https://eemeli.org/yaml/v1/#creating-nodes)
|
---|
| 37 | - [`YAML.defaultOptions`](https://eemeli.org/yaml/v1/#options)
|
---|
| 38 | - [`YAML.Document`](https://eemeli.org/yaml/v1/#yaml-documents)
|
---|
| 39 | - [`constructor(options)`](https://eemeli.org/yaml/v1/#creating-documents)
|
---|
| 40 | - [`defaults`](https://eemeli.org/yaml/v1/#options)
|
---|
| 41 | - [`#anchors`](https://eemeli.org/yaml/v1/#working-with-anchors)
|
---|
| 42 | - [`#contents`](https://eemeli.org/yaml/v1/#content-nodes)
|
---|
| 43 | - [`#errors`](https://eemeli.org/yaml/v1/#errors)
|
---|
| 44 | - [`YAML.parseAllDocuments(str, options): YAML.Document[]`](https://eemeli.org/yaml/v1/#parsing-documents)
|
---|
| 45 | - [`YAML.parseDocument(str, options): YAML.Document`](https://eemeli.org/yaml/v1/#parsing-documents)
|
---|
| 46 |
|
---|
| 47 | ```js
|
---|
| 48 | import { Pair, YAMLMap, YAMLSeq } from 'yaml/types'
|
---|
| 49 | ```
|
---|
| 50 |
|
---|
| 51 | - [`new Pair(key, value)`](https://eemeli.org/yaml/v1/#creating-nodes)
|
---|
| 52 | - [`new YAMLMap()`](https://eemeli.org/yaml/v1/#creating-nodes)
|
---|
| 53 | - [`new YAMLSeq()`](https://eemeli.org/yaml/v1/#creating-nodes)
|
---|
| 54 |
|
---|
| 55 | ### CST Parser
|
---|
| 56 |
|
---|
| 57 | ```js
|
---|
| 58 | import parseCST from 'yaml/parse-cst'
|
---|
| 59 | ```
|
---|
| 60 |
|
---|
| 61 | - [`parseCST(str): CSTDocument[]`](https://eemeli.org/yaml/v1/#parsecst)
|
---|
| 62 | - [`YAML.parseCST(str): CSTDocument[]`](https://eemeli.org/yaml/v1/#parsecst)
|
---|
| 63 |
|
---|
| 64 | ## YAML.parse
|
---|
| 65 |
|
---|
| 66 | ```yaml
|
---|
| 67 | # file.yml
|
---|
| 68 | YAML:
|
---|
| 69 | - A human-readable data serialization language
|
---|
| 70 | - https://en.wikipedia.org/wiki/YAML
|
---|
| 71 | yaml:
|
---|
| 72 | - A complete JavaScript implementation
|
---|
| 73 | - https://www.npmjs.com/package/yaml
|
---|
| 74 | ```
|
---|
| 75 |
|
---|
| 76 | ```js
|
---|
| 77 | import fs from 'fs'
|
---|
| 78 | import YAML from 'yaml'
|
---|
| 79 |
|
---|
| 80 | YAML.parse('3.14159')
|
---|
| 81 | // 3.14159
|
---|
| 82 |
|
---|
| 83 | YAML.parse('[ true, false, maybe, null ]\n')
|
---|
| 84 | // [ true, false, 'maybe', null ]
|
---|
| 85 |
|
---|
| 86 | const file = fs.readFileSync('./file.yml', 'utf8')
|
---|
| 87 | YAML.parse(file)
|
---|
| 88 | // { YAML:
|
---|
| 89 | // [ 'A human-readable data serialization language',
|
---|
| 90 | // 'https://en.wikipedia.org/wiki/YAML' ],
|
---|
| 91 | // yaml:
|
---|
| 92 | // [ 'A complete JavaScript implementation',
|
---|
| 93 | // 'https://www.npmjs.com/package/yaml' ] }
|
---|
| 94 | ```
|
---|
| 95 |
|
---|
| 96 | ## YAML.stringify
|
---|
| 97 |
|
---|
| 98 | ```js
|
---|
| 99 | import YAML from 'yaml'
|
---|
| 100 |
|
---|
| 101 | YAML.stringify(3.14159)
|
---|
| 102 | // '3.14159\n'
|
---|
| 103 |
|
---|
| 104 | YAML.stringify([true, false, 'maybe', null])
|
---|
| 105 | // `- true
|
---|
| 106 | // - false
|
---|
| 107 | // - maybe
|
---|
| 108 | // - null
|
---|
| 109 | // `
|
---|
| 110 |
|
---|
| 111 | YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
|
---|
| 112 | // `number: 3
|
---|
| 113 | // plain: string
|
---|
| 114 | // block: >
|
---|
| 115 | // two
|
---|
| 116 | //
|
---|
| 117 | // lines
|
---|
| 118 | // `
|
---|
| 119 | ```
|
---|
| 120 |
|
---|
| 121 | ---
|
---|
| 122 |
|
---|
| 123 | Browser testing provided by:
|
---|
| 124 |
|
---|
| 125 | <a href="https://www.browserstack.com/open-source">
|
---|
| 126 | <img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" />
|
---|
| 127 | </a>
|
---|