index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. var parseCst = require('./parse-cst.js');
  3. var Document$1 = require('./Document-9b4560a1.js');
  4. var Schema = require('./Schema-88e323a7.js');
  5. var PlainValue = require('./PlainValue-ec8e588e.js');
  6. var warnings = require('./warnings-1000a372.js');
  7. require('./resolveSeq-d03cb037.js');
  8. function createNode(value, wrapScalars = true, tag) {
  9. if (tag === undefined && typeof wrapScalars === 'string') {
  10. tag = wrapScalars;
  11. wrapScalars = true;
  12. }
  13. const options = Object.assign({}, Document$1.Document.defaults[Document$1.defaultOptions.version], Document$1.defaultOptions);
  14. const schema = new Schema.Schema(options);
  15. return schema.createNode(value, wrapScalars, tag);
  16. }
  17. class Document extends Document$1.Document {
  18. constructor(options) {
  19. super(Object.assign({}, Document$1.defaultOptions, options));
  20. }
  21. }
  22. function parseAllDocuments(src, options) {
  23. const stream = [];
  24. let prev;
  25. for (const cstDoc of parseCst.parse(src)) {
  26. const doc = new Document(options);
  27. doc.parse(cstDoc, prev);
  28. stream.push(doc);
  29. prev = doc;
  30. }
  31. return stream;
  32. }
  33. function parseDocument(src, options) {
  34. const cst = parseCst.parse(src);
  35. const doc = new Document(options).parse(cst[0]);
  36. if (cst.length > 1) {
  37. const errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
  38. doc.errors.unshift(new PlainValue.YAMLSemanticError(cst[1], errMsg));
  39. }
  40. return doc;
  41. }
  42. function parse(src, options) {
  43. const doc = parseDocument(src, options);
  44. doc.warnings.forEach(warning => warnings.warn(warning));
  45. if (doc.errors.length > 0) throw doc.errors[0];
  46. return doc.toJSON();
  47. }
  48. function stringify(value, options) {
  49. const doc = new Document(options);
  50. doc.contents = value;
  51. return String(doc);
  52. }
  53. const YAML = {
  54. createNode,
  55. defaultOptions: Document$1.defaultOptions,
  56. Document,
  57. parse,
  58. parseAllDocuments,
  59. parseCST: parseCst.parse,
  60. parseDocument,
  61. scalarOptions: Document$1.scalarOptions,
  62. stringify
  63. };
  64. exports.YAML = YAML;