// loaders.gl // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors import { deduceTableSchema } from "./table-schema.js"; export function makeTableFromData(data) { let table; switch (getTableShapeFromData(data)) { case 'array-row-table': table = { shape: 'array-row-table', data: data }; break; case 'object-row-table': table = { shape: 'object-row-table', data: data }; break; case 'columnar-table': table = { shape: 'columnar-table', data: data }; break; default: throw new Error('table'); } const schema = deduceTableSchema(table); return { ...table, schema }; } /** Helper function to get shape of data */ function getTableShapeFromData(data) { if (Array.isArray(data)) { if (data.length === 0) { throw new Error('cannot deduce type of empty table'); } // Deduce the table shape from the first row const firstRow = data[0]; if (Array.isArray(firstRow)) { return 'array-row-table'; } if (firstRow && typeof firstRow === 'object') { return 'object-row-table'; } } if (data && typeof data === 'object') { return 'columnar-table'; } throw new Error('invalid table'); } /** Convert any table into object row format * export function makeColumnarTable(table: Table): ColumnarTable { if (table.shape === 'columnar-table') { return table; } const length = getTableLength(table); const data = new Array<{[key: string]: unknown}>(length); for (let rowIndex = 0; rowIndex < length; rowIndex++) { data[rowIndex] = getTableRowAsObject(table, rowIndex); } return { shape: 'columnar-table', schema: table.schema, data }; } /** Convert any table into array row format * export function makeArrayRowTable(table: TableLike): ArrayRowTable { if (table.shape === 'array-row-table') { return table; } const length = getTableLength(table); const data = new Array(length); for (let rowIndex = 0; rowIndex < length; rowIndex++) { data[rowIndex] = getTableRowAsArray(table, rowIndex); } return { shape: 'array-row-table', schema: table.schema, data }; } /** Convert any table into object row format * export function makeObjectRowTable(table: Table): ObjectRowTable { if (table.shape === 'object-row-table') { return table; } const length = getTableLength(table); const data = new Array<{[key: string]: unknown}>(length); for (let rowIndex = 0; rowIndex < length; rowIndex++) { data[rowIndex] = getTableRowAsObject(table, rowIndex); } return { shape: 'object-row-table', schema: table.schema, data }; } */