2025-04-05 03:54:57 +02:00
|
|
|
import { InitialValuesType } from './types';
|
|
|
|
|
import { getCsvHeaders, splitCsv } from '@utils/csv';
|
|
|
|
|
import { unquoteIfQuoted } from '@utils/string';
|
|
|
|
|
|
|
|
|
|
function toYaml(
|
|
|
|
|
input: Record<string, string>[] | string[][],
|
|
|
|
|
indentSpaces: number = 2
|
|
|
|
|
): string {
|
|
|
|
|
const indent = ' '.repeat(indentSpaces);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
Array.isArray(input) &&
|
|
|
|
|
input.length > 0 &&
|
|
|
|
|
typeof input[0] === 'object' &&
|
|
|
|
|
!Array.isArray(input[0])
|
|
|
|
|
) {
|
|
|
|
|
return (input as Record<string, string>[])
|
|
|
|
|
.map((obj) => {
|
|
|
|
|
const lines = Object.entries(obj)
|
|
|
|
|
.map(([key, value]) => `${indent}${key}: ${value}`)
|
|
|
|
|
.join('\n');
|
2025-04-06 01:47:10 +02:00
|
|
|
return `-\n${lines}`;
|
2025-04-05 03:54:57 +02:00
|
|
|
})
|
2025-04-06 01:47:10 +02:00
|
|
|
.join('\n');
|
2025-04-05 03:54:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If input is string[][].
|
|
|
|
|
if (Array.isArray(input) && Array.isArray(input[0])) {
|
|
|
|
|
return (input as string[][])
|
|
|
|
|
.map((row) => {
|
2025-04-06 01:47:10 +02:00
|
|
|
const inner = row.map((cell) => `${indent}- ${cell}`).join('\n');
|
|
|
|
|
return `-\n${inner}`;
|
2025-04-05 03:54:57 +02:00
|
|
|
})
|
2025-04-06 01:47:10 +02:00
|
|
|
.join('\n');
|
2025-04-05 03:54:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'invalid input';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function main(input: string, options: InitialValuesType): string {
|
|
|
|
|
if (!input) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rows = splitCsv(
|
|
|
|
|
input,
|
|
|
|
|
true,
|
|
|
|
|
options.commentCharacter,
|
|
|
|
|
options.emptyLines,
|
|
|
|
|
options.csvSeparator
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
rows.forEach((row) => {
|
|
|
|
|
row.forEach((cell, cellIndex) => {
|
|
|
|
|
row[cellIndex] = unquoteIfQuoted(cell, options.quoteCharacter);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (options.headerRow) {
|
|
|
|
|
const headerRow = getCsvHeaders(input, options.csvSeparator);
|
2025-04-05 13:34:35 +02:00
|
|
|
headerRow.forEach((header, headerIndex) => {
|
|
|
|
|
headerRow[headerIndex] = unquoteIfQuoted(header, options.quoteCharacter);
|
|
|
|
|
});
|
2025-04-05 03:54:57 +02:00
|
|
|
|
|
|
|
|
const result: Record<string, string>[] = rows.slice(1).map((row) => {
|
|
|
|
|
const entry: Record<string, string> = {};
|
|
|
|
|
headerRow.forEach((header, headerIndex) => {
|
|
|
|
|
entry[header] = row[headerIndex] ?? '';
|
|
|
|
|
});
|
|
|
|
|
return entry;
|
|
|
|
|
});
|
|
|
|
|
return toYaml(result, options.spaces);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toYaml(rows, options.spaces);
|
|
|
|
|
}
|