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 identity = require('./nodes/identity.js');
|
8 | var lineCounter = require('./parse/line-counter.js');
|
9 | var parser = require('./parse/parser.js');
|
10 |
|
11 | function parseOptions(options) {
|
12 | const prettyErrors = options.prettyErrors !== false;
|
13 | const lineCounter$1 = options.lineCounter || (prettyErrors && new lineCounter.LineCounter()) || null;
|
14 | return { lineCounter: lineCounter$1, prettyErrors };
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function parseAllDocuments(source, options = {}) {
|
26 | const { lineCounter, prettyErrors } = parseOptions(options);
|
27 | const parser$1 = new parser.Parser(lineCounter?.addNewLine);
|
28 | const composer$1 = new composer.Composer(options);
|
29 | const docs = Array.from(composer$1.compose(parser$1.parse(source)));
|
30 | if (prettyErrors && lineCounter)
|
31 | for (const doc of docs) {
|
32 | doc.errors.forEach(errors.prettifyError(source, lineCounter));
|
33 | doc.warnings.forEach(errors.prettifyError(source, lineCounter));
|
34 | }
|
35 | if (docs.length > 0)
|
36 | return docs;
|
37 | return Object.assign([], { empty: true }, composer$1.streamInfo());
|
38 | }
|
39 |
|
40 | function parseDocument(source, options = {}) {
|
41 | const { lineCounter, prettyErrors } = parseOptions(options);
|
42 | const parser$1 = new parser.Parser(lineCounter?.addNewLine);
|
43 | const composer$1 = new composer.Composer(options);
|
44 |
|
45 | let doc = null;
|
46 | for (const _doc of composer$1.compose(parser$1.parse(source), true, source.length)) {
|
47 | if (!doc)
|
48 | doc = _doc;
|
49 | else if (doc.options.logLevel !== 'silent') {
|
50 | doc.errors.push(new errors.YAMLParseError(_doc.range.slice(0, 2), 'MULTIPLE_DOCS', 'Source contains multiple documents; please use YAML.parseAllDocuments()'));
|
51 | break;
|
52 | }
|
53 | }
|
54 | if (prettyErrors && lineCounter) {
|
55 | doc.errors.forEach(errors.prettifyError(source, lineCounter));
|
56 | doc.warnings.forEach(errors.prettifyError(source, lineCounter));
|
57 | }
|
58 | return doc;
|
59 | }
|
60 | function parse(src, reviver, options) {
|
61 | let _reviver = undefined;
|
62 | if (typeof reviver === 'function') {
|
63 | _reviver = reviver;
|
64 | }
|
65 | else if (options === undefined && reviver && typeof reviver === 'object') {
|
66 | options = reviver;
|
67 | }
|
68 | const doc = parseDocument(src, options);
|
69 | if (!doc)
|
70 | return null;
|
71 | doc.warnings.forEach(warning => log.warn(doc.options.logLevel, warning));
|
72 | if (doc.errors.length > 0) {
|
73 | if (doc.options.logLevel !== 'silent')
|
74 | throw doc.errors[0];
|
75 | else
|
76 | doc.errors = [];
|
77 | }
|
78 | return doc.toJS(Object.assign({ reviver: _reviver }, options));
|
79 | }
|
80 | function stringify(value, replacer, options) {
|
81 | let _replacer = null;
|
82 | if (typeof replacer === 'function' || Array.isArray(replacer)) {
|
83 | _replacer = replacer;
|
84 | }
|
85 | else if (options === undefined && replacer) {
|
86 | options = replacer;
|
87 | }
|
88 | if (typeof options === 'string')
|
89 | options = options.length;
|
90 | if (typeof options === 'number') {
|
91 | const indent = Math.round(options);
|
92 | options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
|
93 | }
|
94 | if (value === undefined) {
|
95 | const { keepUndefined } = options ?? replacer ?? {};
|
96 | if (!keepUndefined)
|
97 | return undefined;
|
98 | }
|
99 | if (identity.isDocument(value) && !_replacer)
|
100 | return value.toString(options);
|
101 | return new Document.Document(value, _replacer, options).toString(options);
|
102 | }
|
103 |
|
104 | exports.parse = parse;
|
105 | exports.parseAllDocuments = parseAllDocuments;
|
106 | exports.parseDocument = parseDocument;
|
107 | exports.stringify = stringify;
|