-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathstring.js
More file actions
118 lines (92 loc) · 3.2 KB
/
Copy pathstring.js
File metadata and controls
118 lines (92 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import indentString from 'indent-string';
const DEFAULT_CONCATENATED_VALUES = {
SEPARATOR: ', ',
QUOTE_CHAR: '"',
};
function rtrim (str) {
return str.replace(/\s+$/, '');
}
export function removeTTYColors (str) {
return str.replace(/\033\[[0-9;]*m/g, '');
}
export function wordWrap (str, indent, width) {
let curStr = '';
let wrappedMsg = '';
if (removeTTYColors(str).length <= width - indent)
return indentString(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 indentString(wrappedMsg + curStr, ' ', indent);
}
export 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.includes(currentChar)) {
if (quoteChar === currentChar)
quoteChar = null;
else if (!quoteChar)
quoteChar = currentChar;
else
currentPart += currentChar;
}
else
currentPart += currentChar;
}
if (currentPart)
parts.push(currentPart);
return parts;
}
export function getPluralSuffix (array) {
return array.length > 1 ? 's' : '';
}
function getDisplayedItemText (item, quote) {
return `${quote}${item}${quote}`;
}
export function getConcatenatedValuesString (array, separator = DEFAULT_CONCATENATED_VALUES.SEPARATOR, quoteChar = DEFAULT_CONCATENATED_VALUES.QUOTE_CHAR) {
const clonedArray = [...array];
if (separator.includes('\n'))
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)}`;
}
export function getToBeInPastTense (array) {
return array.length > 1 ? 'were' : 'was';
}
export function createList (array, PREFIX = '- ', SEPARATOR = '\n') {
return array.map(option => PREFIX + option).join(SEPARATOR);
}