public-api.js 4.1 KB

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