| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.findPair = findPair;
- exports.default = void 0;
-
- var _Collection = _interopRequireDefault(require("./Collection"));
-
- var _Pair = _interopRequireDefault(require("./Pair"));
-
- var _Scalar = _interopRequireDefault(require("./Scalar"));
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function findPair(items, key) {
- const k = key instanceof _Scalar.default ? key.value : key;
-
- for (const it of items) {
- if (it instanceof _Pair.default) {
- if (it.key === key || it.key === k) return it;
- if (it.key && it.key.value === k) return it;
- }
- }
-
- return undefined;
- }
-
- class YAMLMap extends _Collection.default {
- add(pair) {
- if (!pair) pair = new _Pair.default(pair);else if (!(pair instanceof _Pair.default)) pair = new _Pair.default(pair.key || pair, pair.value);
- const prev = findPair(this.items, pair.key);
- if (prev) throw new Error(`Key ${pair.key} already set`);
- this.items.push(pair);
- }
-
- delete(key) {
- const it = findPair(this.items, key);
- if (!it) return false;
- const del = this.items.splice(this.items.indexOf(it), 1);
- return del.length > 0;
- }
-
- get(key, keepScalar) {
- const it = findPair(this.items, key);
- const node = it && it.value;
- return !keepScalar && node instanceof _Scalar.default ? node.value : node;
- }
-
- has(key) {
- return !!findPair(this.items, key);
- }
-
- set(key, value) {
- const prev = findPair(this.items, key);
- if (prev) prev.value = value;else this.items.push(new _Pair.default(key, value));
- }
- /**
- * @param {*} arg ignored
- * @param {*} ctx Conversion context, originally set in Document#toJSON()
- * @param {Class} Type If set, forces the returned collection type
- * @returns {*} Instance of Type, Map, or Object
- */
-
-
- toJSON(_, ctx, Type) {
- const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
- if (ctx && ctx.onCreate) ctx.onCreate(map);
-
- for (const item of this.items) item.addToJSMap(ctx, map);
-
- return map;
- }
-
- toString(ctx, onComment, onChompKeep) {
- if (!ctx) return JSON.stringify(this);
-
- for (const item of this.items) {
- if (!(item instanceof _Pair.default)) throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
- }
-
- return super.toString(ctx, {
- blockItem: n => n.str,
- flowChars: {
- start: '{',
- end: '}'
- },
- isMap: true,
- itemIndent: ctx.indent || ''
- }, onComment, onChompKeep);
- }
-
- }
-
- exports.default = YAMLMap;
|