Skip to content

Commit 8e444d1

Browse files
committed
update trimLeft & trimRight for better performance
1 parent ffcd11e commit 8e444d1

10 files changed

Lines changed: 42 additions & 16 deletions

File tree

src/ProcessorLocal.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import P from "bluebird";
33
import { prepareData } from "./dataClean";
44
import getEol from "./getEol";
55
import { stringToLines } from "./fileline";
6-
import { bufFromString, filterArray } from "./util";
6+
import { bufFromString, filterArray,trimLeft } from "./util";
77
import { RowSplit } from "./rowSplit";
88
import lineToJson from "./lineToJson";
99
import { ParseRuntime } from "./ParseRuntime";
1010
import CSVError from "./CSVError";
1111

12+
13+
1214
export class ProcessorLocal extends Processor {
1315
flush(): P<ProcessLineResult[]> {
1416
if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
@@ -83,7 +85,7 @@ export class ProcessorLocal extends Processor {
8385
}
8486
// trim csv file has initial blank lines.
8587
if (params.ignoreEmpty && !runtime.started) {
86-
csv = csv.replace(/^\s+/, "");
88+
csv = trimLeft(csv);
8789
}
8890
const stringToLineResult = stringToLines(csv, runtime);
8991
if (!finalChunk) {

src/rowSplit.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CSVParseParam } from "./Parameters";
22
import { Converter } from "./Converter";
33
import { Fileline } from "./fileline";
44
import getEol from "./getEol";
5-
import { filterArray } from "./util";
5+
import { filterArray,trimLeft,trimRight } from "./util";
66

77
const defaulDelimiters = [",", "|", "\t", ";", ":"];
88
export class RowSplit {
@@ -59,7 +59,7 @@ export class RowSplit {
5959
for (let i = 0, rowLen = rowArr.length; i < rowLen; i++) {
6060
let e = rowArr[i];
6161
if (!inquote && trim) {
62-
e = e.replace(/^\s+/, "");
62+
e = trimLeft(e);
6363
}
6464
const len = e.length;
6565
if (!inquote) {
@@ -80,7 +80,7 @@ export class RowSplit {
8080
}
8181
} else {
8282
if (trim) {
83-
e = e.replace(/\s+$/, "");
83+
e = trimRight(e);
8484
}
8585
row.push(e);
8686
continue;
@@ -92,7 +92,7 @@ export class RowSplit {
9292
quoteBuff += delimiter + e;
9393
quoteBuff = this.escapeQuote(quoteBuff);
9494
if (trim) {
95-
quoteBuff = quoteBuff.replace(/\s+$/, "");
95+
quoteBuff = trimRight(quoteBuff);
9696
}
9797
row.push(quoteBuff);
9898
quoteBuff = "";
@@ -139,7 +139,7 @@ export class RowSplit {
139139
const quote = this.quote;
140140
const escape = this.escape;
141141
if (this.conv.parseParam.trim) {
142-
str = str.replace(/\s+$/, "");
142+
str = trimRight(str);
143143
}
144144
let count = 0;
145145
let idx = str.length - 1;

src/util.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ export function filterArray(arr: any[], filter: number[]): any[] {
2222
}
2323
}
2424
return rtn;
25-
}
25+
}
26+
27+
export const trimLeft=String.prototype.trimLeft?function trimLeftNative(str:string){
28+
return str.trimLeft();
29+
}:function trimLeftRegExp(str:string){
30+
return str.replace(/^\s+/, "");
31+
}
32+
33+
export const trimRight=String.prototype.trimRight?function trimRightNative(str:string){
34+
return str.trimRight();
35+
}:function trimRightRegExp(str:string){
36+
return str.replace(/\s+$/, "");
37+
}

v2/ProcessorLocal.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/ProcessorLocal.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/rowSplit.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/rowSplit.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/util.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
export declare function bufFromString(str: string): Buffer;
33
export declare function emptyBuffer(): Buffer;
44
export declare function filterArray(arr: any[], filter: number[]): any[];
5+
export declare const trimLeft: (str: string) => string;
6+
export declare const trimRight: (str: string) => string;

v2/util.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)