| 1 |
import { Dict } from '@jupyter-widgets/base'; |
| 2 |
import { array_or_json_serializer } from 'bqplot'; |
| 3 |
import { DataSource } from '../datasource'; |
| 4 |
|
| 5 |
export function unpack_raw_data( |
| 6 |
value: any | Dict<unknown> | string | (Dict<unknown> | string)[], |
| 7 |
): any { |
| 8 |
if (Array.isArray(value)) { |
| 9 |
const unpacked: any[] = []; |
| 10 |
value.forEach((sub_value, key) => { |
| 11 |
unpacked.push(unpack_raw_data(sub_value)); |
| 12 |
}); |
| 13 |
return unpacked; |
| 14 |
} else if (value instanceof Object && typeof value !== 'string') { |
| 15 |
const unpacked: { [key: string]: any } = {}; |
| 16 |
Object.keys(value).forEach((key) => { |
| 17 |
unpacked[key] = unpack_raw_data(value[key]); |
| 18 |
}); |
| 19 |
return unpacked; |
| 20 |
} else if (value === '$NaN$') { |
| 21 |
return Number.NaN; |
| 22 |
} else if (value === '$Infinity$') { |
| 23 |
return Number.POSITIVE_INFINITY; |
| 24 |
} else if (value === '$NegInfinity$') { |
| 25 |
return Number.NEGATIVE_INFINITY; |
| 26 |
} else if (value === '$NaT$') { |
| 27 |
return new Date('INVALID'); |
| 28 |
} else { |
| 29 |
return value; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
export function deserialize_data_simple(data: any, manager: any): any { |
| 34 |
const deserialized_data: any = {}; |
| 35 |
|
| 36 |
// Backward compatibility for when data.data was an array of rows |
| 37 |
// (should be removed in ipydatagrid 2.x?) |
| 38 |
if (Array.isArray(data.data)) { |
| 39 |
if (data.data.length === 0) { |
| 40 |
return deserialized_data; |
| 41 |
} |
| 42 |
|
| 43 |
const unpacked = unpack_raw_data(data.data); |
| 44 |
// Turn array of rows (old approach) into a dictionary of columns as arrays (new approach) |
| 45 |
for (const column of Object.keys(unpacked[0])) { |
| 46 |
const columnData = new Array(unpacked.length); |
| 47 |
let rowIdx = 0; |
| 48 |
|
| 49 |
for (const row of unpacked) { |
| 50 |
columnData[rowIdx++] = row[column]; |
| 51 |
} |
| 52 |
|
| 53 |
deserialized_data[column] = columnData; |
| 54 |
} |
| 55 |
|
| 56 |
return deserialized_data; |
| 57 |
} |
| 58 |
|
| 59 |
for (const column of Object.keys(data.data)) { |
| 60 |
deserialized_data[column] = []; |
| 61 |
|
| 62 |
if (Array.isArray(data.data[column])) { |
| 63 |
deserialized_data[column] = data.data[column]; |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
if (data.data[column].type == 'raw') { |
| 68 |
deserialized_data[column] = unpack_raw_data(data.data[column].value); |
| 69 |
} else { |
| 70 |
if (data.data[column].value.length !== 0) { |
| 71 |
let deserialized_array = array_or_json_serializer.deserialize( |
| 72 |
data.data[column], |
| 73 |
manager, |
| 74 |
); |
| 75 |
|
| 76 |
// Turning back float32 dates into isoformat |
| 77 |
if (deserialized_array.type === 'date') { |
| 78 |
const float32Array = deserialized_array; |
| 79 |
deserialized_array = []; |
| 80 |
|
| 81 |
for (let i = 0; i < float32Array.length; i++) { |
| 82 |
deserialized_array[i] = new Date(float32Array[i]).toISOString(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
deserialized_data[column] = deserialized_array; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
return deserialized_data |
| 91 |
} |
| 92 |
|
| 93 |
export function deserialize_data(data: any, manager: any): DataSource { |
| 94 |
const deserialized = deserialize_data_simple(data, manager); |
| 95 |
return new DataSource(deserialized, data.fields, data.schema, true); |
| 96 |
} |