Skip to content

Commit

Permalink
Add CSV stringification functionality and remove verbose error logging (
Browse files Browse the repository at this point in the history
#726)

* Add CSV stringification functionality with tests and update dependencies

* Remove verbose error logging from token estimation function

* Add error logging for encoding failures in token estimation function
  • Loading branch information
pelikhan authored Sep 25, 2024
1 parent 2941fa4 commit b9e859a
Show file tree
Hide file tree
Showing 23 changed files with 691 additions and 190 deletions.
45 changes: 35 additions & 10 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 35 additions & 10 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 35 additions & 10 deletions packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"ajv": "^8.17.1",
"cross-fetch": "^4.0.0",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.5.1",
"dotenv": "^16.4.5",
"esbuild": "^0.24.0",
"fast-xml-parser": "^4.5.0",
Expand Down
32 changes: 31 additions & 1 deletion packages/core/src/csv.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, test, beforeEach } from "node:test"
import assert from "node:assert/strict"
import { CSVParse, CSVTryParse, CSVToMarkdown } from "./csv"
import { CSVParse, CSVTryParse, CSVToMarkdown, CSVStringify } from "./csv"

describe("CSVParse", () => {
test("Parse simple CSV data with default options", () => {
Expand Down Expand Up @@ -82,3 +82,33 @@ describe("CSVToMarkdown", () => {
assert.equal(result, "")
})
})
describe("CSVStringify", () => {
test("Stringify simple CSV data with default options", () => {
const csv = [
{ name: "John", age: "30" },
{ name: "Jane", age: "25" },
]
const result = CSVStringify(csv)
const expected = "John,30\nJane,25\n"
assert.equal(result, expected)
})
test("Stringify simple CSV data with headers", () => {
const csv = [
{ name: "John", age: "30" },
{ name: "Jane", age: "25" },
]
const result = CSVStringify(csv, { header: true })
const expected = "name,age\nJohn,30\nJane,25\n"
assert.equal(result, expected)
})

test("Stringify CSV data with custom delimiter", () => {
const csv = [
{ name: "John", age: "30" },
{ name: "Jane", age: "25" },
]
const result = CSVStringify(csv, { header: true, delimiter: "|" })
const expected = "name|age\nJohn|30\nJane|25\n"
assert.equal(result, expected)
})
})
30 changes: 22 additions & 8 deletions packages/core/src/csv.ts
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.
Expand Down Expand Up @@ -61,6 +63,18 @@ export function CSVTryParse(
}
}

/**
* 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)
}

/**
* Converts an array of objects into a Markdown table format.
*
Expand All @@ -70,7 +84,7 @@ export function CSVTryParse(
* @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[] = [
Expand All @@ -84,11 +98,11 @@ export function CSVToMarkdown(csv: object[], options?: { headers?: string[] }) {
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>
})
.join("|")}|` // Join columns with '|'
),
Expand Down
45 changes: 35 additions & 10 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/core/src/globals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Import various parsing and stringifying utilities
import { YAMLParse, YAMLStringify } from "./yaml"
import { CSVParse, CSVToMarkdown } from "./csv"
import { CSVParse, CSVToMarkdown, CSVStringify } from "./csv"
import { INIParse, INIStringify } from "./ini"
import { XMLParse } from "./xml"
import {
Expand Down Expand Up @@ -46,6 +46,7 @@ export function installGlobals() {
// Freeze CSV utilities
glb.CSV = Object.freeze<CSV>({
parse: CSVParse,
stringify: CSVStringify,
markdownify: CSVToMarkdown,
})

Expand Down
Loading

0 comments on commit b9e859a

Please sign in to comment.