Нема описа

set.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. var Node = require('../../nodes/Node.js');
  3. var Pair = require('../../nodes/Pair.js');
  4. var YAMLMap = require('../../nodes/YAMLMap.js');
  5. class YAMLSet extends YAMLMap.YAMLMap {
  6. constructor(schema) {
  7. super(schema);
  8. this.tag = YAMLSet.tag;
  9. }
  10. add(key) {
  11. let pair;
  12. if (Node.isPair(key))
  13. pair = key;
  14. else if (key &&
  15. typeof key === 'object' &&
  16. 'key' in key &&
  17. 'value' in key &&
  18. key.value === null)
  19. pair = new Pair.Pair(key.key, null);
  20. else
  21. pair = new Pair.Pair(key, null);
  22. const prev = YAMLMap.findPair(this.items, pair.key);
  23. if (!prev)
  24. this.items.push(pair);
  25. }
  26. /**
  27. * If `keepPair` is `true`, returns the Pair matching `key`.
  28. * Otherwise, returns the value of that Pair's key.
  29. */
  30. get(key, keepPair) {
  31. const pair = YAMLMap.findPair(this.items, key);
  32. return !keepPair && Node.isPair(pair)
  33. ? Node.isScalar(pair.key)
  34. ? pair.key.value
  35. : pair.key
  36. : pair;
  37. }
  38. set(key, value) {
  39. if (typeof value !== 'boolean')
  40. throw new Error(`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`);
  41. const prev = YAMLMap.findPair(this.items, key);
  42. if (prev && !value) {
  43. this.items.splice(this.items.indexOf(prev), 1);
  44. }
  45. else if (!prev && value) {
  46. this.items.push(new Pair.Pair(key));
  47. }
  48. }
  49. toJSON(_, ctx) {
  50. return super.toJSON(_, ctx, Set);
  51. }
  52. toString(ctx, onComment, onChompKeep) {
  53. if (!ctx)
  54. return JSON.stringify(this);
  55. if (this.hasAllNullValues(true))
  56. return super.toString(Object.assign({}, ctx, { allNullValues: true }), onComment, onChompKeep);
  57. else
  58. throw new Error('Set items must all have null values');
  59. }
  60. }
  61. YAMLSet.tag = 'tag:yaml.org,2002:set';
  62. const set = {
  63. collection: 'map',
  64. identify: value => value instanceof Set,
  65. nodeClass: YAMLSet,
  66. default: false,
  67. tag: 'tag:yaml.org,2002:set',
  68. resolve(map, onError) {
  69. if (Node.isMap(map)) {
  70. if (map.hasAllNullValues(true))
  71. return Object.assign(new YAMLSet(), map);
  72. else
  73. onError('Set items must all have null values');
  74. }
  75. else
  76. onError('Expected a mapping for this tag');
  77. return map;
  78. },
  79. createNode(schema, iterable, ctx) {
  80. const { replacer } = ctx;
  81. const set = new YAMLSet(schema);
  82. if (iterable && Symbol.iterator in Object(iterable))
  83. for (let value of iterable) {
  84. if (typeof replacer === 'function')
  85. value = replacer.call(iterable, value, value);
  86. set.items.push(Pair.createPair(value, null, ctx));
  87. }
  88. return set;
  89. }
  90. };
  91. exports.YAMLSet = YAMLSet;
  92. exports.set = set;