Swap-Aggregator-Subgraph/node_modules/yaml/dist/schema/Merge.js
2022-07-03 07:27:35 +05:30

75 lines
2.0 KiB
JavaScript

"use strict";
exports.Merge = exports.MERGE_KEY = void 0;
var _Map = require("./Map");
var _Pair = require("./Pair");
var _Scalar = require("./Scalar");
var _Seq = require("./Seq");
const MERGE_KEY = '<<';
exports.MERGE_KEY = MERGE_KEY;
class Merge extends _Pair.Pair {
constructor(pair) {
if (pair instanceof _Pair.Pair) {
let seq = pair.value;
if (!(seq instanceof _Seq.YAMLSeq)) {
seq = new _Seq.YAMLSeq();
seq.items.push(pair.value);
seq.range = pair.value.range;
}
super(pair.key, seq);
this.range = pair.range;
} else {
super(new _Scalar.Scalar(MERGE_KEY), new _Seq.YAMLSeq());
}
this.type = _Pair.Pair.Type.MERGE_PAIR;
} // If the value associated with a merge key is a single mapping node, each of
// its key/value pairs is inserted into the current mapping, unless the key
// already exists in it. If the value associated with the merge key is a
// sequence, then this sequence is expected to contain mapping nodes and each
// of these nodes is merged in turn according to its order in the sequence.
// Keys in mapping nodes earlier in the sequence override keys specified in
// later mapping nodes. -- http://yaml.org/type/merge.html
addToJSMap(ctx, map) {
for (const {
source
} of this.value.items) {
if (!(source instanceof _Map.YAMLMap)) throw new Error('Merge sources must be maps');
const srcMap = source.toJSON(null, ctx, Map);
for (const [key, value] of srcMap) {
if (map instanceof Map) {
if (!map.has(key)) map.set(key, value);
} else if (map instanceof Set) {
map.add(key);
} else {
if (!Object.prototype.hasOwnProperty.call(map, key)) map[key] = value;
}
}
}
return map;
}
toString(ctx, onComment) {
const seq = this.value;
if (seq.items.length > 1) return super.toString(ctx, onComment);
this.value = seq.items[0];
const str = super.toString(ctx, onComment);
this.value = seq;
return str;
}
}
exports.Merge = Merge;