Skip to content

Commit

Permalink
Merge branch 'airon-assustadus-master' into feature/type-script
Browse files Browse the repository at this point in the history
  • Loading branch information
iuccio committed Mar 29, 2024
2 parents 82af3be + 9625e3c commit 4074b79
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
98 changes: 98 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
declare module 'convert-csv-to-json' {
export class ConvertCsvToJson {
/**
* Prints a digit as Number type (for example 32 instead of '32')
*/
formatValueByType(active: boolean): this;

/**
*
*/
supportQuotedField(active: boolean): this;
/**
* Defines the field delimiter which will be used to split the fields
*/
fieldDelimiter(delimiter: string): this;

/**
* Defines the index where the header is defined
*/
indexHeader(index: number): this;

/**
* Defines how to match and parse a sub array
*/
parseSubArray(delimiter: string, separator: string): this;

/**
* Defines a custom encoding to decode a file
*/
customEncoding(encoding: string): this;

/**
* Defines a custom encoding to decode a file
*/
utf8Encoding(): this;

/**
* Defines ucs2 encoding to decode a file
*/
ucs2Encoding(): this;

/**
* Defines utf16le encoding to decode a file
*/
utf16leEncoding(): this;

/**
* Defines latin1 encoding to decode a file
*/
latin1Encoding(): this;

/**
* Defines ascii encoding to decode a file
*/
asciiEncoding(): this;

/**
* Defines base64 encoding to decode a file
*/
base64Encoding(): this;

/**
* Defines hex encoding to decode a file
*/
hexEncoding(): this;

/**
* Parses .csv file and put its content into a file in json format.
* @param {inputFileName} path/filename
* @param {outputFileName} path/filename
*
*/
generateJsonFileFromCsv(
inputFileName: string,
outputFileName: string,
): void;

/**
* Parses .csv file and put its content into an Array of Object in json format.
* @param {inputFileName} path/filename
* @return {Array} Array of Object in json format
*
*/
getJsonFromCsv(inputFileName: string): any[];

csvStringToJson(csvString: string): any[];
/**
* Parses .csv file and put its content into a file in json format.
* @param {inputFileName} path/filename
* @param {outputFileName} path/filename
*
* @deprecated Use generateJsonFileFromCsv()
*/
jsonToCsv(inputFileName: string, outputFileName: string): void;
}
const converter: ConvertCsvToJson;
export default converter;
}
4 changes: 2 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "convert-csv-to-json",
"version": "2.36.0",
"version": "2.37.1",
"description": "Convert CSV to JSON",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "jest",
"test-debug": "node --inspect-brk node_modules/.bin/jest --runInBand --detectOpenHandles",
Expand Down
10 changes: 8 additions & 2 deletions src/csvToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ class CsvToJson {
let lines = parsedCsv.split(newLine);
let fieldDelimiter = this.getFieldDelimiter();
let index = this.getIndexHeader();
let headers = lines[index].split(fieldDelimiter);

let headers;

if(this.isSupportQuotedField){
headers = this.split(lines[index]);
} else {
headers = lines[index].split(fieldDelimiter);
}

while(!stringUtils.hasContent(headers) && index <= lines.length){
index = index + 1;
headers = lines[index].split(fieldDelimiter);
Expand Down

0 comments on commit 4074b79

Please sign in to comment.