import { Category } from './annotations' ; import type { IKeyValue , IMapColumn } from './IArrayColumn' ; import type { IDataRow , IValueColumnDesc } from './interfaces' ; import ValueColumn from './ValueColumn' ; import { integrateDefaults } from './internal' ; export type IMapColumnDesc < T > = IValueColumnDesc < IKeyValue < T > [ ] > ; @ Category ( 'map' ) export default class MapColumn < T > extends ValueColumn < IKeyValue < T > [ ] > implements IMapColumn < T > { constructor ( id , desc ) { super ( id , integrateDefaults ( desc , { width : 200 , } ) ) ; } getValue ( row ) { const r = this . getMap ( row ) ; return r . length === 0 ? null : r ; } getLabels ( row ) { const v = this . getMap ( row ) ; return v . map ( ( { key , value } ) => ( { key , value : String ( value ) } ) ) ; } getMap ( row ) { return toKeyValue < T > ( super . getValue ( row ) ) ; } getMapLabel ( row ) { return this . getLabels ( row ) ; } getLabel ( row ) { const v = this . getLabels ( row ) ; return `{${ v . map ( ( { key , value } ) => `${ key }: ${ value }` ) . join ( ', ' ) }}` ; } } function byKey ( a , b ) { if ( a === b ) { return 0 ; } return a . key . localeCompare ( b . key ) ; } function toKeyValue < T > ( v ? ) { if ( ! v ) { return [ ] ; } if ( v instanceof Map ) { return Array . from ( v . entries ( ) ) . map ( ( [ key , value ] ) => ( { key , value } ) ) . sort ( byKey ) ; } if ( Array . isArray ( v ) ) { return v ; } return Object . keys ( v ) . map ( ( key ) => ( { key , value : v [ key ] } ) ) . sort ( byKey ) ; }