-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CSV stringification functionality and remove verbose error logging #726
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// Import the CSV parsing function from the csv-parse library | ||
// This module provides functions for parsing and converting CSV data, | ||
// including error handling and conversion to Markdown table format. | ||
|
||
import { parse } from "csv-parse/sync" | ||
// Import the TraceOptions interface for logging trace information | ||
import { TraceOptions } from "./trace" | ||
import { stringify } from "csv-stringify/sync" | ||
|
||
/** | ||
* Parses a CSV string into an array of objects. | ||
|
@@ -61,6 +63,18 @@ | |
} | ||
} | ||
|
||
/** | ||
* Converts an array of objects into a CSV string. | ||
* | ||
* @param csv - An array of objects to be converted into CSV format. | ||
* @param options - Optional configuration for CSV stringification. | ||
* @returns A string representing the CSV formatted data. | ||
*/ | ||
export function CSVStringify(csv: object[], options?: CSVStringifyOptions) { | ||
// Convert objects to CSV string using the provided options | ||
return stringify(csv, options) | ||
} | ||
Check failure on line 76 in packages/core/src/csv.ts GitHub Actions / build
|
||
|
||
/** | ||
* Converts an array of objects into a Markdown table format. | ||
* | ||
|
@@ -70,7 +84,7 @@ | |
* @returns A string representing the CSV data in Markdown table format. | ||
*/ | ||
export function CSVToMarkdown(csv: object[], options?: { headers?: string[] }) { | ||
if (!csv?.length) return "" | ||
if (!csv?.length) return "" // Return empty string if CSV is empty | ||
|
||
const { headers = Object.keys(csv[0]) } = options || {} | ||
const res: string[] = [ | ||
|
@@ -84,11 +98,11 @@ | |
const s = v === undefined || v === null ? "" : String(v) | ||
// Escape special Markdown characters and format cell content | ||
return s | ||
.replace(/\s+$/, "") | ||
.replace(/[\\`*_{}[\]()#+\-.!]/g, (m) => "\\" + m) | ||
.replace(/</g, "lt;") | ||
.replace(/>/g, "gt;") | ||
.replace(/\r?\n/g, "<br>") | ||
.replace(/\s+$/, "") // Trim trailing whitespace | ||
.replace(/[\\`*_{}[\]()#+\-.!]/g, (m) => "\\" + m) // Escape special characters | ||
.replace(/</g, "lt;") // Replace '<' with its HTML entity | ||
.replace(/>/g, "gt;") // Replace '>' with its HTML entity | ||
.replace(/\r?\n/g, "<br>") // Replace newlines with <br> | ||
Check failure on line 105 in packages/core/src/csv.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The escape handling in the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The replacement of '<' and '>' with 'lt;' and 'gt;' respectively is incorrect. It should be replaced with their correct HTML entities '<' and '>'. This could lead to incorrect data representation. 🚀
|
||
}) | ||
.join("|")}|` // Join columns with '|' | ||
), | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
CSVStringify
function does not handle potential errors that may occur during the stringification process. It's recommended to wrap thestringify
function call in a try-catch block to handle any exceptions and provide a more graceful error handling.