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.

Merge.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.MERGE_KEY = void 0;
  6. var _Map = _interopRequireDefault(require("./Map"));
  7. var _Pair = _interopRequireDefault(require("./Pair"));
  8. var _Scalar = _interopRequireDefault(require("./Scalar"));
  9. var _Seq = _interopRequireDefault(require("./Seq"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. const MERGE_KEY = '<<';
  12. exports.MERGE_KEY = MERGE_KEY;
  13. class Merge extends _Pair.default {
  14. constructor(pair) {
  15. if (pair instanceof _Pair.default) {
  16. let seq = pair.value;
  17. if (!(seq instanceof _Seq.default)) {
  18. seq = new _Seq.default();
  19. seq.items.push(pair.value);
  20. seq.range = pair.value.range;
  21. }
  22. super(pair.key, seq);
  23. this.range = pair.range;
  24. } else {
  25. super(new _Scalar.default(MERGE_KEY), new _Seq.default());
  26. }
  27. this.type = 'MERGE_PAIR';
  28. } // If the value associated with a merge key is a single mapping node, each of
  29. // its key/value pairs is inserted into the current mapping, unless the key
  30. // already exists in it. If the value associated with the merge key is a
  31. // sequence, then this sequence is expected to contain mapping nodes and each
  32. // of these nodes is merged in turn according to its order in the sequence.
  33. // Keys in mapping nodes earlier in the sequence override keys specified in
  34. // later mapping nodes. -- http://yaml.org/type/merge.html
  35. addToJSMap(ctx, map) {
  36. for (const {
  37. source
  38. } of this.value.items) {
  39. if (!(source instanceof _Map.default)) throw new Error('Merge sources must be maps');
  40. const srcMap = source.toJSON(null, ctx, Map);
  41. for (const [key, value] of srcMap) {
  42. if (map instanceof Map) {
  43. if (!map.has(key)) map.set(key, value);
  44. } else if (map instanceof Set) {
  45. map.add(key);
  46. } else {
  47. if (!Object.prototype.hasOwnProperty.call(map, key)) map[key] = value;
  48. }
  49. }
  50. }
  51. return map;
  52. }
  53. toString(ctx, onComment) {
  54. const seq = this.value;
  55. if (seq.items.length > 1) return super.toString(ctx, onComment);
  56. this.value = seq.items[0];
  57. const str = super.toString(ctx, onComment);
  58. this.value = seq;
  59. return str;
  60. }
  61. }
  62. exports.default = Merge;