diff --git a/.gitignore b/.gitignore index 2b3deaa..3c91699 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ Thumbs.db node_modules/ *.swp *.swo +.idea/ # intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt diff --git a/bin/csvtojson.js b/bin/csvtojson.js index 1e79547..0bff307 100644 --- a/bin/csvtojson.js +++ b/bin/csvtojson.js @@ -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() { diff --git a/src/Converter.ts b/src/Converter.ts index 744b945..ea866ec 100644 --- a/src/Converter.ts +++ b/src/Converter.ts @@ -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 { preRawData(onRawData: PreRawDataCallback): Converter { @@ -38,12 +29,6 @@ export class Converter extends Transform implements PromiseLike { } 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); @@ -66,7 +51,7 @@ export class Converter extends Transform implements PromiseLike { 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; } @@ -108,14 +93,8 @@ export class Converter extends Transform implements PromiseLike { 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); diff --git a/src/lineToJson.ts b/src/lineToJson.ts index b89ee81..94709d4 100644 --- a/src/lineToJson.ts +++ b/src/lineToJson.ts @@ -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[] = []; @@ -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); @@ -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) { diff --git a/src/rowSplit.ts b/src/rowSplit.ts index b6c6502..711fc3b 100644 --- a/src/rowSplit.ts +++ b/src/rowSplit.ts @@ -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; diff --git a/v2/lineToJson.js b/v2/lineToJson.js index 119f329..cfad7e8 100644 --- a/v2/lineToJson.js +++ b/v2/lineToJson.js @@ -17,7 +17,6 @@ function default_1(csvRows, conv) { return res; } exports.default = default_1; -; function processRow(row, conv, index) { if (conv.parseParam.checkColumn && conv.parseRuntime.headers && row.length !== conv.parseRuntime.headers.length) { throw (CSVError_1.default.column_mismatched(conv.parseRuntime.parsedLineNumber + index));