Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Pair.js 4.5KB

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