-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathalignString.ts
134 lines (124 loc) · 6.37 KB
/
alignString.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint-disable max-nested-callbacks */
import {
expect,
} from 'chai';
import chalk from 'chalk';
import {
alignString,
} from '../src/alignString';
import {
stringToRed,
} from './utils';
describe('alignString', () => {
context('subject parameter value width is greater than the container width', () => {
it('throws an error', () => {
expect(() => {
alignString('aa', 1, 'left');
}).to.throw(Error, 'Subject parameter value width cannot be greater than the container width.');
});
});
context('subject parameter value', () => {
context('0 width', () => {
it('produces a string consisting of container width number of whitespace characters', () => {
expect(alignString('', 5, 'left')).to.equal(' ', 'left');
expect(alignString('', 5, 'center')).to.equal(' ', 'center');
expect(alignString('', 5, 'justify')).to.equal(' ', 'justify');
expect(alignString('', 5, 'right')).to.equal(' ', 'right');
});
});
context('plain text', () => {
context('alignment', () => {
context('left', () => {
it('pads the string on the right side using a whitespace character', () => {
expect(alignString('aa', 6, 'left')).to.equal('aa ');
});
});
context('right', () => {
it('pads the string on the left side using a whitespace character', () => {
expect(alignString('aa', 6, 'right')).to.equal(' aa');
});
});
context('center', () => {
it('pads the string on both sides using a whitespace character', () => {
expect(alignString('aa', 8, 'center')).to.equal(' aa ');
});
context('uneven number of available with', () => {
it('floors the available width; adds extra space to the end of the string', () => {
expect(alignString('aa', 7, 'center')).to.equal(' aa ');
});
});
});
context('justify', () => {
it('align left if not contain spaces', () => {
expect(alignString('aa', 5, 'justify')).to.equal('aa ');
});
it('add missing spaces between two words', () => {
expect(alignString('a a', 5, 'justify')).to.equal('a a');
expect(alignString('a a', 5, 'justify')).to.equal('a a');
expect(alignString('a a', 5, 'justify')).to.equal('a a');
});
it('multiple words, distribute spaces from left to right when maximum adding spaces in one place are not greater than 3', () => {
expect(alignString('a b c', 5, 'justify')).to.equal('a b c');
expect(alignString('a b c', 6, 'justify')).to.equal('a b c');
expect(alignString('a b c', 7, 'justify')).to.equal('a b c');
expect(alignString('a b c', 8, 'justify')).to.equal('a b c');
expect(alignString('a b c', 9, 'justify')).to.equal('a b c');
expect(alignString('a b c', 10, 'justify')).to.equal('a b c');
expect(alignString('a b c', 11, 'justify')).to.equal('a b c');
expect(alignString('a b c', 12, 'justify')).to.equal('a b c ');
expect(alignString('a bbb cc d', 11, 'justify')).to.equal('a bbb cc d');
expect(alignString('a bbb cc d', 12, 'justify')).to.equal('a bbb cc d');
expect(alignString('a bbb cc d', 13, 'justify')).to.equal('a bbb cc d');
expect(alignString('a bbb cc d', 14, 'justify')).to.equal('a bbb cc d');
});
});
});
});
context('text containing ANSI escape codes', () => {
context('alignment', () => {
context('left', () => {
it('pads the string on the right side using a whitespace character', () => {
expect(alignString(chalk.red('aa'), 6, 'left')).to.equal(chalk.red('aa') + ' ');
});
});
context('right', () => {
it('pads the string on the left side using a whitespace character', () => {
expect(alignString(chalk.red('aa'), 6, 'right')).to.equal(' ' + chalk.red('aa'));
});
});
context('center', () => {
it('pads the string on both sides using a whitespace character', () => {
expect(alignString(chalk.red('aa'), 6, 'center')).to.equal(' ' + chalk.red('aa') + ' ');
});
context('uneven number of available with', () => {
it('floors the available width; adds extra space to the end of the string', () => {
expect(alignString(chalk.red('aa'), 7, 'center')).to.equal(' ' + chalk.red('aa') + ' ');
});
});
});
context('justify', () => {
it('align left if not contain spaces', () => {
expect(alignString(chalk.red('aa'), 5, 'justify')).to.equal(chalk.red('aa') + ' ');
});
it('add missing spaces between two words', () => {
expect(alignString(stringToRed('a a'), 5, 'justify')).to.equal(stringToRed('a a'));
expect(alignString(stringToRed('a a'), 5, 'justify')).to.equal(stringToRed('a a'));
expect(alignString(stringToRed('a a'), 5, 'justify')).to.equal(stringToRed('a a'));
});
it('multiple words, uneven spaces add from left to right', () => {
expect(alignString(stringToRed('a b c'), 5, 'justify')).to.equal(stringToRed('a b c'));
expect(alignString(stringToRed('a b c'), 6, 'justify')).to.equal(stringToRed('a b c'));
expect(alignString(stringToRed('a b c'), 7, 'justify')).to.equal(stringToRed('a b c'));
expect(alignString(stringToRed('a b c'), 8, 'justify')).to.equal(stringToRed('a b c'));
expect(alignString(stringToRed('a b c'), 9, 'justify')).to.equal(stringToRed('a b c'));
expect(alignString(stringToRed('a b c'), 10, 'justify')).to.equal(stringToRed('a b c'));
expect(alignString(stringToRed('a bbb cc d'), 11, 'justify')).to.equal(stringToRed('a bbb cc d'));
expect(alignString(stringToRed('a bbb cc d'), 12, 'justify')).to.equal(stringToRed('a bbb cc d'));
expect(alignString(stringToRed('a bbb cc d'), 13, 'justify')).to.equal(stringToRed('a bbb cc d'));
expect(alignString(stringToRed('a bbb cc d'), 14, 'justify')).to.equal(stringToRed('a bbb cc d'));
});
});
});
});
});
});