Dashboard sipadu mbip
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findPair = findPair;
  6. exports.default = void 0;
  7. var _Collection = _interopRequireDefault(require("./Collection"));
  8. var _Pair = _interopRequireDefault(require("./Pair"));
  9. var _Scalar = _interopRequireDefault(require("./Scalar"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function findPair(items, key) {
  12. const k = key instanceof _Scalar.default ? key.value : key;
  13. for (const it of items) {
  14. if (it instanceof _Pair.default) {
  15. if (it.key === key || it.key === k) return it;
  16. if (it.key && it.key.value === k) return it;
  17. }
  18. }
  19. return undefined;
  20. }
  21. class YAMLMap extends _Collection.default {
  22. add(pair) {
  23. if (!pair) pair = new _Pair.default(pair);else if (!(pair instanceof _Pair.default)) pair = new _Pair.default(pair.key || pair, pair.value);
  24. const prev = findPair(this.items, pair.key);
  25. if (prev) throw new Error(`Key ${pair.key} already set`);
  26. this.items.push(pair);
  27. }
  28. delete(key) {
  29. const it = findPair(this.items, key);
  30. if (!it) return false;
  31. const del = this.items.splice(this.items.indexOf(it), 1);
  32. return del.length > 0;
  33. }
  34. get(key, keepScalar) {
  35. const it = findPair(this.items, key);
  36. const node = it && it.value;
  37. return !keepScalar && node instanceof _Scalar.default ? node.value : node;
  38. }
  39. has(key) {
  40. return !!findPair(this.items, key);
  41. }
  42. set(key, value) {
  43. const prev = findPair(this.items, key);
  44. if (prev) prev.value = value;else this.items.push(new _Pair.default(key, value));
  45. }
  46. /**
  47. * @param {*} arg ignored
  48. * @param {*} ctx Conversion context, originally set in Document#toJSON()
  49. * @param {Class} Type If set, forces the returned collection type
  50. * @returns {*} Instance of Type, Map, or Object
  51. */
  52. toJSON(_, ctx, Type) {
  53. const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
  54. if (ctx && ctx.onCreate) ctx.onCreate(map);
  55. for (const item of this.items) item.addToJSMap(ctx, map);
  56. return map;
  57. }
  58. toString(ctx, onComment, onChompKeep) {
  59. if (!ctx) return JSON.stringify(this);
  60. for (const item of this.items) {
  61. if (!(item instanceof _Pair.default)) throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
  62. }
  63. return super.toString(ctx, {
  64. blockItem: n => n.str,
  65. flowChars: {
  66. start: '{',
  67. end: '}'
  68. },
  69. isMap: true,
  70. itemIndent: ctx.indent || ''
  71. }, onComment, onChompKeep);
  72. }
  73. }
  74. exports.default = YAMLMap;