diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..cf9ccb2 --- /dev/null +++ b/index.d.ts @@ -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; +} diff --git a/package.json b/package.json index bbfdbd1..9cbd985 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/csvToJson.js b/src/csvToJson.js index 05879ff..1347c84 100644 --- a/src/csvToJson.js +++ b/src/csvToJson.js @@ -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);