feat(csv): add typed CSV APIs and modernize package configuration
This commit is contained in:
+17
-13
@@ -1,5 +1,9 @@
|
||||
import * as plugins from './smartcsv.plugins.js';
|
||||
|
||||
export type TCsvCellValue = string | number | boolean | null | undefined;
|
||||
export type TCsvObjectRow = Record<string, TCsvCellValue>;
|
||||
export type TCsvParsedRow = Record<string, string>;
|
||||
|
||||
export interface ICsvConstructorOptions {
|
||||
headers: boolean;
|
||||
unquote?: boolean;
|
||||
@@ -24,7 +28,7 @@ export class Csv {
|
||||
return csvInstance;
|
||||
}
|
||||
|
||||
public static async createCsvStringFromArray(arrayArg: any[]): Promise<string> {
|
||||
public static async createCsvStringFromArray(arrayArg: TCsvObjectRow[]): Promise<string> {
|
||||
const foundKeys: string[] = [];
|
||||
|
||||
// lets deal with the keys
|
||||
@@ -39,7 +43,7 @@ export class Csv {
|
||||
for (const objectArg of arrayArg) {
|
||||
const dataRowArray: string[] = [];
|
||||
for (const key of foundKeys) {
|
||||
dataRowArray.push(objectArg[key]);
|
||||
dataRowArray.push(String(objectArg[key] ?? ''));
|
||||
}
|
||||
dataRows.push(dataRowArray.join(','));
|
||||
}
|
||||
@@ -53,9 +57,9 @@ export class Csv {
|
||||
|
||||
// INSTANCE
|
||||
public csvString: string;
|
||||
public headers: string[];
|
||||
public keyFrame: string = null;
|
||||
public data: any[];
|
||||
public headers: string[] = [];
|
||||
public keyFrame: ',' | ';' = ',';
|
||||
public data: TCsvParsedRow[] = [];
|
||||
|
||||
private base64RecognitionPrefix = '####12345####';
|
||||
|
||||
@@ -118,9 +122,9 @@ export class Csv {
|
||||
/**
|
||||
* serializes the csv string
|
||||
*/
|
||||
private serializeCsvString() {
|
||||
private serializeCsvString(): string[][] {
|
||||
const rowArray = this.getRows();
|
||||
const resultArray = [];
|
||||
const resultArray: string[][] = [];
|
||||
if (this.options.headers) {
|
||||
this.getHeaders();
|
||||
rowArray.shift();
|
||||
@@ -142,7 +146,7 @@ export class Csv {
|
||||
return rowsArray;
|
||||
}
|
||||
|
||||
private getHeaders() {
|
||||
private getHeaders(): string[] {
|
||||
const rowArray = this.getRows();
|
||||
if (this.options.headers) {
|
||||
let headerRow = rowArray[0];
|
||||
@@ -160,7 +164,7 @@ export class Csv {
|
||||
this.headers = unquotedHeaders;
|
||||
}
|
||||
if (this.options.trimSpace) {
|
||||
const trimmedHeaders = [];
|
||||
const trimmedHeaders: string[] = [];
|
||||
for (const header of this.headers) {
|
||||
trimmedHeaders.push(header.trim());
|
||||
}
|
||||
@@ -170,9 +174,9 @@ export class Csv {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
private createDataObject(dataArray: string[]) {
|
||||
private createDataObject(dataArray: string[]): TCsvParsedRow {
|
||||
const neededIterations = dataArray.length;
|
||||
let resultJson: any = {};
|
||||
const resultJson: TCsvParsedRow = {};
|
||||
for (let i = 0; i < neededIterations; i++) {
|
||||
let value = dataArray[i];
|
||||
if (this.options.unquote && value.startsWith(this.base64RecognitionPrefix)) {
|
||||
@@ -190,9 +194,9 @@ export class Csv {
|
||||
return resultJson;
|
||||
}
|
||||
|
||||
public async exportAsObject(): Promise<any> {
|
||||
public async exportAsObject(): Promise<TCsvParsedRow[]> {
|
||||
const serializedData = this.serializeCsvString();
|
||||
const dataObjects = [];
|
||||
const dataObjects: TCsvParsedRow[] = [];
|
||||
for (const dataArray of serializedData) {
|
||||
dataObjects.push(this.createDataObject(dataArray));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user