dashboardjavacloudcoapiotiot-analyticsiot-platformiot-solutionskafkalwm2mmicroservicesmiddlewaremqttnettyplatformsnmpthingsboardvisualizationwebsocketswidgets
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.
96 lines
2.6 KiB
96 lines
2.6 KiB
"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;
|