1
- import { s_imploder } from ' ./imploder' ;
1
+ import { ImploderBuilder , s_imploder } from " ./imploder" ;
2
2
3
3
describe ( 'imploder' , ( ) => {
4
4
it ( 'imploder' , ( ) => {
@@ -12,3 +12,197 @@ describe('imploder', () => {
12
12
expect ( s_imploder ( test ) . separator ( '; ' ) . build ( ) ) . toBe ( 'Apple; Bannana; Rasberry; Pie; Ananas; Lannanas' ) ;
13
13
} ) ;
14
14
} ) ;
15
+
16
+ describe ( 'ImploderBuilder' , ( ) => {
17
+ it ( 'should create an instance of ImploderBuilder' , ( ) => {
18
+ const builder = new ImploderBuilder ( ) ;
19
+ expect ( builder ) . toBeInstanceOf ( ImploderBuilder ) ;
20
+ } ) ;
21
+
22
+ it ( 'should create an instance of ImploderBuilder using static get method' , ( ) => {
23
+ const builder = ImploderBuilder . get ( ) ;
24
+ expect ( builder ) . toBeInstanceOf ( ImploderBuilder ) ;
25
+ } ) ;
26
+
27
+ it ( 'should set and retrieve the source correctly' , ( ) => {
28
+ const source = [ 'one' , 'two' , 'three' ] ;
29
+ const builder = new ImploderBuilder ( ) . source ( source ) ;
30
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
31
+ } ) ;
32
+
33
+ it ( 'should map source correctly' , ( ) => {
34
+ const source = [ 1 , 2 , 3 ] ;
35
+ const builder = new ImploderBuilder ( ) . mappedSource ( source , ( it ) => it . toString ( ) ) ;
36
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
37
+ } ) ;
38
+
39
+ it ( 'should set and retrieve the maxLength correctly' , ( ) => {
40
+ const maxLength = 5 ;
41
+ const builder = new ImploderBuilder ( ) . maxLength ( maxLength ) ;
42
+ expect ( builder . build ( ) . length ) . toBeLessThanOrEqual ( maxLength ) ;
43
+ } ) ;
44
+
45
+ it ( 'should set and retrieve the offset correctly' , ( ) => {
46
+ const offset = 2 ;
47
+ const source = [ 'one' , 'two' , 'three' ] ;
48
+ const expected = source . slice ( offset ) . join ( '' ) ;
49
+ const builder = new ImploderBuilder ( ) . source ( source ) . offset ( offset ) ;
50
+ expect ( builder . build ( ) ) . toEqual ( expected ) ;
51
+ } ) ;
52
+
53
+ it ( 'should set and retrieve the separator correctly' , ( ) => {
54
+ const separator = ', ' ;
55
+ const source = [ 'one' , 'two' , 'three' ] ;
56
+ const expected = source . join ( separator ) ;
57
+ const builder = new ImploderBuilder ( ) . source ( source ) . separator ( separator ) ;
58
+ expect ( builder . build ( ) ) . toEqual ( expected ) ;
59
+ } ) ;
60
+
61
+ it ( 'should set and retrieve the suffix correctly' , ( ) => {
62
+ const suffix = '...' ;
63
+ const source = [ 'one' , 'two' , 'three' ] ;
64
+ const builder = new ImploderBuilder ( ) . source ( source ) . suffix ( suffix ) ;
65
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
66
+ } ) ;
67
+
68
+ it ( 'should set and retrieve the suffix and max length correctly' , ( ) => {
69
+ const suffix = '...' ;
70
+ const source = [ 'one' , 'two' , 'three' ] ;
71
+ const builder = new ImploderBuilder ( ) . source ( source ) . suffix ( suffix ) . maxLength ( 10 ) ;
72
+ expect ( builder . build ( ) ) . toEqual ( 'onetwothre...' ) ;
73
+ } ) ;
74
+
75
+ it ( 'should handle a null source gracefully' , ( ) => {
76
+ const builder = new ImploderBuilder ( ) . source ( null ) ;
77
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
78
+ } ) ;
79
+
80
+ it ( 'should handle undefined values gracefully' , ( ) => {
81
+ const builder = new ImploderBuilder ( ) ;
82
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
83
+ } ) ;
84
+ } ) ;
85
+
86
+ describe ( 's_imploder' , ( ) => {
87
+ it ( 'should create an instance of ImploderBuilder using s_imploder' , ( ) => {
88
+ const builder = s_imploder ( ) ;
89
+ expect ( builder ) . toBeInstanceOf ( ImploderBuilder ) ;
90
+ } ) ;
91
+
92
+ it ( 'should create an instance of ImploderBuilder with the provided source using s_imploder' , ( ) => {
93
+ const source = [ 'one' , 'two' , 'three' ] ;
94
+ const builder = s_imploder ( source ) ;
95
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
96
+ } ) ;
97
+ } ) ;
98
+
99
+ describe ( 'Edge Cases' , ( ) => {
100
+ it ( 'should handle a null source gracefully when using s_imploder' , ( ) => {
101
+ const builder = s_imploder ( null ) ;
102
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
103
+ } ) ;
104
+
105
+ it ( 'should handle undefined values gracefully when using s_imploder' , ( ) => {
106
+ const builder = s_imploder ( undefined ) ;
107
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
108
+ } ) ;
109
+
110
+ it ( 'should handle mapping with a null source when using mappedSource' , ( ) => {
111
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
112
+ // @ts -ignore
113
+ const builder = new ImploderBuilder ( ) . mappedSource ( null , ( it ) => it . toString ( ) ) ;
114
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
115
+ } ) ;
116
+
117
+ it ( 'should handle mapping without a map function when using mappedSource and numbers' , ( ) => {
118
+ const source = [ 1 , 2 , 3 ] ;
119
+ const builder = new ImploderBuilder ( ) . mappedSource ( source ) ;
120
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
121
+ } ) ;
122
+
123
+ it ( 'should handle mapping without a map function when using mappedSource and strings' , ( ) => {
124
+ const source = [ '1' , '2' , '3' ] ;
125
+ const builder = new ImploderBuilder ( ) . mappedSource ( source ) ;
126
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
127
+ } ) ;
128
+
129
+ it ( 'should handle maxLength exceeding the string length when using maxLength' , ( ) => {
130
+ const maxLength = 100 ;
131
+ const source = [ 'one' , 'two' , 'three' ] ;
132
+ const builder = new ImploderBuilder ( ) . source ( source ) . maxLength ( maxLength ) ;
133
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
134
+ } ) ;
135
+
136
+ it ( 'should handle a negative offset when using offset' , ( ) => {
137
+ const offset = - 2 ;
138
+ const source = [ 'one' , 'two' , 'three' ] ;
139
+ const builder = new ImploderBuilder ( ) . source ( source ) . offset ( offset ) ;
140
+ expect ( builder . build ( ) ) . toEqual ( 'twothree' ) ;
141
+ } ) ;
142
+
143
+ it ( 'should handle a negative offset when using offset and max length' , ( ) => {
144
+ const offset = - 2 ;
145
+ const source = [ 'one' , 'two' , 'three' ] ;
146
+ const builder = new ImploderBuilder ( ) . source ( source ) . offset ( offset ) ;
147
+ expect ( builder . build ( ) ) . toEqual ( 'twothree' ) ;
148
+ } ) ;
149
+
150
+ it ( 'should handle null separator when using separator' , ( ) => {
151
+ const source = [ 'one' , 'two' , 'three' ] ;
152
+ const builder = new ImploderBuilder ( ) . source ( source ) . separator ( null ) ;
153
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
154
+ } ) ;
155
+
156
+ it ( 'should handle null suffix when using suffix' , ( ) => {
157
+ const source = [ 'one' , 'two' , 'three' ] ;
158
+ const builder = new ImploderBuilder ( ) . source ( source ) . suffix ( null ) ;
159
+ expect ( builder . build ( ) ) . toEqual ( source . join ( '' ) ) ;
160
+ } ) ;
161
+
162
+ it ( 'should handle an empty source when using source' , ( ) => {
163
+ const source : string [ ] = [ ] ;
164
+ const builder = new ImploderBuilder ( ) . source ( source ) ;
165
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
166
+ } ) ;
167
+
168
+ it ( 'should handle a source with empty strings when using source' , ( ) => {
169
+ const source = [ '' , '' , '' ] ;
170
+ const builder = new ImploderBuilder ( ) . source ( source ) ;
171
+ expect ( builder . build ( ) ) . toEqual ( '' ) ;
172
+ } ) ;
173
+
174
+ it ( 'should build correctly with maxLength and suffix' , ( ) => {
175
+ const source = [ 'one' , 'two' , 'three' ] ;
176
+ const maxLength = 5 ;
177
+ const suffix = '...' ;
178
+ const builder = new ImploderBuilder ( ) . source ( source ) . maxLength ( maxLength ) . suffix ( suffix ) ;
179
+ const expected = source . join ( '' ) . slice ( 0 , maxLength ) + suffix ;
180
+ expect ( builder . build ( ) ) . toEqual ( expected ) ;
181
+ } ) ;
182
+
183
+ it ( 'should build correctly with offset and separator' , ( ) => {
184
+ const source = [ 'one' , 'two' , 'three' , 'four' ] ;
185
+ const offset = 1 ;
186
+ const separator = ', ' ;
187
+ const builder = new ImploderBuilder ( ) . source ( source ) . offset ( offset ) . separator ( separator ) ;
188
+ const expected = source . slice ( offset ) . join ( separator ) ;
189
+ expect ( builder . build ( ) ) . toEqual ( expected ) ;
190
+ } ) ;
191
+
192
+ it ( 'should handle mapping with a custom mapping function when using mappedSource' , ( ) => {
193
+ const source = [ 1 , 2 , 3 ] ;
194
+ const mappingFunction = ( it : number ) => `Item ${ it } ` ;
195
+ const builder = new ImploderBuilder ( ) . mappedSource ( source , mappingFunction ) ;
196
+ const expected = source . map ( mappingFunction ) . join ( '' ) ;
197
+ expect ( builder . build ( ) ) . toEqual ( expected ) ;
198
+ } ) ;
199
+
200
+ it ( 'should handle mapping with a custom mapping function and maxLength when using mappedSource' , ( ) => {
201
+ const source = [ 1 , 2 , 3 ] ;
202
+ const mappingFunction = ( it : number ) => `Item ${ it } ` ;
203
+ const maxLength = 5 ;
204
+ const builder = new ImploderBuilder ( ) . mappedSource ( source , mappingFunction ) . maxLength ( maxLength ) ;
205
+ const expected = source . map ( mappingFunction ) . join ( '' ) . slice ( 0 , maxLength ) ;
206
+ expect ( builder . build ( ) ) . toEqual ( expected ) ;
207
+ } ) ;
208
+ } ) ;
0 commit comments