Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Pair.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  10. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  11. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  12. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  13. var _addComment = _interopRequireDefault(require("../addComment"));
  14. var _constants = require("../constants");
  15. var _toJSON = _interopRequireDefault(require("../toJSON"));
  16. var _Collection = _interopRequireDefault(require("./Collection"));
  17. var _Node2 = _interopRequireDefault(require("./Node"));
  18. var _Scalar = _interopRequireDefault(require("./Scalar"));
  19. // Published as 'yaml/pair'
  20. var stringifyKey = function stringifyKey(key, jsKey, ctx) {
  21. if (jsKey === null) return '';
  22. if ((0, _typeof2.default)(jsKey) !== 'object') return String(jsKey);
  23. if (key instanceof _Node2.default && ctx && ctx.doc) return key.toString({
  24. anchors: {},
  25. doc: ctx.doc,
  26. indent: '',
  27. inFlow: true,
  28. inStringifyKey: true
  29. });
  30. return JSON.stringify(jsKey);
  31. };
  32. var Pair =
  33. /*#__PURE__*/
  34. function (_Node) {
  35. (0, _inherits2.default)(Pair, _Node);
  36. function Pair(key) {
  37. var _this;
  38. var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  39. (0, _classCallCheck2.default)(this, Pair);
  40. _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Pair).call(this));
  41. _this.key = key;
  42. _this.value = value;
  43. _this.type = 'PAIR';
  44. return _this;
  45. }
  46. (0, _createClass2.default)(Pair, [{
  47. key: "addToJSMap",
  48. value: function addToJSMap(ctx, map) {
  49. var key = (0, _toJSON.default)(this.key, '', ctx);
  50. if (map instanceof Map) {
  51. var value = (0, _toJSON.default)(this.value, key, ctx);
  52. map.set(key, value);
  53. } else if (map instanceof Set) {
  54. map.add(key);
  55. } else {
  56. var stringKey = stringifyKey(this.key, key, ctx);
  57. map[stringKey] = (0, _toJSON.default)(this.value, stringKey, ctx);
  58. }
  59. return map;
  60. }
  61. }, {
  62. key: "toJSON",
  63. value: function toJSON(_, ctx) {
  64. var pair = ctx && ctx.mapAsMap ? new Map() : {};
  65. return this.addToJSMap(ctx, pair);
  66. }
  67. }, {
  68. key: "toString",
  69. value: function toString(ctx, onComment, onChompKeep) {
  70. if (!ctx || !ctx.doc) return JSON.stringify(this);
  71. var simpleKeys = ctx.doc.options.simpleKeys;
  72. var key = this.key,
  73. value = this.value;
  74. var keyComment = key instanceof _Node2.default && key.comment;
  75. if (simpleKeys) {
  76. if (keyComment) {
  77. throw new Error('With simple keys, key nodes cannot have comments');
  78. }
  79. if (key instanceof _Collection.default) {
  80. var msg = 'With simple keys, collection cannot be used as a key value';
  81. throw new Error(msg);
  82. }
  83. }
  84. var explicitKey = !simpleKeys && (!key || keyComment || key instanceof _Collection.default || key.type === _constants.Type.BLOCK_FOLDED || key.type === _constants.Type.BLOCK_LITERAL);
  85. var _ctx = ctx,
  86. doc = _ctx.doc,
  87. indent = _ctx.indent;
  88. ctx = Object.assign({}, ctx, {
  89. implicitKey: !explicitKey,
  90. indent: indent + ' '
  91. });
  92. var chompKeep = false;
  93. var str = doc.schema.stringify(key, ctx, function () {
  94. return keyComment = null;
  95. }, function () {
  96. return chompKeep = true;
  97. });
  98. str = (0, _addComment.default)(str, ctx.indent, keyComment);
  99. if (ctx.allNullValues && !simpleKeys) {
  100. if (this.comment) {
  101. str = (0, _addComment.default)(str, ctx.indent, this.comment);
  102. if (onComment) onComment();
  103. } else if (chompKeep && !keyComment && onChompKeep) onChompKeep();
  104. return ctx.inFlow ? str : "? ".concat(str);
  105. }
  106. str = explicitKey ? "? ".concat(str, "\n").concat(indent, ":") : "".concat(str, ":");
  107. if (this.comment) {
  108. // expected (but not strictly required) to be a single-line comment
  109. str = (0, _addComment.default)(str, ctx.indent, this.comment);
  110. if (onComment) onComment();
  111. }
  112. var vcb = '';
  113. var valueComment = null;
  114. if (value instanceof _Node2.default) {
  115. if (value.spaceBefore) vcb = '\n';
  116. if (value.commentBefore) {
  117. var cs = value.commentBefore.replace(/^/gm, "".concat(ctx.indent, "#"));
  118. vcb += "\n".concat(cs);
  119. }
  120. valueComment = value.comment;
  121. } else if (value && (0, _typeof2.default)(value) === 'object') {
  122. value = doc.schema.createNode(value, true);
  123. }
  124. ctx.implicitKey = false;
  125. chompKeep = false;
  126. var valueStr = doc.schema.stringify(value, ctx, function () {
  127. return valueComment = null;
  128. }, function () {
  129. return chompKeep = true;
  130. });
  131. var ws = ' ';
  132. if (vcb || this.comment) {
  133. ws = "".concat(vcb, "\n").concat(ctx.indent);
  134. } else if (!explicitKey && value instanceof _Collection.default) {
  135. var flow = valueStr[0] === '[' || valueStr[0] === '{';
  136. if (!flow || valueStr.includes('\n')) ws = "\n".concat(ctx.indent);
  137. }
  138. if (chompKeep && !valueComment && onChompKeep) onChompKeep();
  139. return (0, _addComment.default)(str + ws + valueStr, ctx.indent, valueComment);
  140. }
  141. }, {
  142. key: "commentBefore",
  143. get: function get() {
  144. return this.key && this.key.commentBefore;
  145. },
  146. set: function set(cb) {
  147. if (this.key == null) this.key = new _Scalar.default(null);
  148. this.key.commentBefore = cb;
  149. }
  150. }]);
  151. return Pair;
  152. }(_Node2.default);
  153. exports.default = Pair;