-
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
simpler markdown table generation #627
Changes from 1 commit
6e47e2f
d1cc290
4bceb29
012e5c9
b4a83c8
11084d9
b85c730
7f1ad4a
e94b9ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +0,0 @@ | ||
import { describe, test, beforeEach } from "node:test"; | ||
import assert from "node:assert/strict"; | ||
import { CSVParse, CSVTryParse, CSVToMarkdown } from "./csv"; | ||
|
||
describe('CSVParse', () => { | ||
test('Parse simple CSV data with default options', () => { | ||
const csv = "name,age\nJohn,30\nJane,25"; | ||
const result = CSVParse(csv); | ||
assert.deepEqual(result, [ | ||
{ name: "John", age: "30" }, | ||
{ name: "Jane", age: "25" } | ||
]); | ||
}); | ||
|
||
test('Parse CSV data with custom delimiter', () => { | ||
const csv = "name|age\nJohn|30\nJane|25"; | ||
const result = CSVParse(csv, { delimiter: "|" }); | ||
assert.deepEqual(result, [ | ||
{ name: "John", age: "30" }, | ||
{ name: "Jane", age: "25" } | ||
]); | ||
}); | ||
|
||
test('Parse CSV data with specified headers', () => { | ||
const csv = "John,30\nJane,25"; | ||
const result = CSVParse(csv, { headers: ["name", "age"] }); | ||
assert.deepEqual(result, [ | ||
{ name: "John", age: "30" }, | ||
{ name: "Jane", age: "25" } | ||
]); | ||
}); | ||
}); | ||
|
||
describe('CSVTryParse', () => { | ||
test('Try to parse valid CSV data', () => { | ||
const csv = "name,age\nJohn,30\nJane,25"; | ||
const result = CSVTryParse(csv); | ||
assert.deepEqual(result, [ | ||
{ name: "John", age: "30" }, | ||
{ name: "Jane", age: "25" } | ||
]); | ||
}); | ||
}); | ||
|
||
describe('CSVToMarkdown', () => { | ||
test('Convert parsed CSV data to markdown table', () => { | ||
const csv = [{ name: "John", age: "30" }, { name: "Jane", age: "25" }]; | ||
const result = CSVToMarkdown(csv); | ||
const expected = ` | ||
|name|age| | ||
|-|-| | ||
|John|30| | ||
|Jane|25| | ||
`.trim().replace(/[\t ]+/g, " "); | ||
assert.equal(result, expected); | ||
}); | ||
|
||
test('Convert parsed CSV data to markdown table with custom headers', () => { | ||
const csv = [{ name: "John", age: "30" }, { name: "Jane", age: "25" }]; | ||
const result = CSVToMarkdown(csv, { headers: ["age", "name"] }); | ||
const expected = ` | ||
|age|name| | ||
|-|-| | ||
|30|John| | ||
|25|Jane| | ||
`.trim().replace(/[\t ]+/g, " "); | ||
assert.equal(result, expected); | ||
}); | ||
|
||
test('Handle empty CSV data input', () => { | ||
const result = CSVToMarkdown([]); | ||
assert.equal(result, ""); | ||
}); | ||
}); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { parse } from "csv-parse/sync" | ||
import { markdownTable } from "markdown-table" | ||
import { TraceOptions } from "./trace" | ||
|
||
export function CSVParse( | ||
|
@@ -38,24 +37,25 @@ | |
} | ||
|
||
export function CSVToMarkdown(csv: object[], options?: { headers?: string[] }) { | ||
if (!csv?.length) return "" | ||
Check failure on line 40 in packages/core/src/csv.ts GitHub Actions / build
|
||
|
||
const { headers = Object.keys(csv[0]) } = options || {} | ||
const table: string[][] = [ | ||
headers, | ||
...csv.map((row) => | ||
headers.map((v) => { | ||
const rv = (row as any)[v] | ||
return rv === undefined ? "" : "" + rv | ||
}) | ||
const res: string[] = [ | ||
`|${headers.join("|")}|`, | ||
`|${headers.map(() => "---").join("|")}|`, | ||
...csv.map( | ||
(row) => | ||
`|${headers | ||
.map((key) => { | ||
const v = (row as any)[key] | ||
const s = v === undefined || v === null ? "" : "" + v | ||
return s | ||
.replace(/\s+$/, "") | ||
.replace(/[\\`*_{}[\]()#+\-.!]/g, (m) => "\\" + m) | ||
.replace(/\r?\n/g, "<br>") | ||
}) | ||
.join("|")}|` | ||
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 CSV parsing logic has been significantly changed. This includes changes to how headers are handled and how each row is processed. This could potentially introduce bugs or change the output format. Please ensure that these changes are thoroughly tested and that the new output format is as expected. 🧐
|
||
), | ||
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
const md = markdownTable(table, { | ||
stringLength: (str) => str.length, | ||
padding: false, | ||
alignDelimiters: false, | ||
}) | ||
// improves LLM performance | ||
const mdcompact = md.replace(/[\t ]+/g, " ") | ||
return mdcompact | ||
return res.join("\n") | ||
Check failure on line 60 in packages/core/src/csv.ts GitHub Actions / build
Check failure on line 60 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 new implementation of the CSVToMarkdown function does not properly escape the cell content. This can lead to incorrect markdown formatting if the cell content contains markdown special characters.
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 new implementation of the CSVToMarkdown function does not add a newline at the end of the generated markdown. This can cause issues when appending the generated markdown to other text.
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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 new implementation of the CSVToMarkdown function does not check if the csv parameter is null before accessing its length property. This can cause a runtime error if the function is called with a null argument.