index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-eed30217.js');
  3. var parseCst = require('./parse-3997f544.js');
  4. require('./stringifyNumber-dea1120c.js');
  5. require('./Schema-807430ba.js');
  6. var Document = require('./Document-f89a2614.js');
  7. /* global console, process, YAML_SILENCE_WARNINGS */
  8. function warn(warning, type) {
  9. if (typeof YAML_SILENCE_WARNINGS !== 'undefined' && YAML_SILENCE_WARNINGS) return;
  10. if (typeof process !== 'undefined') {
  11. if (process.env.YAML_SILENCE_WARNINGS) return; // This will throw in Jest if `warning` is an Error instance due to
  12. // https://github.com/facebook/jest/issues/2549
  13. if (process.emitWarning) {
  14. process.emitWarning(warning, type);
  15. return;
  16. }
  17. } // eslint-disable-next-line no-console
  18. console.warn(type ? `${type}: ${warning}` : warning);
  19. }
  20. function parseAllDocuments(src, options) {
  21. const stream = [];
  22. let prev;
  23. for (const cstDoc of parseCst.parse(src)) {
  24. const doc = new Document.Document(undefined, null, options);
  25. doc.parse(cstDoc, prev);
  26. stream.push(doc);
  27. prev = doc;
  28. }
  29. return stream;
  30. }
  31. function parseDocument(src, options) {
  32. const cst = parseCst.parse(src);
  33. const doc = new Document.Document(cst[0], null, options);
  34. if (cst.length > 1) {
  35. const errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
  36. doc.errors.unshift(new _rollupPluginBabelHelpers.YAMLSemanticError(cst[1], errMsg));
  37. }
  38. return doc;
  39. }
  40. function parse(src, reviver, options) {
  41. if (options === undefined && reviver && typeof reviver === 'object') {
  42. options = reviver;
  43. reviver = undefined;
  44. }
  45. const doc = parseDocument(src, options);
  46. doc.warnings.forEach(warning => warn(warning));
  47. if (doc.errors.length > 0) throw doc.errors[0];
  48. return doc.toJS({
  49. reviver
  50. });
  51. }
  52. function stringify(value, replacer, options) {
  53. if (typeof options === 'string') options = options.length;
  54. if (typeof options === 'number') {
  55. const indent = Math.round(options);
  56. options = indent < 1 ? undefined : indent > 8 ? {
  57. indent: 8
  58. } : {
  59. indent
  60. };
  61. }
  62. if (value === undefined) {
  63. const {
  64. keepUndefined
  65. } = options || replacer || {};
  66. if (!keepUndefined) return undefined;
  67. }
  68. return new Document.Document(value, replacer, options).toString();
  69. }
  70. exports.parseCST = parseCst.parse;
  71. exports.Document = Document.Document;
  72. exports.defaultOptions = Document.defaultOptions;
  73. exports.scalarOptions = Document.scalarOptions;
  74. exports.parse = parse;
  75. exports.parseAllDocuments = parseAllDocuments;
  76. exports.parseDocument = parseDocument;
  77. exports.stringify = stringify;