Skip to content

Commit

Permalink
Merge pull request #38 from airon-assustadus/master
Browse files Browse the repository at this point in the history
feature Adding index.d.ts for typescript usage
  • Loading branch information
iuccio authored Mar 29, 2024
2 parents 39f60cc + ec14602 commit 68c25cf
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 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;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.37.0",
"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 68c25cf

Please sign in to comment.