import _toArray from "@babel/runtime/helpers/toArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _typeof from "@babel/runtime/helpers/typeof"; function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } import { addComment } from '../addComment'; import { Type } from '../constants'; import { Node } from './Node'; import { Scalar } from './Scalar'; function collectionFromPath(schema, path, value) { var v = value; for (var i = path.length - 1; i >= 0; --i) { var k = path[i]; var o = Number.isInteger(k) && k >= 0 ? [] : {}; o[k] = v; v = o; } return schema.createNode(v, false); } // null, undefined, or an empty non-string iterable (e.g. []) export var isEmptyPath = function isEmptyPath(path) { return path == null || _typeof(path) === 'object' && path[Symbol.iterator]().next().done; }; export var Collection = /*#__PURE__*/function (_Node) { _inherits(Collection, _Node); var _super = _createSuper(Collection); function Collection(schema) { var _this; _classCallCheck(this, Collection); _this = _super.call(this); _defineProperty(_assertThisInitialized(_this), "items", []); _this.schema = schema; return _this; } _createClass(Collection, [{ key: "addIn", value: function addIn(path, value) { if (isEmptyPath(path)) this.add(value);else { var _path = _toArray(path), key = _path[0], rest = _path.slice(1); var node = this.get(key, true); if (node instanceof Collection) node.addIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest)); } } }, { key: "deleteIn", value: function deleteIn(_ref) { var _ref2 = _toArray(_ref), key = _ref2[0], rest = _ref2.slice(1); if (rest.length === 0) return this.delete(key); var node = this.get(key, true); if (node instanceof Collection) return node.deleteIn(rest);else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest)); } }, { key: "getIn", value: function getIn(_ref3, keepScalar) { var _ref4 = _toArray(_ref3), key = _ref4[0], rest = _ref4.slice(1); var node = this.get(key, true); if (rest.length === 0) return !keepScalar && node instanceof Scalar ? node.value : node;else return node instanceof Collection ? node.getIn(rest, keepScalar) : undefined; } }, { key: "hasAllNullValues", value: function hasAllNullValues() { return this.items.every(function (node) { if (!node || node.type !== 'PAIR') return false; var n = node.value; return n == null || n instanceof Scalar && n.value == null && !n.commentBefore && !n.comment && !n.tag; }); } }, { key: "hasIn", value: function hasIn(_ref5) { var _ref6 = _toArray(_ref5), key = _ref6[0], rest = _ref6.slice(1); if (rest.length === 0) return this.has(key); var node = this.get(key, true); return node instanceof Collection ? node.hasIn(rest) : false; } }, { key: "setIn", value: function setIn(_ref7, value) { var _ref8 = _toArray(_ref7), key = _ref8[0], rest = _ref8.slice(1); if (rest.length === 0) { this.set(key, value); } else { var node = this.get(key, true); if (node instanceof Collection) node.setIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest)); } } // overridden in implementations /* istanbul ignore next */ }, { key: "toJSON", value: function toJSON() { return null; } }, { key: "toString", value: function toString(ctx, _ref9, onComment, onChompKeep) { var _this2 = this; var blockItem = _ref9.blockItem, flowChars = _ref9.flowChars, isMap = _ref9.isMap, itemIndent = _ref9.itemIndent; var _ctx = ctx, doc = _ctx.doc, indent = _ctx.indent, indentStep = _ctx.indentStep; var inFlow = this.type === Type.FLOW_MAP || this.type === Type.FLOW_SEQ || ctx.inFlow; if (inFlow) itemIndent += indentStep; var allNullValues = isMap && this.hasAllNullValues(); ctx = Object.assign({}, ctx, { allNullValues: allNullValues, indent: itemIndent, inFlow: inFlow, type: null }); var chompKeep = false; var hasItemWithNewLine = false; var nodes = this.items.reduce(function (nodes, item, i) { var comment; if (item) { if (!chompKeep && item.spaceBefore) nodes.push({ type: 'comment', str: '' }); if (item.commentBefore) item.commentBefore.match(/^.*$/gm).forEach(function (line) { nodes.push({ type: 'comment', str: "#".concat(line) }); }); if (item.comment) comment = item.comment; if (inFlow && (!chompKeep && item.spaceBefore || item.commentBefore || item.comment || item.key && (item.key.commentBefore || item.key.comment) || item.value && (item.value.commentBefore || item.value.comment))) hasItemWithNewLine = true; } chompKeep = false; var str = doc.schema.stringify(item, ctx, function () { return comment = null; }, function () { return chompKeep = true; }); if (inFlow && !hasItemWithNewLine && str.includes('\n')) hasItemWithNewLine = true; if (inFlow && i < _this2.items.length - 1) str += ','; str = addComment(str, itemIndent, comment); if (chompKeep && (comment || inFlow)) chompKeep = false; nodes.push({ type: 'item', str: str }); return nodes; }, []); var str; if (nodes.length === 0) { str = flowChars.start + flowChars.end; } else if (inFlow) { var start = flowChars.start, end = flowChars.end; var strings = nodes.map(function (n) { return n.str; }); if (hasItemWithNewLine || strings.reduce(function (sum, str) { return sum + str.length + 2; }, 2) > Collection.maxFlowStringSingleLineLength) { str = start; var _iterator = _createForOfIteratorHelper(strings), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var s = _step.value; str += s ? "\n".concat(indentStep).concat(indent).concat(s) : '\n'; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } str += "\n".concat(indent).concat(end); } else { str = "".concat(start, " ").concat(strings.join(' '), " ").concat(end); } } else { var _strings = nodes.map(blockItem); str = _strings.shift(); var _iterator2 = _createForOfIteratorHelper(_strings), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _s = _step2.value; str += _s ? "\n".concat(indent).concat(_s) : '\n'; } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } if (this.comment) { str += '\n' + this.comment.replace(/^/gm, "".concat(indent, "#")); if (onComment) onComment(); } else if (chompKeep && onChompKeep) onChompKeep(); return str; } }]); return Collection; }(Node); _defineProperty(Collection, "maxFlowStringSingleLineLength", 60);