1
- import { QueryResultMeta } from '../types/data ' ;
1
+ import { QueryResultMeta } from '../types' ;
2
2
import { Field , FieldType , DataFrame , TIME_SERIES_VALUE_FIELD_NAME } from '../types/dataFrame' ;
3
3
4
4
import { guessFieldTypeForField } from './processDataFrame' ;
@@ -16,18 +16,16 @@ export class ArrayDataFrame<T = any> implements DataFrame {
16
16
meta ?: QueryResultMeta ;
17
17
18
18
constructor ( source : T [ ] , names ?: string [ ] ) {
19
- return arrayToDataFrame ( source , names ) ; // returns a standard DataFrame
19
+ return arrayToDataFrame ( source , names ) as ArrayDataFrame < T > ; // returns a standard DataFrame
20
20
}
21
21
}
22
22
23
23
/**
24
- * arrayToDataFrame will convert any array into a DataFrame.
25
- * @param source - can be an array of objects or an array of simple values.
26
- * @param names - will be used for ordering of fields. Source needs to be array of objects if names are provided.
24
+ * arrayToDataFrame will convert any array into a DataFrame
27
25
*
28
26
* @public
29
27
*/
30
- export function arrayToDataFrame ( source : Array < Record < string , unknown > > | unknown [ ] , names ?: string [ ] ) : DataFrame {
28
+ export function arrayToDataFrame ( source : any [ ] , names ?: string [ ] ) : DataFrame {
31
29
const df : DataFrame = {
32
30
fields : [ ] ,
33
31
length : source . length ,
@@ -36,46 +34,30 @@ export function arrayToDataFrame(source: Array<Record<string, unknown>> | unknow
36
34
return df ;
37
35
}
38
36
39
- // If names are provided then we assume the source is an array of objects with the names as keys (field names). This
40
- // makes ordering of the fields predictable.
41
37
if ( names ) {
42
- if ( ! isObjectArray ( source ) ) {
43
- throw new Error ( 'source is not an array of objects' ) ;
44
- }
45
-
46
38
for ( const name of names ) {
47
39
df . fields . push (
48
40
makeFieldFromValues (
49
41
name ,
50
- source . map ( ( v ) => ( v ? v [ name ] : v ) )
42
+ source . map ( ( v ) => v [ name ] )
51
43
)
52
44
) ;
53
45
}
54
46
return df ;
55
47
}
56
48
57
- const firstDefined = source . find ( ( v ) => v ) ; // first not null|undefined
58
- // This means if the source is lots of null/undefined values we throw that away and return empty dataFrame. This is
59
- // different to how we preserve null/undefined values if there is some defined rows. Not sure this inconsistency
60
- // is by design or not.
61
- if ( firstDefined === null ) {
62
- return df ;
63
- }
64
-
65
- // If is an array of objects we use the keys as field names.
66
- if ( isObjectArray ( source ) ) {
67
- // We need to do this to please TS. We know source is array of objects and that there is some object in there but
68
- // TS still thinks it can all be undefined|nulls.
69
- const first = source . find ( ( v ) => v ) ;
70
- df . fields = Object . keys ( first || { } ) . map ( ( name ) => {
71
- return makeFieldFromValues (
72
- name ,
73
- source . map ( ( v ) => ( v ? v [ name ] : v ) )
74
- ) ;
75
- } ) ;
76
- } else {
77
- // Otherwise source should be an array of simple values, so we create single field data frame.
78
- df . fields . push ( makeFieldFromValues ( TIME_SERIES_VALUE_FIELD_NAME , source ) ) ;
49
+ const first = source . find ( ( v ) => v != null ) ; // first not null|undefined
50
+ if ( first != null ) {
51
+ if ( typeof first === 'object' ) {
52
+ df . fields = Object . keys ( first ) . map ( ( name ) => {
53
+ return makeFieldFromValues (
54
+ name ,
55
+ source . map ( ( v ) => v [ name ] )
56
+ ) ;
57
+ } ) ;
58
+ } else {
59
+ df . fields . push ( makeFieldFromValues ( TIME_SERIES_VALUE_FIELD_NAME , source ) ) ;
60
+ }
79
61
}
80
62
return df ;
81
63
}
@@ -85,8 +67,3 @@ function makeFieldFromValues(name: string, values: unknown[]): Field {
85
67
f . type = guessFieldTypeForField ( f ) ?? FieldType . other ;
86
68
return f ;
87
69
}
88
-
89
- function isObjectArray ( arr : unknown [ ] ) : arr is Array < Record < string , unknown > | null | undefined > {
90
- const first = arr . find ( ( v ) => v ) ; // first not null|undefined
91
- return arr . length > 0 && typeof first === 'object' ;
92
- }
0 commit comments