-
Notifications
You must be signed in to change notification settings - Fork 129
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
refactor : ts and DOM integration : src/simulator/vendor/table2csv.ts #446
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request involves the removal of a jQuery-based Changes
Assessment against linked issues
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/simulator/vendor/table2csv.ts (2)
1-9
: Add parameter validation and extract options interface.Consider adding validation for the table parameter and extracting the options interface for better reusability.
+interface TableToCSVOptions { + separator?: string; + header?: string[]; + columnSelector?: string; + transformGtLt?: boolean; +} + function tableToCSV( table: HTMLTableElement, - options: { - separator?: string; - header?: string[]; - columnSelector?: string; - transformGtLt?: boolean; - } = {} + options: TableToCSVOptions = {} ): string { + if (!(table instanceof HTMLTableElement)) { + throw new Error('Invalid parameter: table must be an HTMLTableElement'); + }
28-36
: Optimize DOM queries and array allocations.Consider caching selector results and using more efficient array operations.
- const rows = table.querySelectorAll("tbody tr"); + const tbody = table.querySelector("tbody"); + if (!tbody) { + throw new Error("Table must contain a tbody element"); + } + const rows = tbody.rows; rows.forEach((row) => { - const columns = row.querySelectorAll(columnSelector); - const rowData = Array.from(columns).map((col) => formatData(col.textContent || "")); + const columns = Array.from(row.querySelectorAll(columnSelector)); + const rowData = columns.map((col) => formatData(col.textContent || "")); if (rowData.some((data) => data.trim() !== "")) { csvData.push(rowData.join(separator)); } });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/simulator/vendor/table2csv.js
(0 hunks)src/simulator/vendor/table2csv.ts
(1 hunks)
💤 Files with no reviewable changes (1)
- src/simulator/vendor/table2csv.js
🔇 Additional comments (2)
src/simulator/vendor/table2csv.ts (2)
1-45
: Overall implementation looks good with room for improvement.The TypeScript implementation successfully replaces the jQuery plugin with a more modern, type-safe approach. The code is well-structured and handles the core functionality effectively.
Consider adding comprehensive unit tests to verify:
- Header validation
- Empty row handling
- HTML entity transformation
- Edge cases with malformed tables
Would you like me to help generate test cases for these scenarios?
19-26
: Add header length validation.Consider validating that the header length matches the number of columns in the table to prevent data misalignment.
if (header.length > 0) { + const columnCount = table.querySelector('tbody tr')?.querySelectorAll(columnSelector).length ?? 0; + if (header.length !== columnCount) { + throw new Error(`Header length (${header.length}) does not match column count (${columnCount})`); + } csvData.push(header.map(formatData).join(separator)); } else {
// Transform `>` and `<` if required | ||
const result = csvData.join("\n"); | ||
return transformGtLt ? result.replace(/>/g, ">").replace(/</g, "<") : result; | ||
|
||
function formatData(input: string): string { | ||
return `"${input.replace(/"/g, '""').trim()}"`; | ||
} |
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.
🛠️ Refactor suggestion
Enhance HTML entity handling for security.
The current implementation only handles >
and <
entities. Consider using a more comprehensive HTML entity decoder to handle all possible entities.
- return transformGtLt ? result.replace(/>/g, ">").replace(/</g, "<") : result;
+ if (transformGtLt) {
+ const entities: Record<string, string> = {
+ '>': '>',
+ '<': '<',
+ '&': '&',
+ '"': '"',
+ ''': "'",
+ // Add more common entities as needed
+ };
+ return result.replace(/&[^;]+;/g, (entity) => entities[entity] || entity);
+ }
+ return result;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// Transform `>` and `<` if required | |
const result = csvData.join("\n"); | |
return transformGtLt ? result.replace(/>/g, ">").replace(/</g, "<") : result; | |
function formatData(input: string): string { | |
return `"${input.replace(/"/g, '""').trim()}"`; | |
} | |
// Transform `>` and `<` if required | |
const result = csvData.join("\n"); | |
if (transformGtLt) { | |
const entities: Record<string, string> = { | |
'>': '>', | |
'<': '<', | |
'&': '&', | |
'"': '"', | |
''': "'", | |
// Add more common entities as needed | |
}; | |
return result.replace(/&[^;]+;/g, (entity) => entities[entity] || entity); | |
} | |
return result; | |
function formatData(input: string): string { | |
return `"${input.replace(/"/g, '""').trim()}"`; | |
} |
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.
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.
@ThatDeparted2061 Hi! I noticed you tagged yourself. Do you need any clarification about the suggested HTML entity handling improvements? I'm here to help if you'd like to discuss the implementation or have any questions about the changes.
Fixes #414
Fixes #433
@JoshVarga @niladrix719 @tachyons
Summary by CodeRabbit