@atomist/rug
Version:
TypeScript model for Atomist Rugs, see http://docs.atomist.com/
298 lines (297 loc) • 11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* String without quotes or newlines. Leave it alone.
*/
var YamlRawValue = (function () {
function YamlRawValue(node) {
this.node = node;
}
YamlRawValue.prototype.text = function () {
return this.node.value();
};
YamlRawValue.prototype.updateText = function (to) {
this.node.update(to);
};
return YamlRawValue;
}());
exports.YamlRawValue = YamlRawValue;
/**
* String enclosed in "". Simply strips them.
*/
var YamlQuotedValue = (function () {
function YamlQuotedValue(node) {
this.node = node;
}
YamlQuotedValue.prototype.text = function () {
return this.node.value().slice(1, this.node.value().length - 1);
};
YamlQuotedValue.prototype.updateText = function (to) {
var newValue = "\"" + to + "\"";
this.node.update(newValue);
};
return YamlQuotedValue;
}());
exports.YamlQuotedValue = YamlQuotedValue;
/**
* String using >. Strip newlines.
*/
var YamlFoldedBlockScalar = (function () {
function YamlFoldedBlockScalar(node) {
this.node = node;
var raw = node.value();
if (raw.charAt(0) !== ">") {
throw new Error("Illegal argument: Must begin with >");
}
if (raw.charAt(1) !== "\n") {
throw new Error("Illegal argument: Must begin with >\n");
}
var index = 2;
var ch = raw.charAt(index);
while (ch === " " || ch === "\t" || ch === "\n") {
ch = raw.charAt(++index);
}
this.leading = raw.substr(0, index);
this.indent = this.leading.substring(2);
}
YamlFoldedBlockScalar.prototype.text = function () {
return this.node.value().substr(this.leading.length)
.replace(new RegExp("^" + this.indent, "mg"), "")
.replace(new RegExp("\n+$"), "")
.replace(new RegExp("([^\n])\n([^\n])", "g"), "$1 $2")
.replace(new RegExp("\n\n", "g"), "\n") + "\n";
};
YamlFoldedBlockScalar.prototype.updateText = function (to) {
var newValue = "" + this.leading + to;
// console.log(`Update from [${this.node.value()}] to [${newValue}]`)
this.node.update(newValue);
};
return YamlFoldedBlockScalar;
}());
exports.YamlFoldedBlockScalar = YamlFoldedBlockScalar;
/**
* String using >-.
*/
var YamlFoldedBlockWithStripChomping = (function () {
function YamlFoldedBlockWithStripChomping(node) {
this.node = node;
var raw = node.value();
if (raw.charAt(0) !== ">" && raw.charAt(1) !== "-") {
throw new Error("Illegal argument: Must begin with >-");
}
if (raw.charAt(2) !== "\n") {
throw new Error("Illegal argument: Must begin with >-\n");
}
var index = 3;
var ch = raw.charAt(index);
while (ch === " " || ch === "\t" || ch === "\n") {
ch = raw.charAt(++index);
}
this.leading = raw.substr(0, index);
this.indent = this.leading.substring(3);
}
YamlFoldedBlockWithStripChomping.prototype.text = function () {
return this.node.value().substr(this.leading.length)
.replace(new RegExp("^" + this.indent, "mg"), "")
.replace(new RegExp("\n+$"), "")
.replace(new RegExp("([^\n])\n([^\n])", "g"), "$1 $2")
.replace(new RegExp("\n\n", "g"), "\n");
};
YamlFoldedBlockWithStripChomping.prototype.updateText = function (to) {
var newValue = "" + this.leading + to;
// console.log(`Update from [${this.node.value()}] to [${newValue}]`)
this.node.update(newValue);
};
return YamlFoldedBlockWithStripChomping;
}());
exports.YamlFoldedBlockWithStripChomping = YamlFoldedBlockWithStripChomping;
/**
* String using >+.
*/
var YamlFoldedBlockWithKeepChomping = (function () {
function YamlFoldedBlockWithKeepChomping(node) {
this.node = node;
var raw = node.value();
if (raw.charAt(0) !== ">" && raw.charAt(1) !== "+") {
throw new Error("Illegal argument: Must begin with >+");
}
if (raw.charAt(2) !== "\n") {
throw new Error("Illegal argument: Must begin with >+\n");
}
var index = 3;
var ch = raw.charAt(index);
while (ch === " " || ch === "\t" || ch === "\n") {
ch = raw.charAt(++index);
}
this.leading = raw.substr(0, index);
this.indent = this.leading.substring(3);
}
YamlFoldedBlockWithKeepChomping.prototype.text = function () {
var raw = this.node.value();
var newlines = raw.match(new RegExp("\n+$"));
return raw.substr(this.leading.length)
.replace(new RegExp("^" + this.indent, "mg"), "")
.replace(new RegExp("\n+$"), "")
.replace(new RegExp("([^\n])\n([^\n])", "g"), "$1 $2")
.replace(new RegExp("\n\n", "g"), "\n")
+ newlines[0].toString();
};
YamlFoldedBlockWithKeepChomping.prototype.updateText = function (to) {
var newValue = "" + this.leading + to;
// console.log(`Update from [${this.node.value()}] to [${newValue}]`)
this.node.update(newValue);
};
return YamlFoldedBlockWithKeepChomping;
}());
exports.YamlFoldedBlockWithKeepChomping = YamlFoldedBlockWithKeepChomping;
/**
* String using |. Maintain newlines.
*/
var YamlLiteralBlockScalar = (function () {
function YamlLiteralBlockScalar(node) {
this.node = node;
var raw = node.value();
if (raw.charAt(0) !== "|") {
throw new Error("Illegal argument: Must begin with |");
}
if (raw.charAt(1) !== "\n") {
throw new Error("Illegal argument: Must begin with |\n");
}
var index = 2;
var ch = raw.charAt(index);
while (ch === " " || ch === "\t" || ch === "\n") {
ch = raw.charAt(++index);
}
this.leading = raw.substr(0, index);
this.indent = this.leading.substring(2);
}
YamlLiteralBlockScalar.prototype.text = function () {
return this.node.value().substr(this.leading.length)
.replace(new RegExp("^" + this.indent, "mg"), "")
.replace(new RegExp("\n+$"), "\n");
};
YamlLiteralBlockScalar.prototype.updateText = function (to) {
var newValue = "" + this.leading + to;
// console.log(`Update from [${this.node.value()}] to [${newValue}]`)
this.node.update(newValue);
};
return YamlLiteralBlockScalar;
}());
exports.YamlLiteralBlockScalar = YamlLiteralBlockScalar;
/**
* String using |-.
*/
var YamlLiteralBlockWithStripChomping = (function () {
function YamlLiteralBlockWithStripChomping(node) {
this.node = node;
var raw = node.value();
if (raw.charAt(0) !== "|" && raw.charAt(1) !== "-") {
throw new Error("Illegal argument: Must begin with |-");
}
if (raw.charAt(2) !== "\n") {
throw new Error("Illegal argument: Must begin with |-\n");
}
var index = 3;
var ch = raw.charAt(index);
while (ch === " " || ch === "\t" || ch === "\n") {
ch = raw.charAt(++index);
}
this.leading = raw.substr(0, index);
this.indent = this.leading.substring(3);
}
YamlLiteralBlockWithStripChomping.prototype.text = function () {
return this.node.value().substr(this.leading.length)
.replace(new RegExp("^" + this.indent, "mg"), "")
.replace(new RegExp("\n+$"), "");
};
YamlLiteralBlockWithStripChomping.prototype.updateText = function (to) {
var newValue = "" + this.leading + to;
// console.log(`Update from [${this.node.value()}] to [${newValue}]`)
this.node.update(newValue);
};
return YamlLiteralBlockWithStripChomping;
}());
exports.YamlLiteralBlockWithStripChomping = YamlLiteralBlockWithStripChomping;
/**
* String using |+.
*/
var YamlLiteralBlockWithKeepChomping = (function () {
function YamlLiteralBlockWithKeepChomping(node) {
this.node = node;
var raw = node.value();
if (raw.charAt(0) !== "|" && raw.charAt(1) !== "+") {
throw new Error("Illegal argument: Must begin with |+");
}
if (raw.charAt(2) !== "\n") {
throw new Error("Illegal argument: Must begin with |+\n");
}
var index = 3;
var ch = raw.charAt(index);
while (ch === " " || ch === "\t" || ch === "\n") {
ch = raw.charAt(++index);
}
this.leading = raw.substr(0, index);
this.indent = this.leading.substring(3);
}
YamlLiteralBlockWithKeepChomping.prototype.text = function () {
var raw = this.node.value();
var newlines = raw.match(new RegExp("\n+$"));
return raw.substr(this.leading.length)
.replace(new RegExp("^" + this.indent, "mg"), "")
.replace(new RegExp("\n+$"), "")
+ newlines[0].toString();
};
YamlLiteralBlockWithKeepChomping.prototype.updateText = function (to) {
var newValue = "" + this.leading + to;
// console.log(`Update from [${this.node.value()}] to [${newValue}]`)
this.node.update(newValue);
};
return YamlLiteralBlockWithKeepChomping;
}());
exports.YamlLiteralBlockWithKeepChomping = YamlLiteralBlockWithKeepChomping;
/**
* Works with a raw node value to add and remove sequence elements, observing YAML formatting.
*/
var YamlSequenceOps = (function () {
function YamlSequenceOps(node) {
this.node = node;
this.items = node.value().trim().split("\n");
}
/**
* Adds an element to a sequence.
*
* @param elem the new element
*/
YamlSequenceOps.prototype.addElement = function (elem) {
var lastItem = this.items[this.items.length - 1];
this.items.push(lastItem.substring(0, lastItem.indexOf("-")) + "- " + elem);
var newValue = this.items.join("\n") + "\n";
this.node.update(newValue);
// console.log(this.node.value())
};
/**
* Removes an element, if it exists, from a sequence.
*
* @param elem the element to remove
*/
YamlSequenceOps.prototype.removeElement = function (elem) {
var lastItem = this.items[this.items.length - 1];
var index = this.items.indexOf(lastItem.substring(0, lastItem.indexOf("-")) + "- " + elem);
if (index > -1) {
this.items.splice(index, 1);
var newValue = this.items.join("\n") + "\n";
this.node.update(newValue);
// console.log(this.node.value())
}
};
/**
* Update a key.
*
* @param the new key name
*/
YamlSequenceOps.prototype.updateKey = function (key) {
this.node.update(key); // this currently only updates the value
};
return YamlSequenceOps;
}());
exports.YamlSequenceOps = YamlSequenceOps;