@ply-ct/ply
Version:
REST API Automated Testing
44 lines • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Code = void 0;
const util_1 = require("./util");
class Code {
constructor(code, commentDelim) {
if (typeof code === 'string') {
this.lines = this.trimComments((0, util_1.lines)(code.trimRight() + '\n'), commentDelim);
}
else {
this.lines = this.trimComments(code, commentDelim);
}
}
/**
* Removes trailing comments, along with any preceding whitespace.
* Returns an array of objects (one for each line) to be passed to extractCode.
*/
trimComments(inLines, delim) {
const lines = [];
const regex = new RegExp('(\\s*' + delim + ')', 'g');
inLines.forEach((line) => {
const segs = line.split(regex);
const ln = { code: segs[0] };
if (segs.length > 1) {
ln.comment = segs.reduce((c, seg, i) => (i > 1 ? c + seg : seg), '');
}
lines.push(ln);
});
return lines;
}
/**
* All newlines are \n
*/
extractCode(withComments = false) {
return this.lines.reduce((acc, line, i, lines) => {
return (acc +
line.code +
(line.comment && withComments ? line.comment : '') +
(i < lines.length - 1 ? '\n' : ''));
}, '');
}
}
exports.Code = Code;
//# sourceMappingURL=code.js.map