BREAKING CHANGE(core): switch to esm and enable white space trimming.

This commit is contained in:
2022-08-06 18:38:53 +02:00
parent c4194bf037
commit d7c1677c9d
10 changed files with 5586 additions and 19555 deletions

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartcsv',
version: '2.0.0',
description: 'handle csv data | gitzone standard compliant'
}

View File

@ -1,8 +1,9 @@
import * as plugins from './smartcsv.plugins';
import * as plugins from './smartcsv.plugins.js';
export interface ICsvConstructorOptions {
headers: boolean;
unquote?: boolean;
trimSpace?: boolean;
removeBomMarkers?: boolean;
}
@ -61,6 +62,7 @@ export class Csv {
public options: ICsvConstructorOptions = {
headers: true,
unquote: true,
trimSpace: true,
removeBomMarkers: true,
};
@ -74,6 +76,9 @@ export class Csv {
if (this.options.removeBomMarkers) {
csvStringToParse = csvStringToParse.replace(/^\uFEFF/, '');
}
// Quotes allow separators to be contained in quoted strings.
// To preserve them, when unquoting, we convert everything in between to base64 here
if (this.options.unquote) {
csvStringToParse = csvStringToParse.replace(
/"(.*?)"/gi,
@ -154,6 +159,13 @@ export class Csv {
}
this.headers = unquotedHeaders;
}
if (this.options.trimSpace) {
const trimmedHeaders = [];
for (const header of this.headers) {
trimmedHeaders.push(header.trim());
}
this.headers = trimmedHeaders;
}
}
return this.headers;
}
@ -170,6 +182,9 @@ export class Csv {
.replace(/['"]+/g, '')
.replace(/['"]+/g, '');
}
if (this.options.trimSpace) {
value = value.trim();
}
resultJson[this.headers[i]] = value;
}
return resultJson;