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.

Collection.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.isEmptyPath = void 0;
  6. var _addComment = _interopRequireDefault(require("../addComment"));
  7. var _Node = _interopRequireDefault(require("./Node"));
  8. var _Pair = _interopRequireDefault(require("./Pair"));
  9. var _Scalar = _interopRequireDefault(require("./Scalar"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  12. // null, undefined, or an empty non-string iterable (e.g. [])
  13. const isEmptyPath = path => path == null || typeof path === 'object' && path[Symbol.iterator]().next().done;
  14. exports.isEmptyPath = isEmptyPath;
  15. class Collection extends _Node.default {
  16. constructor(...args) {
  17. super(...args);
  18. _defineProperty(this, "items", []);
  19. }
  20. addIn(path, value) {
  21. if (isEmptyPath(path)) this.add(value);else {
  22. const [key, ...rest] = path;
  23. const node = this.get(key, true);
  24. if (node instanceof Collection) node.addIn(rest, value);else throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25. }
  26. }
  27. deleteIn([key, ...rest]) {
  28. if (rest.length === 0) return this.delete(key);
  29. const node = this.get(key, true);
  30. if (node instanceof Collection) return node.deleteIn(rest);else throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  31. }
  32. getIn([key, ...rest], keepScalar) {
  33. const node = this.get(key, true);
  34. if (rest.length === 0) return !keepScalar && node instanceof _Scalar.default ? node.value : node;else return node instanceof Collection ? node.getIn(rest, keepScalar) : undefined;
  35. }
  36. hasAllNullValues() {
  37. return this.items.every(node => {
  38. if (!(node instanceof _Pair.default)) return false;
  39. const n = node.value;
  40. return n == null || n instanceof _Scalar.default && n.value == null && !n.commentBefore && !n.comment && !n.tag;
  41. });
  42. }
  43. hasIn([key, ...rest]) {
  44. if (rest.length === 0) return this.has(key);
  45. const node = this.get(key, true);
  46. return node instanceof Collection ? node.hasIn(rest) : false;
  47. }
  48. setIn([key, ...rest], value) {
  49. if (rest.length === 0) {
  50. this.set(key, value);
  51. } else {
  52. const node = this.get(key, true);
  53. if (node instanceof Collection) node.setIn(rest, value);else throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  54. }
  55. } // overridden in implementations
  56. toJSON() {
  57. return null;
  58. }
  59. toString(ctx, {
  60. blockItem,
  61. flowChars,
  62. isMap,
  63. itemIndent
  64. }, onComment, onChompKeep) {
  65. const {
  66. doc,
  67. indent
  68. } = ctx;
  69. const inFlow = this.type && this.type.substr(0, 4) === 'FLOW' || ctx.inFlow;
  70. if (inFlow) itemIndent += ' ';
  71. const allNullValues = isMap && this.hasAllNullValues();
  72. ctx = Object.assign({}, ctx, {
  73. allNullValues,
  74. indent: itemIndent,
  75. inFlow,
  76. type: null
  77. });
  78. let chompKeep = false;
  79. let hasItemWithNewLine = false;
  80. const nodes = this.items.reduce((nodes, item, i) => {
  81. let comment;
  82. if (item) {
  83. if (!chompKeep && item.spaceBefore) nodes.push({
  84. type: 'comment',
  85. str: ''
  86. });
  87. if (item.commentBefore) item.commentBefore.match(/^.*$/gm).forEach(line => {
  88. nodes.push({
  89. type: 'comment',
  90. str: `#${line}`
  91. });
  92. });
  93. if (item.comment) comment = item.comment;
  94. if (inFlow && (!chompKeep && item.spaceBefore || item.commentBefore || item.comment || item.key && (item.key.commentBefore || item.key.comment) || item.value && (item.value.commentBefore || item.value.comment))) hasItemWithNewLine = true;
  95. }
  96. chompKeep = false;
  97. let str = doc.schema.stringify(item, ctx, () => comment = null, () => chompKeep = true);
  98. if (inFlow && !hasItemWithNewLine && str.includes('\n')) hasItemWithNewLine = true;
  99. if (inFlow && i < this.items.length - 1) str += ',';
  100. str = (0, _addComment.default)(str, itemIndent, comment);
  101. if (chompKeep && (comment || inFlow)) chompKeep = false;
  102. nodes.push({
  103. type: 'item',
  104. str
  105. });
  106. return nodes;
  107. }, []);
  108. let str;
  109. if (nodes.length === 0) {
  110. str = flowChars.start + flowChars.end;
  111. } else if (inFlow) {
  112. const {
  113. start,
  114. end
  115. } = flowChars;
  116. const strings = nodes.map(n => n.str);
  117. if (hasItemWithNewLine || strings.reduce((sum, str) => sum + str.length + 2, 2) > Collection.maxFlowStringSingleLineLength) {
  118. str = start;
  119. for (const s of strings) {
  120. str += s ? `\n ${indent}${s}` : '\n';
  121. }
  122. str += `\n${indent}${end}`;
  123. } else {
  124. str = `${start} ${strings.join(' ')} ${end}`;
  125. }
  126. } else {
  127. const strings = nodes.map(blockItem);
  128. str = strings.shift();
  129. for (const s of strings) str += s ? `\n${indent}${s}` : '\n';
  130. }
  131. if (this.comment) {
  132. str += '\n' + this.comment.replace(/^/gm, `${indent}#`);
  133. if (onComment) onComment();
  134. } else if (chompKeep && onChompKeep) onChompKeep();
  135. return str;
  136. }
  137. }
  138. exports.default = Collection;
  139. _defineProperty(Collection, "maxFlowStringSingleLineLength", 60);