mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-11-28 08:15:01 +03:00
chore: updated util to specify the json format
This commit is contained in:
1 parent
5c71fb126d
commit
d75f108ab4
2 files changed
+14
-12
No files matched your search
@@ -84,11 +84,9 @@ export function convertJsonToCsv(
|
||||
|
||||
if (!delimiter) throw new Error('No CSV delimiter.');
|
||||
|
||||
const parsed = parseJsonInput(input);
|
||||
const { data } = parseJsonInput(input);
|
||||
|
||||
const rows = flattenToRows(parsed).filter(
|
||||
(row) => Object.keys(row).length > 0
|
||||
);
|
||||
const rows = flattenToRows(data).filter((row) => Object.keys(row).length > 0);
|
||||
|
||||
if (rows.length === 0) {
|
||||
throw new Error('No data found in the provided JSON.');
|
||||
|
||||
+12
-8
@@ -6,6 +6,13 @@ export type ParsedJson =
|
||||
| boolean
|
||||
| null;
|
||||
|
||||
export type JsonFormat = 'json' | 'jsonl';
|
||||
|
||||
export interface ParseJsonResult {
|
||||
data: ParsedJson;
|
||||
format: JsonFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a JSON string into its JavaScript representation.
|
||||
*
|
||||
@@ -18,9 +25,9 @@ export type ParsedJson =
|
||||
* Returns an empty string if the input is empty or whitespace-only.
|
||||
* @throws {Error} If the input contains invalid JSON.
|
||||
*/
|
||||
export function parseJsonInput(input: string): ParsedJson {
|
||||
export function parseJsonInput(input: string): ParseJsonResult {
|
||||
try {
|
||||
return JSON.parse(input);
|
||||
return { data: JSON.parse(input), format: 'json' };
|
||||
} catch (originalError) {
|
||||
const lines = input.split(/\r?\n/);
|
||||
|
||||
@@ -58,7 +65,7 @@ export function parseJsonInput(input: string): ParsedJson {
|
||||
}
|
||||
}
|
||||
|
||||
return parsedLines;
|
||||
return { data: parsedLines, format: 'jsonl' };
|
||||
}
|
||||
}
|
||||
/**
|
||||
@@ -78,11 +85,8 @@ export function getJsonHeaders(
|
||||
|
||||
if (typeof input === 'string') {
|
||||
try {
|
||||
const parsed = parseJsonInput(input);
|
||||
rows = (Array.isArray(parsed) ? parsed : [parsed]) as Record<
|
||||
string,
|
||||
string
|
||||
>[];
|
||||
const { data } = parseJsonInput(input);
|
||||
rows = (Array.isArray(data) ? data : [data]) as Record<string, string>[];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user