mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-09 08:03:50 +00:00
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { Parser } from '@json2csv/plainjs'
|
|
|
|
export function isLink(value: string) {
|
|
return value?.startsWith('http://') || value?.startsWith('https://')
|
|
}
|
|
|
|
export function isEmail(value: string) {
|
|
return value?.includes('@')
|
|
}
|
|
|
|
export function computeStructuredObjectsAndHeaders(
|
|
objects: Array<Record<string, any>>,
|
|
headersOverride?: string[]
|
|
): [
|
|
string[],
|
|
{
|
|
_id: number
|
|
rowData: Record<string, any>
|
|
}[]
|
|
] {
|
|
if (Array.isArray(objects)) {
|
|
let nextId = 1
|
|
|
|
// `Object.keys` reorders integer-like keys (e.g. "1234") ahead of
|
|
// insertion-ordered keys, so an explicit column order must be passed in
|
|
// rather than re-derived from the row objects.
|
|
let hds: string[] = headersOverride ? [...headersOverride] : []
|
|
let objs = objects.map((obj) => {
|
|
let rowData = obj && typeof obj == 'object' ? obj : {}
|
|
if (Array.isArray(rowData)) {
|
|
rowData = Object.fromEntries(rowData.map((x, i) => ['col' + i, x]))
|
|
}
|
|
let ks = Object.keys(rowData)
|
|
ks.forEach((x) => {
|
|
if (!hds.includes(x)) {
|
|
hds.push(x)
|
|
}
|
|
})
|
|
return {
|
|
_id: nextId++,
|
|
rowData
|
|
}
|
|
})
|
|
return [hds, objs]
|
|
} else {
|
|
return [[], []]
|
|
}
|
|
}
|
|
|
|
export function convertJsonToCsv(arr: Array<Record<string, any>>): string {
|
|
try {
|
|
const parser = new Parser({})
|
|
const csv = parser.parse(arr)
|
|
return csv
|
|
} catch (err) {
|
|
throw new Error('An error occurred when generating CSV:' + err)
|
|
}
|
|
}
|