Debian Codegraph @

js/datasource.ts

raw
1 import { Dict } from '@jupyter-widgets/base';
2
3 export class DataSource {
4 constructor(
5 data: Dict<any[]>,
6 fields: any[],
7 schema: any,
8 computeSchema = false,
9 ) {
10 this._data = { ...data };
11
12 if (computeSchema) {
13 this._fields = fields;
14 this._schema = this._createSchema(fields, schema);
15 } else {
16 this._fields = fields;
17 this._schema = schema;
18 }
19
20 this._columns = Object.keys(this._data);
21 }
22
23 get data(): Dict<any[]> {
24 return this._data;
25 }
26
27 get fields(): any[] {
28 return this._fields;
29 }
30
31 get schema(): DataSource.ISchema {
32 return this._schema;
33 }
34
35 get columns(): string[] {
36 return this._columns;
37 }
38
39 get length(): number {
40 if (this._columns.length == 0) {
41 return 0;
42 }
43
44 return this.data[this._columns[0]].length;
45 }
46
47 setColumns(columnNames: string[]) {
48 this._columns = columnNames;
49 }
50
51 private _createSchema(
52 origFields: Dict<any>,
53 origSchema: any,
54 ): DataSource.ISchema {
55 // Construct a new array of schema fields based on the keys in data.fields
56 // Note: this accounts for how tuples/lists may be serialized into strings
57 // in the case of multiIndex columns.
58 const fields: DataSource.IField[] = [];
59 origFields.forEach((val: { [key: string]: null }, i: number) => {
60 const rows = Array.isArray(origSchema.fields[i].name)
61 ? <any[]>origSchema.fields[i].name
62 : <string[]>[origSchema.fields[i].name];
63 const field = {
64 name: Object.keys(val)[0],
65 type: origSchema.fields[i].type,
66 rows: rows,
67 };
68 fields.push(field);
69 });
70
71 // Updating the primary key to account for a multiIndex primary key.
72 const primaryKey = origSchema.primaryKey.map((key: string) => {
73 for (let i = 0; i < origSchema.fields.length; i++) {
74 const curFieldKey = Array.isArray(key)
75 ? origSchema.fields[i].name[0]
76 : origSchema.fields[i].name;
77 const newKey = Array.isArray(key) ? key[0] : key;
78
79 if (curFieldKey == newKey) {
80 return Object.keys(origFields[i])[0];
81 }
82 }
83 return 'unreachable';
84 });
85
86 return {
87 primaryKey: primaryKey,
88 primaryKeyUuid: origSchema.primaryKeyUuid,
89 fields: fields,
90 };
91 }
92
93 private _data: Dict<any[]>;
94 private _fields: any[];
95 private _schema: DataSource.ISchema;
96 private _columns: string[];
97 }
98
99 export namespace DataSource {
100 /**
101 * An object which describes a column of data in the model.
102 *
103 * #### Notes
104 * This is based on the JSON Table Schema specification:
105 * https://specs.frictionlessdata.io/table-schema/
106 */
107 export interface IField {
108 /**
109 * The name of the column.
110 *
111 * This is used as the key to extract a value from a data record.
112 */
113 readonly name: string;
114
115 /**
116 * The type of data held in the column.
117 */
118 readonly type: string;
119
120 /**
121 * An array of the column labels per header row.
122 */
123 readonly rows: any[];
124 }
125
126 export interface ISchema {
127 /**
128 * The fields which describe the view columns.
129 *
130 * Primary key fields are rendered as row header columns.
131 */
132 readonly fields: DataSource.IField[];
133
134 /**
135 * The values to treat as "missing" data.
136 *
137 * Missing values are automatically converted to `null`.
138 */
139 readonly missingValues?: string[];
140
141 /**
142 * The field names which act as primary keys.
143 *
144 * Primary key fields are rendered as row header columns.
145 */
146 readonly primaryKey: string | string[];
147
148 /**
149 * The name of the unique identifier in the primary key array
150 */
151 readonly primaryKeyUuid: string;
152 }
153 }