Skip to content

Commit 3e7999d

Browse files
authoredApr 18, 2023
Merge pull request #469 from Keyang/update-library
Refactor
2 parents 1535bcf + e5b60c8 commit 3e7999d

File tree

6 files changed

+9
-39
lines changed

6 files changed

+9
-39
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Thumbs.db
33
node_modules/
44
*.swp
55
*.swo
6+
.idea/
67

78
# intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
89
.grunt

‎bin/csvtojson.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function csvtojson() {
4343
}
4444
function stringToRegExp(str) {
4545
var lastSlash = str.lastIndexOf("/");
46-
var source = str.substring(1, lastSlash);
47-
var flag = str.substring(lastSlash + 1);
46+
var source = str.substr(1, lastSlash);
47+
var flag = str.substr(lastSlash + 1);
4848
return new RegExp(source,flag);
4949
}
5050
function parse() {

‎src/Converter.ts

+2-23
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@ import { Transform, TransformOptions, Readable } from "stream";
22
import { CSVParseParam, mergeParams } from "./Parameters";
33
import { ParseRuntime, initParseRuntime } from "./ParseRuntime";
44
import P from "bluebird";
5-
import { stringToLines } from "./fileline";
6-
import { map } from "lodash/map";
7-
import { RowSplit, RowSplitResult } from "./rowSplit";
8-
import getEol from "./getEol";
9-
import lineToJson, { JSONResult } from "./lineToJson";
10-
import { Processor, ProcessLineResult } from "./Processor";
11-
// import { ProcessorFork } from "./ProcessFork";
5+
import { Processor } from "./Processor";
126
import { ProcessorLocal } from "./ProcessorLocal";
137
import { Result } from "./Result";
148
import CSVError from "./CSVError";
15-
import { bufFromString } from "./util";
16-
17-
189

1910
export class Converter extends Transform implements PromiseLike<any[]> {
2011
preRawData(onRawData: PreRawDataCallback): Converter {
@@ -38,12 +29,6 @@ export class Converter extends Transform implements PromiseLike<any[]> {
3829
}
3930
fromFile(filePath: string, options?: string | CreateReadStreamOption | undefined): Converter {
4031
const fs = require("fs");
41-
// var rs = null;
42-
// this.wrapCallback(cb, function () {
43-
// if (rs && rs.destroy) {
44-
// rs.destroy();
45-
// }
46-
// });
4732
fs.exists(filePath, (exist) => {
4833
if (exist) {
4934
const rs = fs.createReadStream(filePath, options);
@@ -66,7 +51,7 @@ export class Converter extends Transform implements PromiseLike<any[]> {
6651
if (idx >= csvString.length) {
6752
this.push(null);
6853
} else {
69-
const str = csvString.substr(idx, size);
54+
const str = csvString.substring(idx, idx + size);
7055
this.push(str);
7156
idx += size;
7257
}
@@ -108,14 +93,8 @@ export class Converter extends Transform implements PromiseLike<any[]> {
10893
this.params = mergeParams(param);
10994
this.runtime = initParseRuntime(this);
11095
this.result = new Result(this);
111-
// if (this.params.fork) {
112-
// this.processor = new ProcessorFork(this);
113-
// } else {
11496
this.processor = new ProcessorLocal(this);
115-
// }
11697
this.once("error", (err: any) => {
117-
// console.log("BBB");
118-
//wait for next cycle to emit the errors.
11998
setImmediate(() => {
12099
this.result.processError(err);
121100
this.emit("done", err);

‎src/lineToJson.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { Converter } from "./Converter";
22
import CSVError from "./CSVError";
33
import { CellParser, ColumnParam } from "./Parameters";
44
import set from "lodash/set";
5-
import { ParseRuntime } from "./ParseRuntime";
65

7-
var numReg = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
6+
const numReg = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
87

98
export default function (csvRows: string[][], conv: Converter): JSONResult[] {
109
const res: JSONResult[] = [];
@@ -58,10 +57,6 @@ function convertRowToJson(row: string[], headRow: string[], conv: Converter): {
5857
setPath(resultRow, head, convRes, conv,i);
5958
}
6059
} else {
61-
// var flag = getFlag(head, i, param);
62-
// if (flag === 'omit') {
63-
// continue;
64-
// }
6560
if (conv.parseParam.checkType) {
6661
const convertFunc = checkType(item, head, i, conv);
6762
item = convertFunc(item);
@@ -193,12 +188,8 @@ function dynamicType(item) {
193188
}
194189

195190
function booleanType(item) {
196-
var trimed = item.trim();
197-
if (trimed.length === 5 && trimed.toLowerCase() === "false") {
198-
return false;
199-
} else {
200-
return true;
201-
}
191+
const trimmed = item.trim();
192+
return !(trimmed.length === 5 && trimmed.toLowerCase() === "false");
202193
}
203194

204195
function jsonType(item) {

‎src/rowSplit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class RowSplit {
6969
} else if (this.isQuoteOpen(e)) { //quote open
7070
e = e.substr(1);
7171
if (this.isQuoteClose(e)) { //quote close
72-
e = e.substring(0, e.lastIndexOf(quote));
72+
e = e.substr(0, e.lastIndexOf(quote));
7373
e = this.escapeQuote(e);
7474
row.push(e);
7575
continue;

‎v2/lineToJson.js

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Failed to load comments.