Skip to content
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

Implementation - csv-delimiter #165

Open
wants to merge 1 commit into
base: feat/c4gt
Choose a base branch
from

Conversation

shashwatm1111
Copy link

Added new config parameter along with test cases to increase coverage ( For the issue #64 )

@@ -1,6 +1,7 @@
{
"globals": {
"onlyCreateWhitelisted": true
"onlyCreateWhitelisted": true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove onlyCreateWhitelist changes from this PR.

'./test/fixtures/test-csvs/csvreader/invalid.reader.csv'
);

expect(res).toBeDefined();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't only check the result to be defined, since you are parsing with a different delimiter, make sure it is parsed correctly.

export function getCsvDelimiter(configPath: string): string | undefined {
try {
const configContent = fs1.readFileSync(configPath, 'utf-8');
const config = JSON.parse(configContent);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.parse() has the potential to throw an error, wrap this is in a try-catch

try {
const configContent = fs1.readFileSync(configPath, 'utf-8');
const config = JSON.parse(configContent);
return config.globals.csvDelimiter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle errors here as well, there is no gurantee that this particular element is defined or not. Also make sure you are handling the case of this function returning as undefined wherever it is being being and defaulting to a ',' in such cases.

@@ -3,13 +3,20 @@ const fs = require('fs').promises;

import * as csv from 'csv-parser';

export async function readCSV(filePath: string): Promise<string[][]> {
export async function readCSV(filePath: string, configPath?: string, delimiter?: string): Promise<string[][]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't pass in configPath to a utility function like this. Read the required paramters (delimiter in this case) at the initial top level when the config is parsed and pass in the delimiter from there only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants