YAMLMap.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. var stringifyCollection = require('../stringify/stringifyCollection.js');
  3. var addPairToJSMap = require('./addPairToJSMap.js');
  4. var Collection = require('./Collection.js');
  5. var identity = require('./identity.js');
  6. var Pair = require('./Pair.js');
  7. var Scalar = require('./Scalar.js');
  8. function findPair(items, key) {
  9. const k = identity.isScalar(key) ? key.value : key;
  10. for (const it of items) {
  11. if (identity.isPair(it)) {
  12. if (it.key === key || it.key === k)
  13. return it;
  14. if (identity.isScalar(it.key) && it.key.value === k)
  15. return it;
  16. }
  17. }
  18. return undefined;
  19. }
  20. class YAMLMap extends Collection.Collection {
  21. static get tagName() {
  22. return 'tag:yaml.org,2002:map';
  23. }
  24. constructor(schema) {
  25. super(identity.MAP, schema);
  26. this.items = [];
  27. }
  28. /**
  29. * A generic collection parsing method that can be extended
  30. * to other node classes that inherit from YAMLMap
  31. */
  32. static from(schema, obj, ctx) {
  33. const { keepUndefined, replacer } = ctx;
  34. const map = new this(schema);
  35. const add = (key, value) => {
  36. if (typeof replacer === 'function')
  37. value = replacer.call(obj, key, value);
  38. else if (Array.isArray(replacer) && !replacer.includes(key))
  39. return;
  40. if (value !== undefined || keepUndefined)
  41. map.items.push(Pair.createPair(key, value, ctx));
  42. };
  43. if (obj instanceof Map) {
  44. for (const [key, value] of obj)
  45. add(key, value);
  46. }
  47. else if (obj && typeof obj === 'object') {
  48. for (const key of Object.keys(obj))
  49. add(key, obj[key]);
  50. }
  51. if (typeof schema.sortMapEntries === 'function') {
  52. map.items.sort(schema.sortMapEntries);
  53. }
  54. return map;
  55. }
  56. /**
  57. * Adds a value to the collection.
  58. *
  59. * @param overwrite - If not set `true`, using a key that is already in the
  60. * collection will throw. Otherwise, overwrites the previous value.
  61. */
  62. add(pair, overwrite) {
  63. let _pair;
  64. if (identity.isPair(pair))
  65. _pair = pair;
  66. else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
  67. // In TypeScript, this never happens.
  68. _pair = new Pair.Pair(pair, pair?.value);
  69. }
  70. else
  71. _pair = new Pair.Pair(pair.key, pair.value);
  72. const prev = findPair(this.items, _pair.key);
  73. const sortEntries = this.schema?.sortMapEntries;
  74. if (prev) {
  75. if (!overwrite)
  76. throw new Error(`Key ${_pair.key} already set`);
  77. // For scalars, keep the old node & its comments and anchors
  78. if (identity.isScalar(prev.value) && Scalar.isScalarValue(_pair.value))
  79. prev.value.value = _pair.value;
  80. else
  81. prev.value = _pair.value;
  82. }
  83. else if (sortEntries) {
  84. const i = this.items.findIndex(item => sortEntries(_pair, item) < 0);
  85. if (i === -1)
  86. this.items.push(_pair);
  87. else
  88. this.items.splice(i, 0, _pair);
  89. }
  90. else {
  91. this.items.push(_pair);
  92. }
  93. }
  94. delete(key) {
  95. const it = findPair(this.items, key);
  96. if (!it)
  97. return false;
  98. const del = this.items.splice(this.items.indexOf(it), 1);
  99. return del.length > 0;
  100. }
  101. get(key, keepScalar) {
  102. const it = findPair(this.items, key);
  103. const node = it?.value;
  104. return (!keepScalar && identity.isScalar(node) ? node.value : node) ?? undefined;
  105. }
  106. has(key) {
  107. return !!findPair(this.items, key);
  108. }
  109. set(key, value) {
  110. this.add(new Pair.Pair(key, value), true);
  111. }
  112. /**
  113. * @param ctx - Conversion context, originally set in Document#toJS()
  114. * @param {Class} Type - If set, forces the returned collection type
  115. * @returns Instance of Type, Map, or Object
  116. */
  117. toJSON(_, ctx, Type) {
  118. const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
  119. if (ctx?.onCreate)
  120. ctx.onCreate(map);
  121. for (const item of this.items)
  122. addPairToJSMap.addPairToJSMap(ctx, map, item);
  123. return map;
  124. }
  125. toString(ctx, onComment, onChompKeep) {
  126. if (!ctx)
  127. return JSON.stringify(this);
  128. for (const item of this.items) {
  129. if (!identity.isPair(item))
  130. throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
  131. }
  132. if (!ctx.allNullValues && this.hasAllNullValues(false))
  133. ctx = Object.assign({}, ctx, { allNullValues: true });
  134. return stringifyCollection.stringifyCollection(this, ctx, {
  135. blockItemPrefix: '',
  136. flowChars: { start: '{', end: '}' },
  137. itemIndent: ctx.indent || '',
  138. onChompKeep,
  139. onComment
  140. });
  141. }
  142. }
  143. exports.YAMLMap = YAMLMap;
  144. exports.findPair = findPair;