@uwdata/mosaic-core
Version:
Scalable and extensible linked data views.
51 lines • 1.51 kB
JavaScript
import { isArrowTable } from './is-arrow-table.js';
/**
* Convert input data to a set of column arrays.
* @param data The input data.
* @returns An object with named column arrays.
*/
export function toDataColumns(data) {
if (isArrowTable(data)) {
return arrowToColumns(data);
}
else if (Array.isArray(data)) {
return arrayToColumns(data);
}
else {
throw new Error('Unrecognized data format.');
}
}
/**
* Convert an Arrow table to a set of column arrays.
* @param data An Arrow Table.
* @returns An object with named column arrays.
*/
function arrowToColumns(data) {
const { numRows } = data;
return { numRows, columns: data.toColumns() };
}
/**
* Convert an array of values to a set of column arrays.
* If the array values are objects, build out named columns.
* We use the keys of the first object as the column names.
* Otherwise, use a special "values" array.
* @param data An array of data objects.
* @returns An object with named column arrays.
*/
function arrayToColumns(data) {
const numRows = data.length;
if (typeof data[0] === 'object') {
const names = numRows ? Object.keys(data[0]) : [];
const columns = {};
if (names.length > 0) {
names.forEach(name => {
columns[name] = data.map(d => d[name]);
});
}
return { numRows, columns };
}
else {
return { numRows, values: data };
}
}
//# sourceMappingURL=to-data-columns.js.map