106 lines
13 KiB
JavaScript
106 lines
13 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.createList = exports.getToBeInPastTense = exports.getConcatenatedValuesString = exports.getPluralSuffix = exports.splitQuotedText = exports.wordWrap = exports.removeTTYColors = void 0;
|
||
|
const indent_string_1 = __importDefault(require("indent-string"));
|
||
|
const DEFAULT_CONCATENATED_VALUES = {
|
||
|
SEPARATOR: ', ',
|
||
|
QUOTE_CHAR: '"',
|
||
|
};
|
||
|
function rtrim(str) {
|
||
|
return str.replace(/\s+$/, '');
|
||
|
}
|
||
|
function removeTTYColors(str) {
|
||
|
return str.replace(/\033\[[0-9;]*m/g, '');
|
||
|
}
|
||
|
exports.removeTTYColors = removeTTYColors;
|
||
|
function wordWrap(str, indent, width) {
|
||
|
let curStr = '';
|
||
|
let wrappedMsg = '';
|
||
|
if (removeTTYColors(str).length <= width - indent)
|
||
|
return (0, indent_string_1.default)(str, ' ', indent);
|
||
|
str = str.replace(/(\r\n)/gm, '\n')
|
||
|
.split(/(\S+[ \t]+)|(\S+(?:\n))|(\n)/m)
|
||
|
//NOTE: cut empty elements
|
||
|
.filter(elm => !!elm);
|
||
|
str.forEach(word => {
|
||
|
const newStr = curStr + word;
|
||
|
if (removeTTYColors(newStr).length > width - indent) {
|
||
|
wrappedMsg += `${rtrim(curStr)}\n`;
|
||
|
curStr = word;
|
||
|
}
|
||
|
else {
|
||
|
if (curStr[curStr.length - 1] === '\n') {
|
||
|
wrappedMsg += `${rtrim(curStr)}\n`;
|
||
|
curStr = '';
|
||
|
}
|
||
|
curStr += word;
|
||
|
}
|
||
|
});
|
||
|
return (0, indent_string_1.default)(wrappedMsg + curStr, ' ', indent);
|
||
|
}
|
||
|
exports.wordWrap = wordWrap;
|
||
|
function splitQuotedText(str, splitChar, quotes = '"\'') {
|
||
|
let currentPart = '';
|
||
|
const parts = [];
|
||
|
let quoteChar = null;
|
||
|
for (let i = 0; i < str.length; i++) {
|
||
|
const currentChar = str[i];
|
||
|
if (currentChar === splitChar) {
|
||
|
if (quoteChar)
|
||
|
currentPart += currentChar;
|
||
|
else {
|
||
|
parts.push(currentPart);
|
||
|
currentPart = '';
|
||
|
}
|
||
|
}
|
||
|
else if (quotes.indexOf(currentChar) > -1) {
|
||
|
if (quoteChar === currentChar)
|
||
|
quoteChar = null;
|
||
|
else if (!quoteChar)
|
||
|
quoteChar = currentChar;
|
||
|
else
|
||
|
currentPart += currentChar;
|
||
|
}
|
||
|
else
|
||
|
currentPart += currentChar;
|
||
|
}
|
||
|
if (currentPart)
|
||
|
parts.push(currentPart);
|
||
|
return parts;
|
||
|
}
|
||
|
exports.splitQuotedText = splitQuotedText;
|
||
|
function getPluralSuffix(array) {
|
||
|
return array.length > 1 ? 's' : '';
|
||
|
}
|
||
|
exports.getPluralSuffix = getPluralSuffix;
|
||
|
function getDisplayedItemText(item, quote) {
|
||
|
return `${quote}${item}${quote}`;
|
||
|
}
|
||
|
function getConcatenatedValuesString(array, separator = DEFAULT_CONCATENATED_VALUES.SEPARATOR, quoteChar = DEFAULT_CONCATENATED_VALUES.QUOTE_CHAR) {
|
||
|
const clonedArray = [...array];
|
||
|
if (separator.indexOf('\n') > -1)
|
||
|
return clonedArray.map(item => getDisplayedItemText(item, quoteChar)).join(separator);
|
||
|
else if (clonedArray.length === 1)
|
||
|
return getDisplayedItemText(clonedArray[0], quoteChar);
|
||
|
else if (clonedArray.length === 2) {
|
||
|
const item1 = array[0];
|
||
|
const item2 = array[1];
|
||
|
return `${getDisplayedItemText(item1, quoteChar)} and ${getDisplayedItemText(item2, quoteChar)}`;
|
||
|
}
|
||
|
const lastItem = clonedArray.pop();
|
||
|
const otherItemString = clonedArray.map(item => getDisplayedItemText(item, quoteChar)).join(separator);
|
||
|
return `${otherItemString}, and ${getDisplayedItemText(lastItem, quoteChar)}`;
|
||
|
}
|
||
|
exports.getConcatenatedValuesString = getConcatenatedValuesString;
|
||
|
function getToBeInPastTense(array) {
|
||
|
return array.length > 1 ? 'were' : 'was';
|
||
|
}
|
||
|
exports.getToBeInPastTense = getToBeInPastTense;
|
||
|
function createList(array, PREFIX = '- ', SEPARATOR = '\n') {
|
||
|
return array.map(option => PREFIX + option).join(SEPARATOR);
|
||
|
}
|
||
|
exports.createList = createList;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RyaW5nLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3V0aWxzL3N0cmluZy5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQSxrRUFBeUM7QUFFekMsTUFBTSwyQkFBMkIsR0FBRztJQUNoQyxTQUFTLEVBQUcsSUFBSTtJQUNoQixVQUFVLEVBQUUsR0FBRztDQUNsQixDQUFDO0FBRUYsU0FBUyxLQUFLLENBQUUsR0FBRztJQUNmLE9BQU8sR0FBRyxDQUFDLE9BQU8sQ0FBQyxNQUFNLEVBQUUsRUFBRSxDQUFDLENBQUM7QUFDbkMsQ0FBQztBQUVELFNBQWdCLGVBQWUsQ0FBRSxHQUFHO0lBQ2hDLE9BQU8sR0FBRyxDQUFDLE9BQU8sQ0FBQyxpQkFBaUIsRUFBRSxFQUFFLENBQUMsQ0FBQztBQUM5QyxDQUFDO0FBRkQsMENBRUM7QUFFRCxTQUFnQixRQUFRLENBQUUsR0FBRyxFQUFFLE1BQU0sRUFBRSxLQUFLO0lBQ3hDLElBQUksTUFBTSxHQUFPLEVBQUUsQ0FBQztJQUNwQixJQUFJLFVBQVUsR0FBRyxFQUFFLENBQUM7SUFFcEIsSUFBSSxlQUFlLENBQUMsR0FBRyxDQUFDLENBQUMsTUFBTSxJQUFJLEtBQUssR0FBRyxNQUFNO1FBQzdDLE9BQU8sSUFBQSx1QkFBWSxFQUFD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