@@ -18,4 +18,60 @@ describe('cluster', () => {
18
18
expect ( c ) . toEqual ( [ 1 , 1 , 1 ] )
19
19
expect ( d ) . toEqual ( [ 2 , 2 ] )
20
20
} )
21
+
22
+ test ( 'handles empty array' , ( ) => {
23
+ const result = _ . cluster ( [ ] , 2 ) ;
24
+ expect ( result ) . toEqual ( [ ] ) ;
25
+ } ) ;
26
+
27
+ test ( 'creates individual clusters when size is 1' , ( ) => {
28
+ const list = [ 1 , 2 , 3 , 4 ] ;
29
+ const result = _ . cluster ( list , 1 ) ;
30
+ expect ( result ) . toEqual ( [ [ 1 ] , [ 2 ] , [ 3 ] , [ 4 ] ] ) ;
31
+ } ) ;
32
+
33
+ test ( 'creates a single cluster when size exceeds array length' , ( ) => {
34
+ const list = [ 1 , 2 , 3 ] ;
35
+ const result = _ . cluster ( list , 5 ) ;
36
+ expect ( result ) . toEqual ( [ [ 1 , 2 , 3 ] ] ) ;
37
+ } ) ;
38
+
39
+ test ( 'throws error when size is 0' , ( ) => {
40
+ const list = [ 1 , 2 , 3 ] ;
41
+ const size = 0
42
+ expect ( ( ) => _ . cluster ( list , size ) ) . toThrow ( _ . IllegalSizeError ) ;
43
+ expect ( ( ) => _ . cluster ( list , size ) ) . toThrow ( `Size must be 1 or more, the size is ${ size } ` ) ;
44
+ } ) ;
45
+
46
+ test ( 'throws error when size is negative' , ( ) => {
47
+ const list = [ 1 , 2 , 3 ] ;
48
+ const size = - 2
49
+ expect ( ( ) => _ . cluster ( list , size ) ) . toThrow ( _ . IllegalSizeError ) ;
50
+ expect ( ( ) => _ . cluster ( list , size ) ) . toThrow ( `Size must be 1 or more, the size is ${ size } ` ) ;
51
+ } ) ;
52
+
53
+ test ( 'uses default size of 2 when no size provided' , ( ) => {
54
+ const list = [ 1 , 2 , 3 , 4 , 5 ] ;
55
+ const actualResult = _ . cluster ( list ) ;
56
+ expect ( actualResult ) . toEqual ( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 ] ] ) ;
57
+ } ) ;
58
+
59
+ // THINK FURTHER
60
+ test ( 'throws error when size is NaN or Infinity' , ( ) => {
61
+ const list = [ 1 , 2 , 3 ] ;
62
+ expect ( ( ) => _ . cluster ( list , Number . NaN ) ) . toThrow ( ) ;
63
+ expect ( ( ) => _ . cluster ( list , Number . POSITIVE_INFINITY ) ) . toThrow ( ) ;
64
+ } ) ;
65
+
66
+ test ( 'handles arrays with mixed data types' , ( ) => {
67
+ const list = [ 1 , 'a' , true , { } , null ] ;
68
+ const actualResult = _ . cluster ( list , 2 ) ;
69
+ expect ( actualResult ) . toEqual ( [ [ 1 , 'a' ] , [ true , { } ] , [ null ] ] ) ;
70
+ } ) ;
71
+
72
+ test ( 'handles very large size values' , ( ) => {
73
+ const list = [ 1 , 2 , 3 ] ;
74
+ const actualResult = _ . cluster ( list , Number . MAX_SAFE_INTEGER ) ;
75
+ expect ( actualResult ) . toEqual ( [ [ 1 , 2 , 3 ] ] ) ;
76
+ } ) ;
21
77
} )
0 commit comments