-
Notifications
You must be signed in to change notification settings - Fork 22
/
string-utils.ts
56 lines (50 loc) · 1.71 KB
/
string-utils.ts
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
import { ALIGNMENT } from '../models/common';
import findWidthInConsole from './console-utils';
import { cellText } from './table-helpers';
// ("How are you?",center, 20) => " How are you? "
// ("How are you?",right, 20) => " How are you?"
export const textWithPadding = (
text: string,
alignment: ALIGNMENT,
mxColumnLen: number
): string => {
const curTextSize = findWidthInConsole(text);
// alignments for center padding case
const leftPadding = Math.floor((mxColumnLen - curTextSize) / 2);
const rightPadding = mxColumnLen - leftPadding - curTextSize;
switch (alignment) {
case 'left':
return text.concat(' '.repeat(mxColumnLen - curTextSize));
case 'center':
return ' '
.repeat(leftPadding)
.concat(text)
.concat(' '.repeat(rightPadding));
case 'right':
default:
return ' '.repeat(mxColumnLen - curTextSize).concat(text);
}
};
// ("How are you?",10) => ["How are ", "you?"]
export const limitWidth = (inpStr: string, width: number): string[] => {
const ret: string[] = [];
const spaceSeparatedStrings = inpStr.split(' ');
let now: string[] = [];
let cnt = 0;
spaceSeparatedStrings.forEach((strWithoutSpace) => {
const consoleWidth = findWidthInConsole(strWithoutSpace);
if (cnt + consoleWidth <= width) {
cnt += consoleWidth + 1; // 1 for the space
now.push(strWithoutSpace);
} else {
ret.push(now.join(' '));
now = [strWithoutSpace];
cnt = consoleWidth + 1;
}
});
ret.push(now.join(' '));
return ret;
};
// ("How are you?",10) => ["How are ", "you?"]
export const biggestWordInSentence = (inpStr: string): number =>
inpStr.split(' ').reduce((a, b) => Math.max(a, findWidthInConsole(b)), 0);