public-api.js 4.0 KB

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