public-api.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Composer } from './compose/composer.js';
  2. import { Document } from './doc/Document.js';
  3. import { prettifyError, YAMLParseError } from './errors.js';
  4. import { warn } from './log.js';
  5. import { isDocument } from './nodes/identity.js';
  6. import { LineCounter } from './parse/line-counter.js';
  7. import { Parser } from './parse/parser.js';
  8. function parseOptions(options) {
  9. const prettyErrors = options.prettyErrors !== false;
  10. const lineCounter = options.lineCounter || (prettyErrors && new LineCounter()) || null;
  11. return { lineCounter, 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 = new Parser(lineCounter?.addNewLine);
  25. const composer = new Composer(options);
  26. const docs = Array.from(composer.compose(parser.parse(source)));
  27. if (prettyErrors && lineCounter)
  28. for (const doc of docs) {
  29. doc.errors.forEach(prettifyError(source, lineCounter));
  30. doc.warnings.forEach(prettifyError(source, lineCounter));
  31. }
  32. if (docs.length > 0)
  33. return docs;
  34. return Object.assign([], { empty: true }, composer.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 = new Parser(lineCounter?.addNewLine);
  40. const composer = new 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.compose(parser.parse(source), true, source.length)) {
  44. if (!doc)
  45. doc = _doc;
  46. else if (doc.options.logLevel !== 'silent') {
  47. doc.errors.push(new 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(prettifyError(source, lineCounter));
  53. doc.warnings.forEach(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 => 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. if (isDocument(value) && !_replacer)
  97. return value.toString(options);
  98. return new Document(value, _replacer, options).toString(options);
  99. }
  100. export { parse, parseAllDocuments, parseDocument, stringify };