2022-10-21 14:44:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-01-17 09:08:10 +00:00
|
|
|
var identity = require('../../nodes/identity.js');
|
2022-10-21 14:44:44 +00:00
|
|
|
var toJS = require('../../nodes/toJS.js');
|
|
|
|
var YAMLMap = require('../../nodes/YAMLMap.js');
|
2024-01-17 09:08:10 +00:00
|
|
|
var YAMLSeq = require('../../nodes/YAMLSeq.js');
|
2022-10-21 14:44:44 +00:00
|
|
|
var pairs = require('./pairs.js');
|
|
|
|
|
|
|
|
class YAMLOMap extends YAMLSeq.YAMLSeq {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.add = YAMLMap.YAMLMap.prototype.add.bind(this);
|
|
|
|
this.delete = YAMLMap.YAMLMap.prototype.delete.bind(this);
|
|
|
|
this.get = YAMLMap.YAMLMap.prototype.get.bind(this);
|
|
|
|
this.has = YAMLMap.YAMLMap.prototype.has.bind(this);
|
|
|
|
this.set = YAMLMap.YAMLMap.prototype.set.bind(this);
|
|
|
|
this.tag = YAMLOMap.tag;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If `ctx` is given, the return type is actually `Map<unknown, unknown>`,
|
|
|
|
* but TypeScript won't allow widening the signature of a child method.
|
|
|
|
*/
|
|
|
|
toJSON(_, ctx) {
|
|
|
|
if (!ctx)
|
|
|
|
return super.toJSON(_);
|
|
|
|
const map = new Map();
|
|
|
|
if (ctx?.onCreate)
|
|
|
|
ctx.onCreate(map);
|
|
|
|
for (const pair of this.items) {
|
|
|
|
let key, value;
|
2024-01-17 09:08:10 +00:00
|
|
|
if (identity.isPair(pair)) {
|
2022-10-21 14:44:44 +00:00
|
|
|
key = toJS.toJS(pair.key, '', ctx);
|
|
|
|
value = toJS.toJS(pair.value, key, ctx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
key = toJS.toJS(pair, '', ctx);
|
|
|
|
}
|
|
|
|
if (map.has(key))
|
|
|
|
throw new Error('Ordered maps must not include duplicate keys');
|
|
|
|
map.set(key, value);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
2024-01-17 09:08:10 +00:00
|
|
|
static from(schema, iterable, ctx) {
|
|
|
|
const pairs$1 = pairs.createPairs(schema, iterable, ctx);
|
|
|
|
const omap = new this();
|
|
|
|
omap.items = pairs$1.items;
|
|
|
|
return omap;
|
|
|
|
}
|
2022-10-21 14:44:44 +00:00
|
|
|
}
|
|
|
|
YAMLOMap.tag = 'tag:yaml.org,2002:omap';
|
|
|
|
const omap = {
|
|
|
|
collection: 'seq',
|
|
|
|
identify: value => value instanceof Map,
|
|
|
|
nodeClass: YAMLOMap,
|
|
|
|
default: false,
|
|
|
|
tag: 'tag:yaml.org,2002:omap',
|
|
|
|
resolve(seq, onError) {
|
|
|
|
const pairs$1 = pairs.resolvePairs(seq, onError);
|
|
|
|
const seenKeys = [];
|
|
|
|
for (const { key } of pairs$1.items) {
|
2024-01-17 09:08:10 +00:00
|
|
|
if (identity.isScalar(key)) {
|
2022-10-21 14:44:44 +00:00
|
|
|
if (seenKeys.includes(key.value)) {
|
|
|
|
onError(`Ordered maps must not include duplicate keys: ${key.value}`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
seenKeys.push(key.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Object.assign(new YAMLOMap(), pairs$1);
|
|
|
|
},
|
2024-01-17 09:08:10 +00:00
|
|
|
createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx)
|
2022-10-21 14:44:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.YAMLOMap = YAMLOMap;
|
|
|
|
exports.omap = omap;
|