UNPKG

4.19 kBJavaScriptView Raw
1'use strict';
2
3var composer = require('./compose/composer.js');
4var Document = require('./doc/Document.js');
5var errors = require('./errors.js');
6var log = require('./log.js');
7var identity = require('./nodes/identity.js');
8var lineCounter = require('./parse/line-counter.js');
9var parser = require('./parse/parser.js');
10
11function 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 * Parse the input as a stream of YAML documents.
18 *
19 * Documents should be separated from each other by `...` or `---` marker lines.
20 *
21 * @returns If an empty `docs` array is returned, it will be of type
22 * EmptyStream and contain additional stream information. In
23 * TypeScript, you should use `'empty' in docs` as a type guard for it.
24 */
25function 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/** Parse an input string into a single YAML.Document */
40function 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 // `doc` is always set by compose.end(true) at the very latest
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}
60function 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}
80function 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
104exports.parse = parse;
105exports.parseAllDocuments = parseAllDocuments;
106exports.parseDocument = parseDocument;
107exports.stringify = stringify;