@@ -5,40 +5,129 @@ import { xBinning } from '../xBinning.ts';
55test ( 'binSize 1 returns a copy' , ( ) => {
66 const array = [ 1 , 2 , 3 , 4 ] ;
77
8- expect ( xBinning ( array , 1 ) ) . toStrictEqual ( Float64Array . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
8+ expect ( xBinning ( array , { binSize : 1 } ) ) . toStrictEqual (
9+ Float64Array . from ( [ 1 , 2 , 3 , 4 ] ) ,
10+ ) ;
911} ) ;
1012
11- test ( 'even division' , ( ) => {
13+ test ( 'default keepFirstAndLast with even division' , ( ) => {
1214 const array = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;
1315
14- expect ( xBinning ( array , 2 ) ) . toStrictEqual ( Float64Array . from ( [ 1.5 , 3.5 , 5.5 ] ) ) ;
15- expect ( xBinning ( array , 3 ) ) . toStrictEqual ( Float64Array . from ( [ 2 , 5 ] ) ) ;
16+ expect ( xBinning ( array , { binSize : 2 } ) ) . toStrictEqual (
17+ Float64Array . from ( [ 1 , 2.5 , 4.5 , 6 ] ) ,
18+ ) ;
19+ expect ( xBinning ( array , { binSize : 3 } ) ) . toStrictEqual (
20+ Float64Array . from ( [ 1 , 3 , 5 , 6 ] ) ,
21+ ) ;
1622} ) ;
1723
18- test ( 'uneven division averages remaining points ' , ( ) => {
24+ test ( 'default keepFirstAndLast with uneven division ' , ( ) => {
1925 const array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
2026
21- expect ( xBinning ( array , 3 ) ) . toStrictEqual ( Float64Array . from ( [ 2 , 5 , 7 ] ) ) ;
27+ expect ( xBinning ( array , { binSize : 3 } ) ) . toStrictEqual (
28+ Float64Array . from ( [ 1 , 3 , 5.5 , 7 ] ) ,
29+ ) ;
2230} ) ;
2331
2432test ( 'accepts Float64Array input' , ( ) => {
2533 const array = Float64Array . from ( [ 2 , 4 , 6 , 8 ] ) ;
2634
27- expect ( xBinning ( array , 2 ) ) . toStrictEqual ( Float64Array . from ( [ 3 , 7 ] ) ) ;
35+ expect ( xBinning ( array , { binSize : 2 } ) ) . toStrictEqual (
36+ Float64Array . from ( [ 2 , 5 , 8 ] ) ,
37+ ) ;
38+ } ) ;
39+
40+ test ( 'default binSize is 10' , ( ) => {
41+ const array = Array . from ( { length : 25 } , ( _ , i ) => i + 1 ) ;
42+
43+ expect ( xBinning ( array ) ) . toStrictEqual (
44+ Float64Array . from ( [ 1 , 6.5 , 16.5 , 23 , 25 ] ) ,
45+ ) ;
46+ } ) ;
47+
48+ test ( 'keepFirstAndLast=false restores pure binning' , ( ) => {
49+ const array = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;
50+
51+ expect (
52+ xBinning ( array , { binSize : 2 , keepFirstAndLast : false } ) ,
53+ ) . toStrictEqual ( Float64Array . from ( [ 1.5 , 3.5 , 5.5 ] ) ) ;
54+ expect (
55+ xBinning ( array , { binSize : 3 , keepFirstAndLast : false } ) ,
56+ ) . toStrictEqual ( Float64Array . from ( [ 2 , 5 ] ) ) ;
57+ } ) ;
58+
59+ test ( 'keepFirstAndLast=false with uneven division' , ( ) => {
60+ const array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
61+
62+ expect (
63+ xBinning ( array , { binSize : 3 , keepFirstAndLast : false } ) ,
64+ ) . toStrictEqual ( Float64Array . from ( [ 2 , 5 , 7 ] ) ) ;
2865} ) ;
2966
3067test ( 'throws on invalid binSize' , ( ) => {
31- expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , 0 ) ) . toThrow (
68+ expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , { binSize : 0 } ) ) . toThrow (
3269 / b i n S i z e m u s t b e a p o s i t i v e i n t e g e r / ,
3370 ) ;
34- expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , 1.5 ) ) . toThrow (
71+ expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , { binSize : 1.5 } ) ) . toThrow (
3572 / b i n S i z e m u s t b e a p o s i t i v e i n t e g e r / ,
3673 ) ;
37- expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , - 2 ) ) . toThrow (
74+ expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , { binSize : - 2 } ) ) . toThrow (
3875 / b i n S i z e m u s t b e a p o s i t i v e i n t e g e r / ,
3976 ) ;
4077} ) ;
4178
4279test ( 'throws on empty input' , ( ) => {
43- expect ( ( ) => xBinning ( [ ] , 2 ) ) . toThrow ( / i n p u t m u s t n o t b e e m p t y / ) ;
80+ expect ( ( ) => xBinning ( [ ] , { binSize : 2 } ) ) . toThrow ( / i n p u t m u s t n o t b e e m p t y / ) ;
81+ } ) ;
82+
83+ test ( 'length <= 2 returns a copy' , ( ) => {
84+ expect ( xBinning ( [ 1 , 2 ] , { binSize : 2 } ) ) . toStrictEqual (
85+ Float64Array . from ( [ 1 , 2 ] ) ,
86+ ) ;
87+ expect ( xBinning ( [ 5 ] , { binSize : 2 } ) ) . toStrictEqual ( Float64Array . from ( [ 5 ] ) ) ;
88+ } ) ;
89+
90+ test ( 'numberOfPoints splits into N bins' , ( ) => {
91+ const array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ;
92+
93+ expect (
94+ xBinning ( array , { numberOfPoints : 5 , keepFirstAndLast : false } ) ,
95+ ) . toStrictEqual ( Float64Array . from ( [ 1.5 , 3.5 , 5.5 , 7.5 , 9.5 ] ) ) ;
96+ } ) ;
97+
98+ test ( 'numberOfPoints with uneven split' , ( ) => {
99+ const array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ;
100+
101+ expect (
102+ xBinning ( array , { numberOfPoints : 3 , keepFirstAndLast : false } ) ,
103+ ) . toStrictEqual ( Float64Array . from ( [ 2 , 5 , 8.5 ] ) ) ;
104+ } ) ;
105+
106+ test ( 'numberOfPoints with keepFirstAndLast' , ( ) => {
107+ const array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ;
108+
109+ expect ( xBinning ( array , { numberOfPoints : 5 } ) ) . toStrictEqual (
110+ Float64Array . from ( [ 1 , 2.5 , 5 , 8 , 10 ] ) ,
111+ ) ;
112+ } ) ;
113+
114+ test ( 'numberOfPoints throws when > length' , ( ) => {
115+ expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , { numberOfPoints : 4 } ) ) . toThrow (
116+ / n u m b e r O f P o i n t s m u s t b e < = a r r a y .l e n g t h / ,
117+ ) ;
118+ } ) ;
119+
120+ test ( 'numberOfPoints throws when not a positive integer' , ( ) => {
121+ expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , { numberOfPoints : 0 } ) ) . toThrow (
122+ / n u m b e r O f P o i n t s m u s t b e a p o s i t i v e i n t e g e r / ,
123+ ) ;
124+ expect ( ( ) => xBinning ( [ 1 , 2 , 3 ] , { numberOfPoints : 2.5 } ) ) . toThrow (
125+ / n u m b e r O f P o i n t s m u s t b e a p o s i t i v e i n t e g e r / ,
126+ ) ;
127+ } ) ;
128+
129+ test ( 'binSize and numberOfPoints are mutually exclusive' , ( ) => {
130+ expect ( ( ) =>
131+ xBinning ( [ 1 , 2 , 3 , 4 ] , { binSize : 2 , numberOfPoints : 2 } ) ,
132+ ) . toThrow ( / m u t u a l l y e x c l u s i v e / ) ;
44133} ) ;
0 commit comments