yaml 1.0.0-rc.6 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -2
- package/dist/Anchors.js +74 -104
- package/dist/Document.js +378 -387
- package/dist/addComment.js +3 -3
- package/dist/createNode.js +12 -14
- package/dist/cst/Alias.js +23 -54
- package/dist/cst/BlockValue.js +170 -199
- package/dist/cst/Collection.js +122 -145
- package/dist/cst/CollectionItem.js +58 -83
- package/dist/cst/Comment.js +15 -41
- package/dist/cst/Directive.js +48 -89
- package/dist/cst/Document.js +168 -194
- package/dist/cst/FlowCollection.js +146 -147
- package/dist/cst/Node.js +268 -301
- package/dist/cst/ParseContext.js +135 -157
- package/dist/cst/PlainValue.js +108 -145
- package/dist/cst/QuoteDouble.js +193 -228
- package/dist/cst/QuoteSingle.js +74 -106
- package/dist/cst/Range.js +46 -34
- package/dist/cst/parse.js +33 -9
- package/dist/errors.js +28 -88
- package/dist/foldFlowLines.js +28 -31
- package/dist/index.js +22 -44
- package/dist/listTagNames.js +8 -12
- package/dist/schema/Alias.js +32 -64
- package/dist/schema/Collection.js +75 -115
- package/dist/schema/Map.js +57 -96
- package/dist/schema/Merge.js +19 -56
- package/dist/schema/Node.js +4 -6
- package/dist/schema/Pair.js +68 -100
- package/dist/schema/Scalar.js +18 -47
- package/dist/schema/Seq.js +24 -61
- package/dist/schema/_binary.js +22 -26
- package/dist/schema/_string.js +114 -81
- package/dist/schema/_timestamp.js +19 -27
- package/dist/schema/core.js +18 -37
- package/dist/schema/failsafe.js +4 -8
- package/dist/schema/index.js +157 -197
- package/dist/schema/json.js +11 -19
- package/dist/schema/parseMap.js +100 -54
- package/dist/schema/parseSeq.js +63 -37
- package/dist/schema/parseUtils.js +12 -11
- package/dist/schema/yaml-1.1.js +27 -52
- package/dist/test-events.js +174 -0
- package/dist/toJSON.js +5 -4
- package/package.json +26 -14
package/README.md
CHANGED
|
@@ -1,5 +1,121 @@
|
|
|
1
|
-
# YAML
|
|
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><a href="https://travis-ci.org/eemeli/yaml"><img align="right" src="https://travis-ci.org/eemeli/yaml.svg?branch=master" title="Build status" /></a>
|
|
2
2
|
|
|
3
|
-
JavaScript parser and stringifier for [YAML
|
|
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 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 is usable in both browser and node environments.
|
|
6
|
+
|
|
7
|
+
For more information, see the project's documentation site: [**eemeli.org/yaml**](https://eemeli.org/yaml/)
|
|
8
|
+
|
|
9
|
+
> To install:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install yaml
|
|
13
|
+
# or
|
|
14
|
+
yarn add yaml
|
|
15
|
+
```
|
|
4
16
|
|
|
5
17
|
Note: `yaml` 0.x and 1.x are rather different implementations. For the earlier `yaml`, see [tj/js-yaml](https://github.com/tj/js-yaml).
|
|
18
|
+
|
|
19
|
+
## API Overview
|
|
20
|
+
|
|
21
|
+
The API provided by `yaml` has three layers, depending on how deep you need to go: [Pure JavaScript](https://eemeli.org/yaml/#pure-javascript), [YAML Documents](https://eemeli.org/yaml/#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, 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
|
+
### Pure JavaScript
|
|
30
|
+
|
|
31
|
+
* [`YAML.parse(str, options): value`](https://eemeli.org/yaml/#yaml-parse)
|
|
32
|
+
* [`YAML.stringify(value, options): string`](https://eemeli.org/yaml/#yaml-stringify)
|
|
33
|
+
|
|
34
|
+
### YAML Documents
|
|
35
|
+
|
|
36
|
+
* [`YAML.createNode(value, wrapScalars): Node`](https://eemeli.org/yaml/#creating-nodes)
|
|
37
|
+
* [`YAML.defaultOptions`](https://eemeli.org/yaml/#options)
|
|
38
|
+
* [`YAML.Document`](https://eemeli.org/yaml/#yaml-documents)
|
|
39
|
+
* [`constructor(options)`](https://eemeli.org/yaml/#creating-documents)
|
|
40
|
+
* [`defaults`](https://eemeli.org/yaml/#options)
|
|
41
|
+
* [`#anchors`](https://eemeli.org/yaml/#working-with-anchors)
|
|
42
|
+
* [`#contents`](https://eemeli.org/yaml/#content-nodes)
|
|
43
|
+
* [`#errors`](https://eemeli.org/yaml/#errors)
|
|
44
|
+
* [`YAML.parseAllDocuments(str, options): YAML.Document[]`](https://eemeli.org/yaml/#parsing-documents)
|
|
45
|
+
* [`YAML.parseDocument(str, options): YAML.Document`](https://eemeli.org/yaml/#parsing-documents)
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import Map from 'yaml/map'
|
|
49
|
+
import Pair from 'yaml/pair'
|
|
50
|
+
import Seq from 'yaml/seq'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
* [`new Map()`](https://eemeli.org/yaml/#creating-nodes)
|
|
54
|
+
* [`new Pair(key, value)`](https://eemeli.org/yaml/#creating-nodes)
|
|
55
|
+
* [`new Seq()`](https://eemeli.org/yaml/#creating-nodes)
|
|
56
|
+
|
|
57
|
+
### CST Parser
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import parseCST from 'yaml/parse-cst'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
* [`parseCST(str): CSTDocument[]`](https://eemeli.org/yaml/#parsecst)
|
|
64
|
+
* [`YAML.parseCST(str): CSTDocument[]`](https://eemeli.org/yaml/#parsecst)
|
|
65
|
+
|
|
66
|
+
## YAML.parse
|
|
67
|
+
|
|
68
|
+
```yaml
|
|
69
|
+
# file.yml
|
|
70
|
+
YAML:
|
|
71
|
+
- A human-readable data serialization language
|
|
72
|
+
- https://en.wikipedia.org/wiki/YAML
|
|
73
|
+
yaml:
|
|
74
|
+
- A complete JavaScript implementation
|
|
75
|
+
- https://www.npmjs.com/package/yaml
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
import fs from 'fs'
|
|
80
|
+
import YAML from 'yaml'
|
|
81
|
+
|
|
82
|
+
YAML.parse('3.14159')
|
|
83
|
+
// 3.14159
|
|
84
|
+
|
|
85
|
+
YAML.parse('[ true, false, maybe, null ]\n')
|
|
86
|
+
// [ true, false, 'maybe', null ]
|
|
87
|
+
|
|
88
|
+
const file = fs.readFileSync('./file.yml', 'utf8')
|
|
89
|
+
YAML.parse(file)
|
|
90
|
+
// { YAML:
|
|
91
|
+
// [ 'A human-readable data serialization language',
|
|
92
|
+
// 'https://en.wikipedia.org/wiki/YAML' ],
|
|
93
|
+
// yaml:
|
|
94
|
+
// [ 'A complete JavaScript implementation',
|
|
95
|
+
// 'https://www.npmjs.com/package/yaml' ] }
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## YAML.stringify
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
import YAML from 'yaml'
|
|
102
|
+
|
|
103
|
+
YAML.stringify(3.14159)
|
|
104
|
+
// '3.14159\n'
|
|
105
|
+
|
|
106
|
+
YAML.stringify([true, false, 'maybe', null])
|
|
107
|
+
// `- true
|
|
108
|
+
// - false
|
|
109
|
+
// - maybe
|
|
110
|
+
// - null
|
|
111
|
+
// `
|
|
112
|
+
|
|
113
|
+
YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
|
|
114
|
+
// `number: 3
|
|
115
|
+
// plain: string
|
|
116
|
+
// block: >
|
|
117
|
+
// two
|
|
118
|
+
//
|
|
119
|
+
// lines
|
|
120
|
+
// `
|
|
121
|
+
```
|
package/dist/Anchors.js
CHANGED
|
@@ -17,132 +17,102 @@ var _Seq = _interopRequireDefault(require("./schema/Seq"));
|
|
|
17
17
|
|
|
18
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
19
|
|
|
20
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
21
|
-
|
|
22
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
23
|
-
|
|
24
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
25
|
-
|
|
26
20
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
27
21
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
function () {
|
|
31
|
-
function Anchors() {
|
|
32
|
-
_classCallCheck(this, Anchors);
|
|
33
|
-
|
|
22
|
+
class Anchors {
|
|
23
|
+
constructor() {
|
|
34
24
|
_defineProperty(this, "map", {});
|
|
35
25
|
}
|
|
36
26
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.setAnchor(node, name);
|
|
41
|
-
return new _Alias.default(node);
|
|
42
|
-
}
|
|
43
|
-
}, {
|
|
44
|
-
key: "createMergePair",
|
|
45
|
-
value: function createMergePair() {
|
|
46
|
-
var _this = this;
|
|
27
|
+
static validAnchorNode(node) {
|
|
28
|
+
return node instanceof _Scalar.default || node instanceof _Seq.default || node instanceof _Map.default;
|
|
29
|
+
}
|
|
47
30
|
|
|
48
|
-
|
|
31
|
+
createAlias(node, name) {
|
|
32
|
+
this.setAnchor(node, name);
|
|
33
|
+
return new _Alias.default(node);
|
|
34
|
+
}
|
|
49
35
|
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
createMergePair(...sources) {
|
|
37
|
+
const merge = new _Merge.default();
|
|
38
|
+
merge.value.items = sources.map(s => {
|
|
39
|
+
if (s instanceof _Alias.default) {
|
|
40
|
+
if (s.source instanceof _Map.default) return s;
|
|
41
|
+
} else if (s instanceof _Map.default) {
|
|
42
|
+
return this.createAlias(s);
|
|
52
43
|
}
|
|
53
44
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return _this.createAlias(s);
|
|
59
|
-
}
|
|
45
|
+
throw new Error('Merge sources must be Map nodes or their Aliases');
|
|
46
|
+
});
|
|
47
|
+
return merge;
|
|
48
|
+
}
|
|
60
49
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return this.map[name];
|
|
50
|
+
getName(node) {
|
|
51
|
+
const map = this.map;
|
|
52
|
+
return Object.keys(map).find(a => map[a] === node);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getNode(name) {
|
|
56
|
+
return this.map[name];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
newName(prefix) {
|
|
60
|
+
const names = Object.keys(this.map);
|
|
61
|
+
|
|
62
|
+
for (let i = 1; true; ++i) {
|
|
63
|
+
const name = `${prefix}${i}`;
|
|
64
|
+
if (!names.includes(name)) return name;
|
|
77
65
|
}
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
value: function newName(prefix) {
|
|
81
|
-
var names = Object.keys(this.map);
|
|
82
|
-
|
|
83
|
-
for (var i = 1; true; ++i) {
|
|
84
|
-
var name = "".concat(prefix).concat(i);
|
|
85
|
-
if (!names.includes(name)) return name;
|
|
86
|
-
}
|
|
87
|
-
} // During parsing, map & aliases contain CST nodes
|
|
66
|
+
} // During parsing, map & aliases contain CST nodes
|
|
67
|
+
|
|
88
68
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
value: function resolveNodes() {
|
|
92
|
-
var map = this.map,
|
|
69
|
+
resolveNodes() {
|
|
70
|
+
const map = this.map,
|
|
93
71
|
_cstAliases = this._cstAliases;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
72
|
+
Object.keys(map).forEach(a => {
|
|
73
|
+
map[a] = map[a].resolved;
|
|
74
|
+
});
|
|
97
75
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
76
|
+
_cstAliases.forEach(a => {
|
|
77
|
+
a.source = a.source.resolved;
|
|
78
|
+
});
|
|
101
79
|
|
|
102
|
-
|
|
80
|
+
delete this._cstAliases;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
setAnchor(node, name) {
|
|
84
|
+
if (node != null && !Anchors.validAnchorNode(node)) {
|
|
85
|
+
throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
|
|
103
86
|
}
|
|
104
|
-
}, {
|
|
105
|
-
key: "setAnchor",
|
|
106
|
-
value: function setAnchor(node, name) {
|
|
107
|
-
if (node != null && !Anchors.validAnchorNode(node)) {
|
|
108
|
-
throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
|
|
109
|
-
}
|
|
110
87
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
88
|
+
if (name && /[\x00-\x19\s,[\]{}]/.test(name)) {
|
|
89
|
+
throw new Error('Anchor names must not contain whitespace or control characters');
|
|
90
|
+
}
|
|
114
91
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return map[a] === node;
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
if (prev) {
|
|
121
|
-
if (!name) {
|
|
122
|
-
return prev;
|
|
123
|
-
} else if (prev !== name) {
|
|
124
|
-
delete map[prev];
|
|
125
|
-
map[name] = node;
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
if (!name) {
|
|
129
|
-
if (!node) return null;
|
|
130
|
-
name = this.newName('a');
|
|
131
|
-
}
|
|
92
|
+
const map = this.map;
|
|
93
|
+
const prev = node && Object.keys(map).find(a => map[a] === node);
|
|
132
94
|
|
|
95
|
+
if (prev) {
|
|
96
|
+
if (!name) {
|
|
97
|
+
return prev;
|
|
98
|
+
} else if (prev !== name) {
|
|
99
|
+
delete map[prev];
|
|
133
100
|
map[name] = node;
|
|
134
101
|
}
|
|
102
|
+
} else {
|
|
103
|
+
if (!name) {
|
|
104
|
+
if (!node) return null;
|
|
105
|
+
name = this.newName('a');
|
|
106
|
+
}
|
|
135
107
|
|
|
136
|
-
|
|
108
|
+
map[name] = node;
|
|
137
109
|
}
|
|
138
|
-
}], [{
|
|
139
|
-
key: "validAnchorNode",
|
|
140
|
-
value: function validAnchorNode(node) {
|
|
141
|
-
return node instanceof _Scalar.default || node instanceof _Seq.default || node instanceof _Map.default;
|
|
142
|
-
}
|
|
143
|
-
}]);
|
|
144
110
|
|
|
145
|
-
|
|
146
|
-
}
|
|
111
|
+
return name;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
}
|
|
147
115
|
|
|
148
|
-
exports.default = Anchors;
|
|
116
|
+
exports.default = Anchors;
|
|
117
|
+
module.exports = exports.default;
|
|
118
|
+
module.exports.default = exports.default;
|