|
| 1 | +import { BuildSupportedMethodsArgs } from '$/types' |
| 2 | +import { |
| 3 | + emptyMetadata, |
| 4 | + type SupportedMethod, |
| 5 | +} from '@latitude-data/sql-compiler' |
| 6 | +import { QueryResultArray } from '@latitude-data/query_result' |
| 7 | + |
| 8 | +type Col = { |
| 9 | + name: string |
| 10 | + values: string[] |
| 11 | + maxLength: number |
| 12 | +} |
| 13 | + |
| 14 | +function padSpaces(str: string, length: number) { |
| 15 | + return str + ' '.repeat(Math.max(0, length - str.length)) |
| 16 | +} |
| 17 | + |
| 18 | +const buildCastMethod = (_: BuildSupportedMethodsArgs): SupportedMethod => ({ |
| 19 | + requirements: { |
| 20 | + interpolationPolicy: 'require', // Results can be interpolated |
| 21 | + interpolationMethod: 'raw', // When interpolating, use the raw value |
| 22 | + requireStaticArguments: false, // Can be used with variables or logic expressions |
| 23 | + }, |
| 24 | + |
| 25 | + resolve: async (value: QueryResultArray) => { |
| 26 | + // Check value is the correct type (array of objects) |
| 27 | + if (!Array.isArray(value) || !value.every((v) => typeof v === 'object')) { |
| 28 | + throw new Error('Value must be a query result.') |
| 29 | + } |
| 30 | + |
| 31 | + const cols: Col[] = [] |
| 32 | + |
| 33 | + value.forEach((row, i) => { |
| 34 | + Object.entries(row).forEach(([col, val]) => { |
| 35 | + let colIndex = cols.findIndex((c) => c.name === col) |
| 36 | + if (colIndex === -1) { |
| 37 | + colIndex = cols.length |
| 38 | + cols.push({ |
| 39 | + name: col, |
| 40 | + values: Array(value.length).fill(''), // Fill with empty strings |
| 41 | + maxLength: 0, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + const valStr = String(val).replace(/\n/g, '\\n') |
| 46 | + cols[colIndex]!.values[i] = valStr |
| 47 | + |
| 48 | + if (valStr.length > cols[colIndex]!.maxLength) { |
| 49 | + cols[colIndex]!.maxLength = valStr.length |
| 50 | + } |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + let table = '' |
| 55 | + |
| 56 | + // Add header row |
| 57 | + table += '| ' + cols.map(c => padSpaces(c.name, c.maxLength)).join(' | ') + ' |\n' |
| 58 | + table += '|-' + cols.map(c => '-'.repeat(c.maxLength)).join('-|-') + '-|\n' |
| 59 | + |
| 60 | + // Add data rows |
| 61 | + value.forEach((_, i) => { |
| 62 | + const cells = cols.map((c) => padSpaces(c.values[i]!, c.maxLength)) |
| 63 | + table += '| ' + cells.join(' | ') + ' |\n' |
| 64 | + }) |
| 65 | + |
| 66 | + return table |
| 67 | + }, |
| 68 | + |
| 69 | + readMetadata: async () => { |
| 70 | + return emptyMetadata() |
| 71 | + }, |
| 72 | +}) |
| 73 | + |
| 74 | +export default buildCastMethod |
0 commit comments