Skip to content

Commit

Permalink
Merge pull request #469 from Keyang/update-library
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
dogabudak committed Apr 18, 2023
2 parents 1535bcf + e5b60c8 commit 3e7999d
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Thumbs.db
node_modules/
*.swp
*.swo
.idea/

# intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
4 changes: 2 additions & 2 deletions bin/csvtojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function csvtojson() {
}
function stringToRegExp(str) {
var lastSlash = str.lastIndexOf("/");
var source = str.substring(1, lastSlash);
var flag = str.substring(lastSlash + 1);
var source = str.substr(1, lastSlash);
var flag = str.substr(lastSlash + 1);
return new RegExp(source,flag);
}
function parse() {
Expand Down
25 changes: 2 additions & 23 deletions src/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@ import { Transform, TransformOptions, Readable } from "stream";
import { CSVParseParam, mergeParams } from "./Parameters";
import { ParseRuntime, initParseRuntime } from "./ParseRuntime";
import P from "bluebird";
import { stringToLines } from "./fileline";
import { map } from "lodash/map";
import { RowSplit, RowSplitResult } from "./rowSplit";
import getEol from "./getEol";
import lineToJson, { JSONResult } from "./lineToJson";
import { Processor, ProcessLineResult } from "./Processor";
// import { ProcessorFork } from "./ProcessFork";
import { Processor } from "./Processor";
import { ProcessorLocal } from "./ProcessorLocal";
import { Result } from "./Result";
import CSVError from "./CSVError";
import { bufFromString } from "./util";



export class Converter extends Transform implements PromiseLike<any[]> {
preRawData(onRawData: PreRawDataCallback): Converter {
Expand All @@ -38,12 +29,6 @@ export class Converter extends Transform implements PromiseLike<any[]> {
}
fromFile(filePath: string, options?: string | CreateReadStreamOption | undefined): Converter {
const fs = require("fs");
// var rs = null;
// this.wrapCallback(cb, function () {
// if (rs && rs.destroy) {
// rs.destroy();
// }
// });
fs.exists(filePath, (exist) => {
if (exist) {
const rs = fs.createReadStream(filePath, options);
Expand All @@ -66,7 +51,7 @@ export class Converter extends Transform implements PromiseLike<any[]> {
if (idx >= csvString.length) {
this.push(null);
} else {
const str = csvString.substr(idx, size);
const str = csvString.substring(idx, idx + size);
this.push(str);
idx += size;
}
Expand Down Expand Up @@ -108,14 +93,8 @@ export class Converter extends Transform implements PromiseLike<any[]> {
this.params = mergeParams(param);
this.runtime = initParseRuntime(this);
this.result = new Result(this);
// if (this.params.fork) {
// this.processor = new ProcessorFork(this);
// } else {
this.processor = new ProcessorLocal(this);
// }
this.once("error", (err: any) => {
// console.log("BBB");
//wait for next cycle to emit the errors.
setImmediate(() => {
this.result.processError(err);
this.emit("done", err);
Expand Down
15 changes: 3 additions & 12 deletions src/lineToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Converter } from "./Converter";
import CSVError from "./CSVError";
import { CellParser, ColumnParam } from "./Parameters";
import set from "lodash/set";
import { ParseRuntime } from "./ParseRuntime";

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

export default function (csvRows: string[][], conv: Converter): JSONResult[] {
const res: JSONResult[] = [];
Expand Down Expand Up @@ -58,10 +57,6 @@ function convertRowToJson(row: string[], headRow: string[], conv: Converter): {
setPath(resultRow, head, convRes, conv,i);
}
} else {
// var flag = getFlag(head, i, param);
// if (flag === 'omit') {
// continue;
// }
if (conv.parseParam.checkType) {
const convertFunc = checkType(item, head, i, conv);
item = convertFunc(item);
Expand Down Expand Up @@ -193,12 +188,8 @@ function dynamicType(item) {
}

function booleanType(item) {
var trimed = item.trim();
if (trimed.length === 5 && trimed.toLowerCase() === "false") {
return false;
} else {
return true;
}
const trimmed = item.trim();
return !(trimmed.length === 5 && trimmed.toLowerCase() === "false");
}

function jsonType(item) {
Expand Down
2 changes: 1 addition & 1 deletion src/rowSplit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class RowSplit {
} else if (this.isQuoteOpen(e)) { //quote open
e = e.substr(1);
if (this.isQuoteClose(e)) { //quote close
e = e.substring(0, e.lastIndexOf(quote));
e = e.substr(0, e.lastIndexOf(quote));
e = this.escapeQuote(e);
row.push(e);
continue;
Expand Down
1 change: 0 additions & 1 deletion v2/lineToJson.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e7999d

Please sign in to comment.