@@ -14,6 +14,48 @@ describe("ListIt", () => {
14
14
" 12 12.3 111\n" +
15
15
"123 1.23 0" ) ;
16
16
} ) ;
17
+ describe ( "opt.columnWidth" , ( ) => {
18
+ it ( "should truncate the texts with the width" , ( ) => {
19
+ const listit = new ListIt ( { columnWidth :[ 3 ] } ) ;
20
+ listit . d ( [
21
+ [ "ABCDEFG" , "OPQRSTU" ] ,
22
+ [ "HIJKLMN" , "VWXYZ" ] ,
23
+ ] ) ;
24
+ assert . equal ( listit . toString ( ) ,
25
+ "ABC OPQRSTU\n" +
26
+ "HIJ VWXYZ " ) ;
27
+ } ) ;
28
+ it ( "should truncate the texts even if it represents number" , ( ) => {
29
+ const listit = new ListIt ( { columnWidth :[ , 3 ] } ) ;
30
+ listit . d ( [
31
+ [ "123456" , "123456" ] ,
32
+ [ "123456" , "123456" ] ,
33
+ ] ) ;
34
+ assert . equal ( listit . toString ( ) ,
35
+ "123456 123\n" +
36
+ "123456 123" ) ;
37
+ } ) ;
38
+ it ( "should not affect when a text converted from number data which is longer than the specified length exists in column" , ( ) => {
39
+ const listit = new ListIt ( { columnWidth :3 } ) ;
40
+ listit . d ( [
41
+ [ "ABCDEFGOPQRSTU" , 1.2 ] ,
42
+ [ 123.456 , "VWXYZ" ] ,
43
+ ] ) ;
44
+ assert . equal ( listit . toString ( ) ,
45
+ "ABCDEFG 1.2\n" +
46
+ "123.456 VWX" ) ;
47
+ } ) ;
48
+ it ( "should not affect when all the text is shorter than the specified length" , ( ) => {
49
+ const listit = new ListIt ( { columnWidth :6 } ) ;
50
+ listit . d ( [
51
+ [ "ABCDEFGOPQRSTU" , 1.2 ] ,
52
+ [ 123.456 , "VWXYZ" ] ,
53
+ ] ) ;
54
+ assert . equal ( listit . toString ( ) ,
55
+ "ABCDEFG 1.2\n" +
56
+ "123.456 VWXYZ" ) ;
57
+ } ) ;
58
+ } ) ;
17
59
} ) ;
18
60
describe ( ".buffer" , ( ) => {
19
61
it ( "should not set the autoAlign option" , ( ) => {
@@ -39,6 +81,47 @@ describe("ListIt", () => {
39
81
} ) ;
40
82
} ) ;
41
83
describe ( ".setColumnWidth" , ( ) => {
84
+ it ( "should return the instance" , ( ) => {
85
+ const listit = new ListIt ( ) ;
86
+ assert . deepEqual ( listit . setColumnWidth ( 0 , 10 ) , listit ) ;
87
+ } ) ;
88
+ it ( "should throw the index is not a number" , ( ) => {
89
+ assert . throws ( ( ) => {
90
+ const listit = new ListIt ( ) ;
91
+ listit . d ( [
92
+ [ "ABCDEFG" , "OPQRSTU" ] ,
93
+ [ "HIJKLMN" , "VWXYZ" ] ,
94
+ ] ) . setColumnWidth ( "" , 10 ) ;
95
+ assert . equal ( listit . toString ( ) ,
96
+ "ABC OPQRSTU\n" +
97
+ "HIJ VWXYZ " ) ;
98
+
99
+ } ) ;
100
+ } ) ;
101
+ it ( "should throw the width is not a number" , ( ) => {
102
+ assert . throws ( ( ) => {
103
+ const listit = new ListIt ( ) ;
104
+ listit . d ( [
105
+ [ "ABCDEFG" , "OPQRSTU" ] ,
106
+ [ "HIJKLMN" , "VWXYZ" ] ,
107
+ ] ) . setColumnWidth ( 0 , "" ) ;
108
+ assert . equal ( listit . toString ( ) ,
109
+ "ABC OPQRSTU\n" +
110
+ "HIJ VWXYZ " ) ;
111
+
112
+ } ) ;
113
+ } ) ;
114
+ it ( "should accept null for width that remove the previous specification" , ( ) => {
115
+ const listit = new ListIt ( ) ;
116
+ listit . d ( [
117
+ [ "ABCDEFG" , "OPQRSTU" ] ,
118
+ [ "HIJKLMN" , "VWXYZ" ] ,
119
+ ] ) . setColumnWidth ( 0 , 3 ) ;
120
+ listit . setColumnWidth ( 0 , null ) ;
121
+ assert . equal ( listit . toString ( ) ,
122
+ "ABCDEFG OPQRSTU\n" +
123
+ "HIJKLMN VWXYZ " ) ;
124
+ } ) ;
42
125
it ( "should truncate the texts with the width" , ( ) => {
43
126
const listit = new ListIt ( ) ;
44
127
listit . d ( [
@@ -80,4 +163,74 @@ describe("ListIt", () => {
80
163
" 123.456 VWXYZ" ) ;
81
164
} ) ;
82
165
} ) ;
166
+ describe ( ".setColumnWidthAll" , ( ) => {
167
+ it ( "should return the instance" , ( ) => {
168
+ const listit = new ListIt ( ) ;
169
+ assert . deepEqual ( listit . setColumnWidthAll ( 0 , 10 ) , listit ) ;
170
+ } ) ;
171
+ it ( "should throw the width is not a number" , ( ) => {
172
+ assert . throws ( ( ) => {
173
+ const listit = new ListIt ( ) ;
174
+ listit . d ( [
175
+ [ "ABCDEFG" , "OPQRSTU" ] ,
176
+ [ "HIJKLMN" , "VWXYZ" ] ,
177
+ ] ) . setColumnWidthAll ( "" ) ;
178
+ assert . equal ( listit . toString ( ) ,
179
+ "ABC OPQRSTU\n" +
180
+ "HIJ VWXYZ " ) ;
181
+
182
+ } ) ;
183
+ } ) ;
184
+ it ( "should accept null for width that remove the previous specification" , ( ) => {
185
+ const listit = new ListIt ( ) ;
186
+ listit . d ( [
187
+ [ "ABCDEFG" , "OPQRSTU" ] ,
188
+ [ "HIJKLMN" , "VWXYZ" ] ,
189
+ ] ) . setColumnWidth ( 0 , 3 ) ;
190
+ listit . setColumnWidthAll ( null ) ;
191
+ assert . equal ( listit . toString ( ) ,
192
+ "ABCDEFG OPQRSTU\n" +
193
+ "HIJKLMN VWXYZ " ) ;
194
+ } ) ;
195
+ it ( "should truncate the texts with the width" , ( ) => {
196
+ const listit = new ListIt ( ) ;
197
+ listit . d ( [
198
+ [ "ABCDEFG" , "OPQRSTU" ] ,
199
+ [ "HIJKLMN" , "VWXYZ" ] ,
200
+ ] ) . setColumnWidthAll ( [ 3 ] ) ;
201
+ assert . equal ( listit . toString ( ) ,
202
+ "ABC OPQRSTU\n" +
203
+ "HIJ VWXYZ " ) ;
204
+ } ) ;
205
+ it ( "should truncate the texts even if it represents number" , ( ) => {
206
+ const listit = new ListIt ( ) ;
207
+ listit . d ( [
208
+ [ "123456" , "123456" ] ,
209
+ [ "123456" , "123456" ] ,
210
+ ] ) . setColumnWidthAll ( [ undefined , 3 ] ) ;
211
+ assert . equal ( listit . toString ( ) ,
212
+ "123456 123\n" +
213
+ "123456 123" ) ;
214
+ } ) ;
215
+ it ( "should not affect when a text converted from number data which is longer than the specified length exists in column" , ( ) => {
216
+ const listit = new ListIt ( ) ;
217
+ listit . d ( [
218
+ [ "ABCDEFGOPQRSTU" , 1.2 ] ,
219
+ [ 123.456 , "VWXYZ" ] ,
220
+ ] ) . setColumnWidthAll ( 3 ) ;
221
+ assert . equal ( listit . toString ( ) ,
222
+ "ABCDEFG 1.2\n" +
223
+ "123.456 VWX" ) ;
224
+ } ) ;
225
+ it ( "should not affect when all the text is shorter than the specified length" , ( ) => {
226
+ const listit = new ListIt ( ) ;
227
+ listit . d ( [
228
+ [ "ABCDEFGOPQRSTU" , 1.2 ] ,
229
+ [ 123.456 , "VWXYZ" ] ,
230
+ ] ) . setColumnWidthAll ( 6 ) ;
231
+ assert . equal ( listit . toString ( ) ,
232
+ "ABCDEFG 1.2\n" +
233
+ "123.456 VWXYZ" ) ;
234
+ } ) ;
235
+ } ) ;
83
236
} ) ;
0 commit comments