@opra/cli
Version:
Opra CLI tools
69 lines (68 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapJSDocString = wrapJSDocString;
exports.wrapQuotedString = wrapQuotedString;
exports.wrapStringArray = wrapStringArray;
exports.wrapTypeArray = wrapTypeArray;
const tslib_1 = require("tslib");
const js_string_escape_1 = tslib_1.__importDefault(require("js-string-escape"));
function wrapJSDocString(s, indent, currentColumn) {
const arr = (s || '')
.split(/[ \n\r]/)
.map((x, i, a) => i < a.length - 1 ? x + ' ' : x);
return _printLines(arr, {
indent,
currentColumn,
lineStart: '* ',
lineEnd: '',
});
}
function wrapQuotedString(s, indent, currentColumn) {
const arr = (0, js_string_escape_1.default)(s || '')
.split(' ')
.map((x, i, a) => i < a.length - 1 ? x + ' ' : x);
return ("'" +
_printLines(arr, {
indent,
currentColumn,
lineStart: "'",
lineEnd: "' +",
}) +
"'");
}
function wrapStringArray(arr, indent, currentColumn) {
const ar1 = arr.map((x, i, a) => "'" + x + "'" + (i < a.length - 1 ? ', ' : ''));
return '[' + _printLines(ar1, { indent, currentColumn }) + ']';
}
function wrapTypeArray(arr, indent, currentColumn) {
const ar1 = arr.map((x, i, a) => x + (i < a.length - 1 ? ' | ' : ''));
return _printLines(ar1, { indent, currentColumn });
}
function _printLines(arr, opts = {}) {
let s = '';
let line = '';
const indent = opts.indent || 0;
let lineWidth = (opts.lineWidth || 90) - (opts.currentColumn || 0);
const l = arr.length;
const printLine = (eof) => {
s +=
(s
? '\n' +
' '.repeat(indent || 0) +
(opts.lineStart ? opts.lineStart : '')
: '') +
line +
(!eof ? (opts.lineEnd ? opts.lineEnd : '') : '');
line = '';
};
for (let i = 0; i < l; i++) {
const x = arr[i];
if (line && line.length + x.length > lineWidth) {
lineWidth = (opts.lineWidth || 90) - indent;
printLine(false);
}
line += x;
}
printLine(true);
return s;
}