BREAKING CHANGE(core): switch to esm and enable white space trimming.
This commit is contained in:
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal 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'
|
||||
}
|
17
ts/index.ts
17
ts/index.ts
@ -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;
|
||||
|
Reference in New Issue
Block a user