11import { Row } from "@tanstack/react-table" ;
22import { RowDataType } from "cdm/FolderModel" ;
3+ import { FooterType } from "helpers/Constants" ;
34
45export default class Footer {
56 constructor ( public readonly rows : Row < RowDataType > [ ] ) { }
67
8+ public dispatch ( footerType : string , key : string ) : string {
9+ let footerInfo : string ;
10+ try {
11+ switch ( footerType ) {
12+ case FooterType . COUNT_UNIQUE :
13+ footerInfo = this . countUnique ( key ) ;
14+ break ;
15+ case FooterType . COUNT_EMPTY :
16+ footerInfo = this . countEmpty ( key ) ;
17+ break ;
18+ case FooterType . PERCENT_EMPTY :
19+ footerInfo = this . percentEmpty ( key ) ;
20+ break ;
21+ case FooterType . COUNT_FILLED :
22+ footerInfo = this . countFilled ( key ) ;
23+ break ;
24+ case FooterType . PERCENT_FILLED :
25+ footerInfo = this . percentFilled ( key ) ;
26+ break ;
27+ case FooterType . SUM :
28+ footerInfo = this . sum ( key ) ;
29+ break ;
30+ case FooterType . MIN :
31+ footerInfo = this . min ( key ) ;
32+ break ;
33+ case FooterType . MAX :
34+ footerInfo = this . max ( key ) ;
35+ break ;
36+ case FooterType . NONE :
37+ default :
38+ footerInfo = "" ;
39+ }
40+ } catch ( e ) {
41+ footerInfo = `Error: ${ e . message } ` ;
42+ } finally {
43+ return footerInfo ;
44+ }
45+ }
46+
747 public sum ( key : string ) : string {
8- const total = this . rows . reduce ( ( acc , row ) => acc + row . getValue < number > ( key ) , 0 ) ;
48+ const total = this . rows . reduce ( ( acc , row ) => acc + Number ( row . getValue < number > ( key ) ) , 0 ) ;
949 return `Total: ${ total } ` ;
1050 }
51+
52+ public min ( key : string ) : string {
53+ const min = this . rows . reduce ( ( acc , row ) => Math . min ( acc , row . getValue < number > ( key ) ) , Number . MAX_SAFE_INTEGER ) ;
54+ return `Min: ${ min } ` ;
55+ }
56+
57+ public max ( key : string ) : string {
58+ const max = this . rows . reduce ( ( acc , row ) => Math . max ( acc , row . getValue < number > ( key ) ) , Number . MIN_SAFE_INTEGER ) ;
59+ return `Max: ${ max } ` ;
60+ }
61+
62+ public countUnique ( key : string ) : string {
63+ const uniqueValues = new Set ( ) ;
64+ this . rows
65+ . filter ( ( row ) => row . getValue ( key ) !== undefined )
66+ . forEach ( ( row ) => {
67+ uniqueValues . add ( row . getValue ( key ) ) ;
68+ } ) ;
69+ return `Unique: ${ uniqueValues . size } ` ;
70+ }
71+
72+ public countEmpty ( key : string ) : string {
73+ const empty = this . rows . filter ( ( row ) => ! row . getValue ( key ) ) . length ;
74+ return `Empty: ${ empty } ` ;
75+ }
76+
77+ public percentEmpty ( key : string ) : string {
78+ const empty = this . rows . filter ( ( row ) => ! row . getValue ( key ) ) . length ;
79+ return `Empty: ${ ( empty / this . rows . length * 100 ) . toFixed ( 2 ) } %` ;
80+ }
81+
82+ public countFilled ( key : string ) : string {
83+ const filled = this . rows . filter ( ( row ) => row . getValue ( key ) ) . length ;
84+ return `Filled: ${ filled } ` ;
85+ }
86+
87+ public percentFilled ( key : string ) : string {
88+ const filled = this . rows . filter ( ( row ) => row . getValue ( key ) ) . length ;
89+ return `Filled: ${ ( filled / this . rows . length * 100 ) . toFixed ( 2 ) } %` ;
90+ }
91+
1192}
0 commit comments