Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: Node CI

on: [push]
on:
push:
branches: [ "master" ]
pull_request:

jobs:
build:
name: build
runs-on: ubuntu-latest

strategy:
fail-fast: false
max-parallel: 0
matrix:
node-version: [14.x, 16.x, 18.x, 20.x, 22.x, 24.x]

Expand Down
27 changes: 27 additions & 0 deletions benchmarks/csv-to-json-basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const { readFileSync } = require('node:fs');
const { resolve: pathResolve } = require('node:path');
const { Bench } = require('tinybench');
const { CsvToJson } = require('../csv-to-json');

const csvFile = readFileSync(pathResolve(__dirname, '../test/resource/input.csv'), 'utf-8');
const csvFileWithQuotes = readFileSync(pathResolve(__dirname, '../test/resource/input_quoted_fields.csv'), 'utf-8');

const csv2json = new CsvToJson({
delimiter: ';',
supportQuotedField: true,
});

const bench = new Bench();

bench.add('csv-to-json basic', () => {
csv2json.csvStringToJson(csvFile);
});
bench.add('csv-to-json csvFileWithQuotes', () => {
csv2json.csvStringToJson(csvFileWithQuotes);
});

bench.run().then(() => {
console.table(bench.table());
});
131 changes: 131 additions & 0 deletions csv-to-json.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
export = CsvToJson;
declare class CsvToJson {
/**
* @param {CsvToJsonOptions} opts
*/
constructor(opts?: CsvToJsonOptions);
/**
* Prints a digit as Number type (for example 32 instead of '32')
* @param {boolean} [active = false]
* @returns {this}
*/
formatValueByType(active?: boolean): this;
/**
* Makes parser aware of quoted fields
* @param {boolean} [active = false]
* @returns {this}
*/
setSupportQuotedField(active?: boolean): this;
/**
* Defines the field delimiter which will be used to split the fields
* @param {string} [delimiter=';']
* @return {this}
* @throws {TypeError} when the delimiter is not a string or empty
*/
setDelimiter(delimiter?: string): this;
/**
* Configures if the content of the Header Fields is trimmed including the
* white spaces, e.g. "My Name" -> "MyName"
* @param {boolean} [active = false]
* @return {this}
*/
setShouldRemoveAllWhiteSpaceInHeaderField(active?: boolean): this;
/**
* Defines the index where the header is defined
* @param {number} indexHeader
* @return {this}
* @throws {TypeError} when the indexHeader is not a number
*/
setIndexHeader(indexHeader: number): this;
/**
* Defines how to match and parse a sub array
* @param {boolean} [active = false]
* @param {string} [delimiter='*']
* @param {string} [separator=',']
* @returns {this}
*/
setParseSubArray(active?: boolean, delimiter?: string, separator?: string): this;
/**
* Defines the file encoding
* @param {BufferEncoding} encoding
* @return {this}
*/
setEncoding(encoding?: BufferEncoding): this;
/**
* Parses .csv file and put its content into a file in json format.
* @param {string} fileInputName
* @param {string} fileOutputName
* @throws {Error} when input or output file name is not defined
* @return {void}
*/
generateJsonFileFromCsv(fileInputName: string, fileOutputName: string): void;
/**
* Parses .csv file and converts its content into a string in json format.
* @param {string} fileInputName
* @returns {string}
*/
getJsonStringifiedFromCsv(fileInputName: string): string;
/**
* Parses .csv file and put its content into an Array of Object in json format.
* @param {string} fileInputName
* @return {Array} Array of Object in json format
* @throws {Error} when input file name is not defined
*/
getJsonFromCsv(fileInputName: string): any[];
/**
* @param {string} csvString
* @returns {Array<Record<string, string|number|boolean|Array>>}
*/
csvStringToJson(csvString: string): Array<Record<string, string | number | boolean | any[]>>;
/**
* @param {CSVHeaders} headers
* @param {Array<string>} currentLine
* @returns {object}
*/
buildJsonResult(headers: CSVHeaders, currentLine: Array<string>): object;
#private;
}
declare namespace CsvToJson {
export { CsvToJson as default, CsvToJson, BufferEncoding, CSVHeaders, CsvToJsonOptions };
}
type BufferEncoding = (typeof encodings)[number];
type CSVHeaders = Array<string>;
type CsvToJsonOptions = {
/**
* The field delimiter which will be used to split the fields
*/
delimiter?: string;
/**
* The file encoding
*/
encoding?: BufferEncoding;
/**
* The index where the header is defined
*/
indexHeader?: number;
/**
* Makes parser aware of quoted fields
*/
supportQuotedField?: boolean;
/**
* Prints a digit as Number type (for example 32 instead of '32')
*/
printValueFormatByType?: boolean;
/**
* Whether to parse sub arrays
*/
parseSubArrays?: boolean;
/**
* The delimiter to identify a sub array
*/
parseSubArrayDelimiter?: string;
/**
* The separator to split values in a sub array
*/
parseSubArraySeparator?: string;
/**
* If active the content of the Header Fields is trimmed including the white spaces, e.g. "My Name" -> "MyName"
*/
removeAllWhiteSpaceInHeaderField?: boolean;
};
declare const encodings: readonly ["utf8", "ucs2", "utf16le", "latin1", "ascii", "base64", "hex"];
Loading