import _typeof from "@babel/runtime/helpers/typeof"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _get from "@babel/runtime/helpers/get"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { YAMLSemanticError } from '../../errors'; import YAMLMap, { findPair } from '../../schema/Map'; import Pair from '../../schema/Pair'; import parseMap from '../../schema/parseMap'; import Scalar from '../../schema/Scalar'; export var YAMLSet = /*#__PURE__*/function (_YAMLMap) { _inherits(YAMLSet, _YAMLMap); function YAMLSet() { var _this; _classCallCheck(this, YAMLSet); _this = _possibleConstructorReturn(this, _getPrototypeOf(YAMLSet).call(this)); _this.tag = YAMLSet.tag; return _this; } _createClass(YAMLSet, [{ key: "add", value: function add(key) { var pair = key instanceof Pair ? key : new Pair(key); var prev = findPair(this.items, pair.key); if (!prev) this.items.push(pair); } }, { key: "get", value: function get(key, keepPair) { var pair = findPair(this.items, key); return !keepPair && pair instanceof Pair ? pair.key instanceof Scalar ? pair.key.value : pair.key : pair; } }, { key: "set", value: function set(key, value) { if (typeof value !== 'boolean') throw new Error("Expected boolean value for set(key, value) in a YAML set, not ".concat(_typeof(value))); var prev = findPair(this.items, key); if (prev && !value) { this.items.splice(this.items.indexOf(prev), 1); } else if (!prev && value) { this.items.push(new Pair(key)); } } }, { key: "toJSON", value: function toJSON(_, ctx) { return _get(_getPrototypeOf(YAMLSet.prototype), "toJSON", this).call(this, _, ctx, Set); } }, { key: "toString", value: function toString(ctx, onComment, onChompKeep) { if (!ctx) return JSON.stringify(this); if (this.hasAllNullValues()) return _get(_getPrototypeOf(YAMLSet.prototype), "toString", this).call(this, ctx, onComment, onChompKeep);else throw new Error('Set items must all have null values'); } }]); return YAMLSet; }(YAMLMap); _defineProperty(YAMLSet, "tag", 'tag:yaml.org,2002:set'); function parseSet(doc, cst) { var map = parseMap(doc, cst); if (!map.hasAllNullValues()) throw new YAMLSemanticError(cst, 'Set items must all have null values'); return Object.assign(new YAMLSet(), map); } function createSet(schema, iterable, ctx) { var set = new YAMLSet(); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = iterable[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var value = _step.value; set.items.push(schema.createPair(value, null, ctx)); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return set; } export default { identify: function identify(value) { return value instanceof Set; }, nodeClass: YAMLSet, default: false, tag: 'tag:yaml.org,2002:set', resolve: parseSet, createNode: createSet };