1 | 'use strict';
|
---|
2 |
|
---|
3 | var composer = require('./compose/composer.js');
|
---|
4 | var Document = require('./doc/Document.js');
|
---|
5 | var errors = require('./errors.js');
|
---|
6 | var log = require('./log.js');
|
---|
7 | var lineCounter = require('./parse/line-counter.js');
|
---|
8 | var parser = require('./parse/parser.js');
|
---|
9 |
|
---|
10 | function parseOptions(options) {
|
---|
11 | const prettyErrors = options.prettyErrors !== false;
|
---|
12 | const lineCounter$1 = options.lineCounter || (prettyErrors && new lineCounter.LineCounter()) || null;
|
---|
13 | return { lineCounter: lineCounter$1, prettyErrors };
|
---|
14 | }
|
---|
15 | /**
|
---|
16 | * Parse the input as a stream of YAML documents.
|
---|
17 | *
|
---|
18 | * Documents should be separated from each other by `...` or `---` marker lines.
|
---|
19 | *
|
---|
20 | * @returns If an empty `docs` array is returned, it will be of type
|
---|
21 | * EmptyStream and contain additional stream information. In
|
---|
22 | * TypeScript, you should use `'empty' in docs` as a type guard for it.
|
---|
23 | */
|
---|
24 | function parseAllDocuments(source, options = {}) {
|
---|
25 | const { lineCounter, prettyErrors } = parseOptions(options);
|
---|
26 | const parser$1 = new parser.Parser(lineCounter?.addNewLine);
|
---|
27 | const composer$1 = new composer.Composer(options);
|
---|
28 | const docs = Array.from(composer$1.compose(parser$1.parse(source)));
|
---|
29 | if (prettyErrors && lineCounter)
|
---|
30 | for (const doc of docs) {
|
---|
31 | doc.errors.forEach(errors.prettifyError(source, lineCounter));
|
---|
32 | doc.warnings.forEach(errors.prettifyError(source, lineCounter));
|
---|
33 | }
|
---|
34 | if (docs.length > 0)
|
---|
35 | return docs;
|
---|
36 | return Object.assign([], { empty: true }, composer$1.streamInfo());
|
---|
37 | }
|
---|
38 | /** Parse an input string into a single YAML.Document */
|
---|
39 | function parseDocument(source, options = {}) {
|
---|
40 | const { lineCounter, prettyErrors } = parseOptions(options);
|
---|
41 | const parser$1 = new parser.Parser(lineCounter?.addNewLine);
|
---|
42 | const composer$1 = new composer.Composer(options);
|
---|
43 | // `doc` is always set by compose.end(true) at the very latest
|
---|
44 | let doc = null;
|
---|
45 | for (const _doc of composer$1.compose(parser$1.parse(source), true, source.length)) {
|
---|
46 | if (!doc)
|
---|
47 | doc = _doc;
|
---|
48 | else if (doc.options.logLevel !== 'silent') {
|
---|
49 | doc.errors.push(new errors.YAMLParseError(_doc.range.slice(0, 2), 'MULTIPLE_DOCS', 'Source contains multiple documents; please use YAML.parseAllDocuments()'));
|
---|
50 | break;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | if (prettyErrors && lineCounter) {
|
---|
54 | doc.errors.forEach(errors.prettifyError(source, lineCounter));
|
---|
55 | doc.warnings.forEach(errors.prettifyError(source, lineCounter));
|
---|
56 | }
|
---|
57 | return doc;
|
---|
58 | }
|
---|
59 | function parse(src, reviver, options) {
|
---|
60 | let _reviver = undefined;
|
---|
61 | if (typeof reviver === 'function') {
|
---|
62 | _reviver = reviver;
|
---|
63 | }
|
---|
64 | else if (options === undefined && reviver && typeof reviver === 'object') {
|
---|
65 | options = reviver;
|
---|
66 | }
|
---|
67 | const doc = parseDocument(src, options);
|
---|
68 | if (!doc)
|
---|
69 | return null;
|
---|
70 | doc.warnings.forEach(warning => log.warn(doc.options.logLevel, warning));
|
---|
71 | if (doc.errors.length > 0) {
|
---|
72 | if (doc.options.logLevel !== 'silent')
|
---|
73 | throw doc.errors[0];
|
---|
74 | else
|
---|
75 | doc.errors = [];
|
---|
76 | }
|
---|
77 | return doc.toJS(Object.assign({ reviver: _reviver }, options));
|
---|
78 | }
|
---|
79 | function stringify(value, replacer, options) {
|
---|
80 | let _replacer = null;
|
---|
81 | if (typeof replacer === 'function' || Array.isArray(replacer)) {
|
---|
82 | _replacer = replacer;
|
---|
83 | }
|
---|
84 | else if (options === undefined && replacer) {
|
---|
85 | options = replacer;
|
---|
86 | }
|
---|
87 | if (typeof options === 'string')
|
---|
88 | options = options.length;
|
---|
89 | if (typeof options === 'number') {
|
---|
90 | const indent = Math.round(options);
|
---|
91 | options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
|
---|
92 | }
|
---|
93 | if (value === undefined) {
|
---|
94 | const { keepUndefined } = options ?? replacer ?? {};
|
---|
95 | if (!keepUndefined)
|
---|
96 | return undefined;
|
---|
97 | }
|
---|
98 | return new Document.Document(value, _replacer, options).toString(options);
|
---|
99 | }
|
---|
100 |
|
---|
101 | exports.parse = parse;
|
---|
102 | exports.parseAllDocuments = parseAllDocuments;
|
---|
103 | exports.parseDocument = parseDocument;
|
---|
104 | exports.stringify = stringify;
|
---|