@@ -3,7 +3,26 @@ import {maybeTransposeData} from './index';
3
3
4
4
const SRC_ATTR_PATTERN = / s r c $ / ;
5
5
6
- export default function dereference ( container , dataSources , config = { deleteKeys : false } ) {
6
+ export function getColumnNames ( srcArray , dataSourceOptions ) {
7
+ return srcArray
8
+ . map ( src => {
9
+ const columns = dataSourceOptions . filter ( dso => dso . value === src ) ;
10
+ if ( columns . length === 1 ) {
11
+ return columns [ 0 ] . columnName || columns [ 0 ] . label ;
12
+ }
13
+ return '' ;
14
+ } )
15
+ . join ( ' - ' ) ;
16
+ }
17
+
18
+ export default function dereference (
19
+ container ,
20
+ dataSources ,
21
+ config = { deleteKeys : false } ,
22
+ dataSourceOptions = null
23
+ ) {
24
+ const containerIsData = Array . isArray ( container ) ;
25
+
7
26
const replacer = ( key , parent , srcPath ) => {
8
27
if ( ! SRC_ATTR_PATTERN . test ( key ) ) {
9
28
return ;
@@ -34,22 +53,31 @@ export default function dereference(container, dataSources, config = {deleteKeys
34
53
return ;
35
54
}
36
55
37
- if ( Array . isArray ( container ) ) {
38
- // Case where we were originally given data to dereference
39
- const traceType = parent . type ;
40
- parent [ dataKey ] = maybeTransposeData ( dereferencedData , srcPath , traceType ) ;
56
+ if ( containerIsData ) {
57
+ if ( parent . type !== null ) {
58
+ // we're at the top level of the trace
59
+ if ( dataSourceOptions !== null ) {
60
+ parent . meta = parent . meta || { } ;
61
+ parent . meta . columnNames = parent . meta . columnNames || { } ;
62
+ parent . meta . columnNames [ dataKey ] = getColumnNames ( srcRef , dataSourceOptions ) ;
63
+ }
64
+ parent [ dataKey ] = maybeTransposeData ( dereferencedData , srcPath , parent . type ) ;
65
+ } else {
66
+ parent [ dataKey ] = dereferencedData ;
67
+ }
41
68
} else {
42
- // This means we're dereferencing layout
69
+ // container is layout
43
70
parent [ dataKey ] = dereferencedData ;
44
71
}
45
72
} ;
46
73
47
- if ( Array . isArray ( container ) ) {
74
+ if ( containerIsData ) {
48
75
walkObject ( container , replacer , {
49
76
walkArraysMatchingKeys : [ 'data' , 'transforms' ] ,
50
77
pathType : 'nestedProperty' ,
51
78
} ) ;
52
79
} else {
80
+ // container is layout
53
81
walkObject ( container , replacer , { pathType : 'nestedProperty' } ) ;
54
82
}
55
83
}
0 commit comments