yaml
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
@@ -68,3 +68,3 @@ "use strict"; | ||
while (!Document.atDocumentBoundary(src, offset, _Node2.Char.DIRECTIVES_END)) { | ||
while (!_Node2.default.atDocumentBoundary(src, offset, _Node2.Char.DIRECTIVES_END)) { | ||
offset = Document.startCommentOrEndBlankLine(src, offset); | ||
@@ -100,3 +100,9 @@ | ||
default: | ||
if (hasDirectives) this.error = new _errors.YAMLSyntaxError(this, 'Missing directives-end indicator line'); | ||
if (hasDirectives) { | ||
this.error = new _errors.YAMLSyntaxError(this, 'Missing directives-end indicator line'); | ||
} else if (this.directives.length > 0) { | ||
this.contents = this.directives; | ||
this.directives = []; | ||
} | ||
return offset; | ||
@@ -107,3 +113,10 @@ } | ||
if (src[offset]) return offset + 3; | ||
if (hasDirectives) this.error = new _errors.YAMLSyntaxError(this, 'Missing directives-end indicator line'); | ||
if (hasDirectives) { | ||
this.error = new _errors.YAMLSyntaxError(this, 'Missing directives-end indicator line'); | ||
} else if (this.directives.length > 0) { | ||
this.contents = this.directives; | ||
this.directives = []; | ||
} | ||
return offset; | ||
@@ -117,3 +130,3 @@ } | ||
src = _context.src; | ||
this.contents = []; | ||
if (!this.contents) this.contents = []; | ||
var lineStart = start; | ||
@@ -130,3 +143,3 @@ | ||
while (!Document.atDocumentBoundary(src, offset, _Node2.Char.DOCUMENT_END)) { | ||
while (!_Node2.default.atDocumentBoundary(src, offset, _Node2.Char.DOCUMENT_END)) { | ||
switch (src[offset]) { | ||
@@ -133,0 +146,0 @@ case '\n': |
@@ -14,4 +14,8 @@ "use strict"; | ||
var _Collection = require("./schema/Collection"); | ||
var _Collection = _interopRequireWildcard(require("./schema/Collection")); | ||
var _Node2 = require("./schema/Node"); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
@@ -27,2 +31,6 @@ | ||
var isCollectionItem = function isCollectionItem(node) { | ||
return node && [_Node.Type.MAP_KEY, _Node.Type.MAP_VALUE, _Node.Type.SEQ_ITEM].includes(node.type); | ||
}; | ||
var Document = | ||
@@ -88,4 +96,6 @@ /*#__PURE__*/ | ||
this.version = null; | ||
var directiveComments = []; | ||
directives.forEach(function (directive) { | ||
var name = directive.name; | ||
var comment = directive.comment, | ||
name = directive.name; | ||
@@ -106,7 +116,18 @@ switch (name) { | ||
} | ||
if (comment) directiveComments.push(comment); | ||
}); | ||
var contentNodes = contents.filter(function (node) { | ||
return node.valueRange && !node.valueRange.isEmpty; | ||
}).map(function (node) { | ||
return _this.resolveNode(node); | ||
this.commentBefore = directiveComments.join('\n') || null; | ||
var comments = { | ||
before: [], | ||
after: [] | ||
}; | ||
var contentNodes = []; | ||
contents.forEach(function (node) { | ||
if (node.valueRange && !node.valueRange.isEmpty) { | ||
contentNodes.push(_this.resolveNode(node)); | ||
} else if (node.comment) { | ||
var cc = contentNodes.length === 0 ? comments.before : comments.after; | ||
cc.push(node.comment); | ||
} | ||
}); | ||
@@ -117,2 +138,3 @@ | ||
this.contents = null; | ||
comments.after = comments.before; | ||
break; | ||
@@ -122,2 +144,14 @@ | ||
this.contents = contentNodes[0]; | ||
if (this.contents) { | ||
var cb = comments.before.join('\n') || null; | ||
if (cb) { | ||
var cbNode = this.contents instanceof _Collection.default && this.contents.items[0] ? this.contents.items[0] : this.contents; | ||
cbNode.commentBefore = cbNode.commentBefore ? "".concat(cb, "\n").concat(cbNode.commentBefore) : cb; | ||
} | ||
} else { | ||
comments.after = comments.before.concat(comments.after); | ||
} | ||
break; | ||
@@ -128,3 +162,6 @@ | ||
this.contents = contentNodes; | ||
if (this.contents[0]) this.contents[0].commentBefore = comments.before.join('\n') || null;else comments.after = comments.before.concat(comments.after); | ||
} | ||
this.comment = comments.after.join('\n') || null; | ||
} | ||
@@ -192,3 +229,8 @@ | ||
var hasTag = false; | ||
node.props.forEach(function (_ref3) { | ||
var comments = { | ||
before: [], | ||
after: [] | ||
}; | ||
var props = isCollectionItem(node.context.parent) ? node.context.parent.props.concat(node.props) : node.props; | ||
props.forEach(function (_ref3, i) { | ||
var start = _ref3.start, | ||
@@ -200,2 +242,12 @@ end = _ref3.end; | ||
if (!node.commentHasRequiredWhitespace(start)) errors.push(new _errors.YAMLSyntaxError(node, 'Comments must be separated from other tokens by white space characters')); | ||
var c = node.context.src.slice(start + 1, end); | ||
var header = node.header, | ||
valueRange = node.valueRange; | ||
if (valueRange && (start > valueRange.start || header && start > header.start)) { | ||
comments.after.push(c); | ||
} else { | ||
comments.before.push(c); | ||
} | ||
break; | ||
@@ -214,4 +266,4 @@ | ||
}); | ||
var anchor = node.anchor; | ||
if (anchor) anchors[anchor] = node; | ||
if (hasAnchor) anchors[node.anchor] = node; | ||
var res; | ||
@@ -221,18 +273,38 @@ if (node.type === _Node.Type.ALIAS) { | ||
var src = anchors[node.rawValue]; | ||
if (src) return node.resolved = src.resolved; | ||
errors.push(new _errors.YAMLReferenceError(node, "Aliased anchor not found: ".concat(node.rawValue))); | ||
return null; | ||
if (!src) { | ||
errors.push(new _errors.YAMLReferenceError(node, "Aliased anchor not found: ".concat(node.rawValue))); | ||
return null; | ||
} | ||
res = src.resolved; | ||
} else { | ||
var tagName = this.resolveTagName(node); | ||
if (tagName) { | ||
res = tags.resolve(this, node, tagName); | ||
} else { | ||
if (node.type !== _Node.Type.PLAIN) { | ||
errors.push(new _errors.YAMLSyntaxError(node, "Failed to resolve ".concat(node.type, " node here"))); | ||
return null; | ||
} | ||
try { | ||
res = tags.resolveScalar(node.strValue || ''); | ||
} catch (error) { | ||
if (!error.source) error.source = node; | ||
errors.push(error); | ||
return null; | ||
} | ||
} | ||
} | ||
var tagName = this.resolveTagName(node); | ||
if (tagName) return node.resolved = tags.resolve(this, node, tagName); | ||
if (node.type === _Node.Type.PLAIN) try { | ||
return node.resolved = tags.resolveScalar(node.strValue || ''); | ||
} catch (error) { | ||
if (!error.source) error.source = node; | ||
errors.push(error); | ||
return null; | ||
if (res) { | ||
var cb = comments.before.join('\n'); | ||
if (cb) res.commentBefore = res.commentBefore ? "".concat(res.commentBefore, "\n").concat(cb) : cb; | ||
var ca = comments.after.join('\n'); | ||
if (ca) res.comment = res.comment ? "".concat(res.comment, "\n").concat(ca) : ca; | ||
} | ||
errors.push(new _errors.YAMLSyntaxError(node, "Failed to resolve ".concat(node.type, " node here"))); | ||
return null; | ||
return node.resolved = res; | ||
} | ||
@@ -249,13 +321,45 @@ }, { | ||
if (Array.isArray(this.contents)) { | ||
return this.contents.map(function (c) { | ||
return _this2.tags.stringify(c, { | ||
indent: '' | ||
}); | ||
}).join('\n---\n') + '\n'; | ||
var contents = this.contents; | ||
if (Array.isArray(contents)) { | ||
if (contents.length <= 1) contents = contents[0] || null;else throw new Error('Document with multiple content nodes cannot be stringified'); | ||
} | ||
return this.tags.stringify(this.contents, { | ||
indent: '' | ||
}) + '\n'; | ||
var lines = this.directives.filter(function (_ref4) { | ||
var comment = _ref4.comment; | ||
return comment; | ||
}).map(function (_ref5) { | ||
var comment = _ref5.comment; | ||
return comment.replace(/^/gm, '#'); | ||
}); | ||
var hasDirectives = false; | ||
if (this.version) { | ||
lines.push('%YAML 1.2'); | ||
hasDirectives = true; | ||
} | ||
Object.keys(this.tagPrefixes).forEach(function (handle) { | ||
var prefix = _this2.tagPrefixes[handle]; | ||
lines.push("%TAG ".concat(handle, " ").concat(prefix)); | ||
hasDirectives = true; | ||
}); | ||
if (hasDirectives) lines.push('---'); | ||
if (contents) { | ||
if (contents.commentBefore) lines.push(contents.commentBefore.replace(/^/gm, '#')); | ||
var options = { | ||
// top-level block scalars need to be indented if followed by a comment | ||
forceBlockIndent: !!this.comment, | ||
indent: '' | ||
}; | ||
var comment = contents.comment; | ||
var body = this.tags.stringify(contents, options, function () { | ||
comment = null; | ||
}); | ||
lines.push((0, _Node2.addComment)(body, '', comment)); | ||
} | ||
if (this.comment) lines.push(this.comment.replace(/^/gm, '#')); | ||
return lines.join('\n') + '\n'; | ||
} | ||
@@ -262,0 +366,0 @@ }]); |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.default = resolve; | ||
exports.tokenize = exports.eval = void 0; | ||
@@ -24,2 +25,14 @@ var _parse = _interopRequireDefault(require("./ast/parse")); | ||
}); | ||
} | ||
} | ||
var deprecatedEval = function deprecatedEval() { | ||
throw new Error('The yaml API has changed, try replacing `eval(str)` with `resolve(str)[0].toJSON()`'); | ||
}; | ||
exports.eval = deprecatedEval; | ||
var deprecatedTokenize = function deprecatedTokenize() { | ||
throw new Error('The yaml API has changed, see README.md for more information'); | ||
}; | ||
exports.tokenize = deprecatedTokenize; |
@@ -47,3 +47,4 @@ "use strict"; | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref) { | ||
var value = _ref.value; | ||
var str; | ||
@@ -50,0 +51,0 @@ |
@@ -139,5 +139,6 @@ "use strict"; | ||
function blockString(value, indent, literal) { | ||
function blockString(value, indent, literal, forceBlockIndent, comment, onComment) { | ||
// block can't end in whitespace unless the last line is non-empty | ||
if (/\n[\t ]+$/.test(value)) return doubleQuotedString(value, indent, false); | ||
if (forceBlockIndent && !indent) indent = ' '; | ||
var indentSize = indent ? '2' : '1'; // root is at -1 | ||
@@ -174,2 +175,8 @@ | ||
if (wsStart) wsStart = wsStart.replace(/\n+/g, "$&".concat(indent)); | ||
if (comment) { | ||
header += ' #' + comment.replace(/ ?[\r\n]+/g, ' '); | ||
if (onComment) onComment(); | ||
} | ||
if (!value) return "".concat(header).concat(indentSize, "\n").concat(indent).concat(wsEnd); | ||
@@ -188,3 +195,3 @@ | ||
function plainString(value, indent, implicitKey, inFlow, tags) { | ||
function plainString(value, indent, implicitKey, inFlow, forceBlockIndent, tags, comment, onComment) { | ||
if (implicitKey && /[\n[\]{},]/.test(value) || inFlow && /[[\]{},]/.test(value)) { | ||
@@ -201,3 +208,3 @@ return doubleQuotedString(value, indent, implicitKey); | ||
// - end with ' ' | ||
return implicitKey || inFlow ? doubleQuotedString(value, indent, implicitKey) : blockString(value, indent, false); | ||
return implicitKey || inFlow ? doubleQuotedString(value, indent, implicitKey) : blockString(value, indent, false, forceBlockIndent, comment, onComment); | ||
} // Need to verify that output will be parsed as a string | ||
@@ -207,4 +214,14 @@ | ||
var str = value.replace(/\n+/g, "$&\n".concat(indent)); | ||
var res = tags.resolveScalar(str); | ||
return typeof res === 'string' ? str : doubleQuotedString(value, indent, implicitKey); | ||
if (typeof tags.resolveScalar(str).value !== 'string') { | ||
return doubleQuotedString(value, indent, implicitKey); | ||
} | ||
if (comment && !inFlow && (str.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) { | ||
if (onComment) onComment(); | ||
var cc = comment.replace(/[\s\S]^/gm, "$&".concat(indent, "#")); | ||
return "#".concat(cc, "\n").concat(indent).concat(str); | ||
} | ||
return str; | ||
} | ||
@@ -217,10 +234,15 @@ | ||
options: strOptions, | ||
stringify: function stringify(value) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
implicitKey = _ref.implicitKey, | ||
indent = _ref.indent, | ||
inFlow = _ref.inFlow, | ||
tags = _ref.tags, | ||
type = _ref.type; | ||
stringify: function stringify(_ref) { | ||
var comment = _ref.comment, | ||
value = _ref.value; | ||
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
forceBlockIndent = _ref2.forceBlockIndent, | ||
implicitKey = _ref2.implicitKey, | ||
indent = _ref2.indent, | ||
inFlow = _ref2.inFlow, | ||
tags = _ref2.tags, | ||
type = _ref2.type; | ||
var onComment = arguments.length > 2 ? arguments[2] : undefined; | ||
var dropCR = strOptions.dropCR, | ||
@@ -234,15 +256,15 @@ defaultType = strOptions.defaultType; | ||
case _Node.Type.BLOCK_FOLDED: | ||
return blockString(value, indent, false); | ||
return blockString(value, indent, false, forceBlockIndent, comment, onComment); | ||
case _Node.Type.BLOCK_LITERAL: | ||
return blockString(value, indent, true); | ||
return blockString(value, indent, true, forceBlockIndent, comment, onComment); | ||
case _Node.Type.QUOTE_DOUBLE: | ||
return doubleQuotedString(value, indent, implicitKey); | ||
return doubleQuotedString(value, indent, implicitKey, comment); | ||
case _Node.Type.QUOTE_SINGLE: | ||
return singleQuotedString(value, indent, implicitKey); | ||
return singleQuotedString(value, indent, implicitKey, comment); | ||
case _Node.Type.PLAIN: | ||
return plainString(value, indent, implicitKey, inFlow, tags); | ||
return plainString(value, indent, implicitKey, inFlow, forceBlockIndent, tags, comment, onComment); | ||
@@ -249,0 +271,0 @@ default: |
@@ -18,3 +18,4 @@ "use strict"; | ||
var stringifySexagesimal = function stringifySexagesimal(value) { | ||
var stringifySexagesimal = function stringifySexagesimal(_ref) { | ||
var value = _ref.value; | ||
if (!isNan(value) || !isFinite(value)) return (0, _core.stringifyNumber)(value); | ||
@@ -92,3 +93,4 @@ var sign = ''; | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref2) { | ||
var value = _ref2.value; | ||
return value.toISOString(); | ||
@@ -95,0 +97,0 @@ } |
@@ -6,6 +6,14 @@ "use strict"; | ||
}); | ||
exports.default = exports.Comment = exports.toJSON = void 0; | ||
exports.default = exports.toJSON = void 0; | ||
var _errors = require("../errors"); | ||
var _Node2 = _interopRequireWildcard(require("./Node")); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
@@ -15,3 +23,3 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
@@ -26,14 +34,7 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
var Comment = function Comment(comment, before) { | ||
_classCallCheck(this, Comment); | ||
this.before = before; | ||
this.comment = comment; | ||
}; | ||
exports.Comment = Comment; | ||
var Collection = | ||
/*#__PURE__*/ | ||
function () { | ||
function (_Node) { | ||
_inherits(Collection, _Node); | ||
_createClass(Collection, null, [{ | ||
@@ -65,8 +66,11 @@ key: "checkKeyLength", | ||
function Collection(doc) { | ||
var _this; | ||
_classCallCheck(this, Collection); | ||
this.comments = []; // TODO: include collection & item comments | ||
this.doc = doc; | ||
this.items = []; | ||
_this = _possibleConstructorReturn(this, (Collection.__proto__ || Object.getPrototypeOf(Collection)).call(this)); | ||
_this._comments = []; | ||
_this.doc = doc; | ||
_this.items = []; | ||
return _this; | ||
} | ||
@@ -77,3 +81,25 @@ | ||
value: function addComment(comment) { | ||
this.comments.push(new Comment(comment, this.items.length)); | ||
this._comments.push({ | ||
comment: comment, | ||
before: this.items.length | ||
}); | ||
} | ||
}, { | ||
key: "resolveComments", | ||
value: function resolveComments() { | ||
var _this2 = this; | ||
this._comments.forEach(function (_ref) { | ||
var comment = _ref.comment, | ||
before = _ref.before; | ||
var item = _this2.items[before]; | ||
if (!item) { | ||
if (_this2.comment) _this2.comment += '\n' + comment;else _this2.comment = comment; | ||
} else { | ||
if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment; | ||
} | ||
}); | ||
delete this._comments; | ||
} // overridden in implementations | ||
@@ -86,7 +112,88 @@ | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString(options, onComment) { | ||
var _this3 = this; | ||
var tags = this.doc.tags; | ||
var blockItem = options.blockItem, | ||
flowChars = options.flowChars, | ||
indent = options.indent, | ||
inFlow = options.inFlow, | ||
itemIndent = options.itemIndent; | ||
var opt = { | ||
indent: itemIndent, | ||
inFlow: inFlow, | ||
type: null | ||
}; | ||
var hasItemWithComment = false; | ||
var hasItemWithNewLine = false; | ||
var nodes = this.items.reduce(function (nodes, item, i) { | ||
var commentBefore = item && item.commentBefore; | ||
if (commentBefore) { | ||
hasItemWithComment = true; | ||
commentBefore.match(/^.*$/gm).forEach(function (line) { | ||
nodes.push({ | ||
type: 'comment', | ||
str: "#".concat(line) | ||
}); | ||
}); | ||
} | ||
var comment = item && item.comment; | ||
if (comment) hasItemWithComment = true; | ||
var str = tags.stringify(item, opt, function () { | ||
comment = null; | ||
}); | ||
if (!hasItemWithNewLine && str.indexOf('\n') !== -1) hasItemWithNewLine = true; | ||
if (inFlow && i < _this3.items.length - 1) str += ','; | ||
str = (0, _Node2.addComment)(str, opt.indent, comment); | ||
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 (_ref2) { | ||
var str = _ref2.str; | ||
return str; | ||
}); | ||
if (hasItemWithComment || hasItemWithNewLine || strings.reduce(function (sum, str) { | ||
return sum + str.length + 2; | ||
}, 2) > Collection.maxFlowStringSingleLineLength) { | ||
str = "".concat(start, "\n ").concat(indent).concat(strings.join("\n ".concat(indent)), "\n").concat(indent).concat(end); | ||
} else { | ||
str = "".concat(start, " ").concat(strings.join(' '), " ").concat(end); | ||
} | ||
} else { | ||
str = nodes.map(blockItem).join("\n".concat(indent)); | ||
} | ||
if (this.comment) { | ||
if (!hasItemWithNewLine && str.indexOf('\n') === -1) str = (0, _Node2.addComment)(str, indent, this.comment);else str += '\n' + this.comment.replace(/^/gm, "".concat(indent, "#")); | ||
if (onComment) onComment(); | ||
} | ||
return str; | ||
} | ||
}]); | ||
return Collection; | ||
}(); | ||
}(_Node2.default); | ||
exports.default = Collection; | ||
exports.default = Collection; | ||
Object.defineProperty(Collection, "maxFlowStringSingleLineLength", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: 60 | ||
}); |
@@ -12,3 +12,4 @@ "use strict"; | ||
var stringifyNumber = function stringifyNumber(value) { | ||
var stringifyNumber = function stringifyNumber(_ref) { | ||
var value = _ref.value; | ||
return isFinite(value) ? JSON.stringify(value) : isNaN(value) ? '.nan' : value < 0 ? '-.inf' : '.inf'; | ||
@@ -31,3 +32,3 @@ }; | ||
options: nullOptions, | ||
stringify: function stringify(value) { | ||
stringify: function stringify() { | ||
return nullOptions.nullStr; | ||
@@ -50,3 +51,4 @@ } | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref2) { | ||
var value = _ref2.value; | ||
return '0o' + value.toString(8); | ||
@@ -70,3 +72,4 @@ } | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref3) { | ||
var value = _ref3.value; | ||
return '0x' + value.toString(16); | ||
@@ -73,0 +76,0 @@ } |
@@ -38,3 +38,3 @@ "use strict"; | ||
options: nullOptions, | ||
stringify: function stringify(value) { | ||
stringify: function stringify() { | ||
return nullOptions.nullStr; | ||
@@ -50,3 +50,4 @@ } | ||
options: boolOptions, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref) { | ||
var value = _ref.value; | ||
return value ? boolOptions.trueStr : boolOptions.falseStr; | ||
@@ -62,3 +63,4 @@ } | ||
options: boolOptions, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref2) { | ||
var value = _ref2.value; | ||
return value ? boolOptions.trueStr : boolOptions.falseStr; | ||
@@ -74,3 +76,4 @@ } | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref3) { | ||
var value = _ref3.value; | ||
return '0b' + value.toString(2); | ||
@@ -86,3 +89,4 @@ } | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref4) { | ||
var value = _ref4.value; | ||
return (value < 0 ? '-0' : '0') + value.toString(8); | ||
@@ -106,3 +110,4 @@ } | ||
}, | ||
stringify: function stringify(value) { | ||
stringify: function stringify(_ref5) { | ||
var value = _ref5.value; | ||
return (value < 0 ? '-0x' : '0x') + value.toString(16); | ||
@@ -109,0 +114,0 @@ } |
@@ -24,6 +24,6 @@ "use strict"; | ||
}, | ||
stringify: function stringify(value, _ref) { | ||
stringify: function stringify(value, _ref, onComment) { | ||
var indent = _ref.indent, | ||
inFlow = _ref.inFlow; | ||
return value.toString(indent, inFlow); | ||
return value.toString(indent, inFlow, onComment); | ||
} | ||
@@ -38,6 +38,6 @@ }; | ||
}, | ||
stringify: function stringify(value, _ref2) { | ||
stringify: function stringify(value, _ref2, onComment) { | ||
var indent = _ref2.indent, | ||
inFlow = _ref2.inFlow; | ||
return value.toString(indent, inFlow); | ||
return value.toString(indent, inFlow, onComment); | ||
} | ||
@@ -44,0 +44,0 @@ }; |
@@ -34,2 +34,4 @@ "use strict"; | ||
function _get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return _get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
@@ -58,8 +60,12 @@ | ||
_this.resolveComments(); | ||
for (var i = 0; i < _this.items.length; ++i) { | ||
var key = _this.items[i].key; | ||
var iKey = _this.items[i].key; | ||
for (var j = i + 1; j < _this.items.length; ++j) { | ||
if (_this.items[j].key === key) { | ||
doc.errors.push(new _errors.YAMLSyntaxError(node, "Map keys must be unique; \"".concat(key, "\" is repeated"))); | ||
var jKey = _this.items[j].key; | ||
if (iKey === jKey || iKey && jKey && iKey.hasOwnProperty('value') && iKey.value === jKey.value) { | ||
doc.errors.push(new _errors.YAMLSyntaxError(node, "Map keys must be unique; \"".concat(iKey, "\" is repeated"))); | ||
break; | ||
@@ -69,3 +75,3 @@ } | ||
if (doc.options.merge && key === '<<') { | ||
if (doc.options.merge && iKey.value === '<<') { | ||
var src = _this.items[i].value; | ||
@@ -79,6 +85,6 @@ var srcItems = src instanceof _Seq.default ? src.items.reduce(function (acc, _ref) { | ||
var key = _ref2.key; | ||
return key === pair.key; | ||
return key.value === pair.key.value; | ||
}) || toAdd.some(function (_ref3) { | ||
var key = _ref3.key; | ||
return key === pair.key; | ||
return key.value === pair.key.value; | ||
}); | ||
@@ -218,19 +224,16 @@ return exists ? toAdd : toAdd.concat(pair); | ||
key: "toString", | ||
value: function toString(indent, inFlow) { | ||
var tags = this.doc.tags; | ||
var options = { | ||
value: function toString(indent, inFlow, onComment) { | ||
return _get(YAMLMap.prototype.__proto__ || Object.getPrototypeOf(YAMLMap.prototype), "toString", this).call(this, { | ||
blockItem: function blockItem(_ref5) { | ||
var str = _ref5.str; | ||
return str; | ||
}, | ||
flowChars: { | ||
start: '{', | ||
end: '}' | ||
}, | ||
indent: indent, | ||
inFlow: inFlow, | ||
type: null | ||
}; | ||
var items = this.items.map(function (pair) { | ||
return pair.toString(tags, options); | ||
}); | ||
if (inFlow || items.length === 0) { | ||
// return `{\n ${indent}${items.join(`,\n ${indent}`)}\n${indent}}` | ||
return "{ ".concat(items.join(', '), " }"); | ||
} else { | ||
return items.join("\n".concat(indent)); | ||
} | ||
itemIndent: indent + (inFlow ? ' ' : '') | ||
}, onComment); | ||
} | ||
@@ -237,0 +240,0 @@ }]); |
@@ -10,2 +10,8 @@ "use strict"; | ||
var _Node = require("./Node"); | ||
var _Scalar = _interopRequireDefault(require("./Scalar")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
@@ -42,3 +48,3 @@ | ||
key: "toString", | ||
value: function toString(tags, options) { | ||
value: function toString(tags, options, onComment) { | ||
if (!tags) return JSON.stringify(this); | ||
@@ -48,11 +54,16 @@ var key = this.key, | ||
var indent = options.indent; | ||
var explicitKey = !key || key.comment || key instanceof _Collection.default; | ||
var opt = Object.assign({}, options, { | ||
implicitKey: true | ||
implicitKey: !explicitKey, | ||
indent: options.indent + ' ' | ||
}); | ||
opt.indent += ' '; | ||
var keyStr = tags.stringify(key, opt); | ||
var keyComment = key && key.comment; | ||
var keyStr = tags.stringify(key, opt, function () { | ||
keyComment = null; | ||
}); | ||
if (keyComment) keyStr = (0, _Node.addComment)(keyStr, opt.indent, keyComment); | ||
opt.implicitKey = false; | ||
var valueStr = tags.stringify(value, opt); | ||
var valueStr = tags.stringify(value, opt, onComment); | ||
if (key instanceof _Collection.default) { | ||
if (explicitKey) { | ||
return "? ".concat(keyStr, "\n").concat(indent, ": ").concat(valueStr); | ||
@@ -66,2 +77,11 @@ } else if (value instanceof _Collection.default) { | ||
}, { | ||
key: "comment", | ||
get: function get() { | ||
return this.value && this.value.comment; | ||
}, | ||
set: function set(comment) { | ||
if (this.value == null) this.value = new _Scalar.default(null); | ||
this.value.comment = comment; | ||
} | ||
}, { | ||
key: "stringKey", | ||
@@ -68,0 +88,0 @@ get: function get() { |
@@ -30,2 +30,4 @@ "use strict"; | ||
function _get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return _get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
@@ -54,2 +56,4 @@ | ||
_this.resolveComments(); | ||
return _this; | ||
@@ -148,21 +152,17 @@ } | ||
key: "toString", | ||
value: function toString(indent, inFlow) { | ||
var tags = this.doc.tags; | ||
var options = { | ||
implicitKey: false, | ||
indent: indent + ' ', | ||
value: function toString(indent, inFlow, onComment) { | ||
return _get(YAMLSeq.prototype.__proto__ || Object.getPrototypeOf(YAMLSeq.prototype), "toString", this).call(this, { | ||
blockItem: function blockItem(_ref) { | ||
var type = _ref.type, | ||
str = _ref.str; | ||
return type === 'comment' ? str : "- ".concat(str); | ||
}, | ||
flowChars: { | ||
start: '[', | ||
end: ']' | ||
}, | ||
indent: indent, | ||
inFlow: inFlow, | ||
type: null | ||
}; | ||
var items = this.items.map(function (node) { | ||
return tags.stringify(node, options); | ||
}); | ||
if (inFlow || items.length === 0) { | ||
return "[ ".concat(items.join(', '), " ]"); | ||
} else { | ||
return items.map(function (item) { | ||
return "- ".concat(item); | ||
}).join("\n".concat(indent)); | ||
} | ||
itemIndent: indent + (inFlow ? ' ' : ' ') | ||
}, onComment); | ||
} | ||
@@ -169,0 +169,0 @@ }]); |
@@ -14,4 +14,8 @@ "use strict"; | ||
var _Collection = _interopRequireDefault(require("./schema/Collection")); | ||
var _Pair = _interopRequireDefault(require("./schema/Pair")); | ||
var _Scalar = _interopRequireDefault(require("./schema/Scalar")); | ||
var _string = require("./schema/_string"); | ||
@@ -98,7 +102,8 @@ | ||
var match = str.match(test); | ||
if (match) return resolve.apply(null, match); | ||
if (match) return new _Scalar.default(resolve.apply(null, match)); | ||
} | ||
} | ||
return this.schema.scalarFallback ? this.schema.scalarFallback(str) : str; | ||
if (this.schema.scalarFallback) str = this.schema.scalarFallback(str); | ||
return new _Scalar.default(str); | ||
} // sets node.resolved on success | ||
@@ -120,7 +125,12 @@ | ||
try { | ||
if (generic) return node.resolved = generic.resolve(doc, node); | ||
var str = (0, _string.resolve)(doc, node); | ||
if (generic) { | ||
var res = generic.resolve(doc, node); | ||
if (!(res instanceof _Collection.default)) res = new _Scalar.default(res); | ||
node.resolved = res; | ||
} else { | ||
var str = (0, _string.resolve)(doc, node); | ||
if (typeof str === 'string' && tags.length > 0) { | ||
return node.resolved = this.resolveScalar(str, tags); | ||
if (typeof str === 'string' && tags.length > 0) { | ||
node.resolved = this.resolveScalar(str, tags); | ||
} | ||
} | ||
@@ -133,3 +143,6 @@ } catch (error) { | ||
return null; | ||
if (!node.resolved) return null; | ||
if (node.hasProps) node.resolved.anchor = node.anchor; | ||
if (tagName) node.resolved.tag = tagName; | ||
return node.resolved; | ||
} | ||
@@ -153,42 +166,42 @@ }, { | ||
}, { | ||
key: "getStringifier", | ||
value: function getStringifier(value, tag, format) { | ||
var _this = this; | ||
key: "stringify", | ||
value: function stringify(item, options, onComment) { | ||
if (item == null || _typeof(item) !== 'object') item = new _Scalar.default(item); | ||
options.tags = this; | ||
var match; | ||
if (tag) { | ||
match = this.schema.filter(function (t) { | ||
return t.tag === tag && (!format || t.format === format); | ||
if (item instanceof _Pair.default) { | ||
return item.toString(this, options, onComment); | ||
} else if (item.tag) { | ||
match = this.schema.filter(function (_ref6) { | ||
var format = _ref6.format, | ||
tag = _ref6.tag; | ||
return tag === item.tag && (!item.format || format === item.format); | ||
}); | ||
if (match.length === 0) throw new Error("Tag not available: ".concat(tag).concat(format ? ', format ' + format : '')); | ||
} else if (value == null) { | ||
if (match.length === 0) throw new Error("Tag not available: ".concat(item.tag).concat(item.format ? ', format ' + item.format : '')); | ||
} else if (item.value === null) { | ||
match = this.schema.filter(function (t) { | ||
return t.class === null && !t.format; | ||
}); | ||
if (match.length === 0) match = this.schema.filter(function (t) { | ||
return t.class === String && !t.format; | ||
}); | ||
} else if (value instanceof _Pair.default) { | ||
return function (value, options) { | ||
return value.toString(_this, options); | ||
}; | ||
if (match.length === 0) throw new Error('Schema is missing a null stringifier'); | ||
} else { | ||
var obj; | ||
var obj = item; | ||
switch (_typeof(value)) { | ||
case 'boolean': | ||
obj = new Boolean(); | ||
break; | ||
if (item.hasOwnProperty('value')) { | ||
switch (_typeof(item.value)) { | ||
case 'boolean': | ||
obj = new Boolean(); | ||
break; | ||
case 'number': | ||
obj = new Number(); | ||
break; | ||
case 'number': | ||
obj = new Number(); | ||
break; | ||
case 'string': | ||
obj = new String(); | ||
break; | ||
case 'string': | ||
obj = new String(); | ||
break; | ||
default: | ||
obj = value; | ||
default: | ||
obj = item.value; | ||
} | ||
} | ||
@@ -199,14 +212,8 @@ | ||
}); | ||
if (match.length === 0) throw new Error("Tag not resolved for ".concat(obj && obj.constructor ? obj.constructor.name : _typeof(obj))); // TODO: Handle bare arrays and objects | ||
if (match.length === 0) throw new Error("Tag not resolved for ".concat(obj && obj.constructor ? obj.constructor.name : _typeof(obj))); // TODO: Handle bare arrays and objects? | ||
} | ||
return match[0].stringify || Tags.defaultStringifier; | ||
var stringify = match[0].stringify || Tags.defaultStringifier; | ||
return stringify(item, options, onComment); | ||
} | ||
}, { | ||
key: "stringify", | ||
value: function stringify(value, options, tag, format) { | ||
var stringifier = this.getStringifier(value, tag, format); | ||
options.tags = this; | ||
return stringifier(value, options); | ||
} | ||
}]); | ||
@@ -213,0 +220,0 @@ |
{ | ||
"name": "yaml", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
178033
36
3955