public-api.js 3.8 KB

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