Comparing version 3.4.0 to 3.5.0
module.exports = { | ||
parser: "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
parserOptions: { | ||
project: "./tsconfig.json" | ||
}, | ||
plugins: ["@typescript-eslint"], | ||
extends: ["airbnb-base", "plugin:@typescript-eslint/recommended"], | ||
plugins: ["@typescript-eslint", "prettier"], | ||
extends: [ | ||
"airbnb-base", | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier", | ||
"prettier/@typescript-eslint" | ||
], | ||
env: { | ||
@@ -13,5 +20,16 @@ node: true, | ||
settings:{ | ||
"import/extensions": [ | ||
".ts" | ||
], | ||
"import/parsers": { | ||
"@typescript-eslint/parser": [ | ||
".ts" | ||
] | ||
}, | ||
"import/resolver": { | ||
node: { | ||
extensions: [".ts"] | ||
}, | ||
"typescript": { | ||
"alwaysTryTypes": true | ||
} | ||
@@ -24,2 +42,10 @@ } | ||
rules: { | ||
"import/extensions": [ | ||
"error", | ||
"ignorePackages", | ||
{ | ||
"ts": "never" | ||
} | ||
], | ||
"prettier/prettier": "error", | ||
"indent": [ | ||
@@ -29,12 +55,4 @@ "error", | ||
], | ||
"comma-dangle": ["error", { | ||
"arrays": "always-multiline", | ||
"objects": "always-multiline", | ||
"imports": "always-multiline", | ||
"exports": "always-multiline", | ||
"functions": "never" | ||
}], | ||
"no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"], | ||
"object-curly-spacing": ["error", "always"], | ||
"array-bracket-spacing": ["error", "always"], | ||
"no-underscore-dangle": 0, | ||
@@ -49,2 +67,22 @@ "max-len": ["error", 150, 2, { | ||
}, | ||
overrides: [ | ||
{ | ||
"files": [ | ||
"*.test.ts" | ||
], | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off" | ||
} | ||
}, | ||
{ | ||
"files": [ | ||
"*.js" | ||
], | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"@typescript-eslint/no-var-requires": "off" | ||
} | ||
} | ||
] | ||
}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const stream_1 = require("stream"); | ||
const RowFormatter_1 = require("./formatter/RowFormatter"); | ||
const RowFormatter_1 = __importDefault(require("./formatter/RowFormatter")); | ||
class CsvFormatterStream extends stream_1.Transform { | ||
@@ -6,0 +9,0 @@ constructor(formatterOptions) { |
@@ -8,3 +8,3 @@ import { FormatterOptions } from '../FormatterOptions'; | ||
constructor(formatterOptions: FormatterOptions); | ||
headers: string[]; | ||
set headers(headers: string[]); | ||
private shouldQuote; | ||
@@ -11,0 +11,0 @@ format(field: string, fieldIndex: number, isHeader: boolean): string; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const lodash_isboolean_1 = __importDefault(require("lodash.isboolean")); | ||
const lodash_isnil_1 = __importDefault(require("lodash.isnil")); | ||
const lodash_escaperegexp_1 = __importDefault(require("lodash.escaperegexp")); | ||
class FieldFormatter { | ||
@@ -12,3 +17,3 @@ constructor(formatterOptions) { | ||
this.REPLACE_REGEXP = new RegExp(formatterOptions.quote, 'g'); | ||
const escapePattern = `[${formatterOptions.delimiter}${lodash_1.escapeRegExp(formatterOptions.rowDelimiter)}']`; | ||
const escapePattern = `[${formatterOptions.delimiter}${lodash_escaperegexp_1.default(formatterOptions.rowDelimiter)}']`; | ||
this.ESCAPE_REGEXP = new RegExp(escapePattern); | ||
@@ -21,3 +26,3 @@ } | ||
const quoteConfig = isHeader ? this.formatterOptions.quoteHeaders : this.formatterOptions.quoteColumns; | ||
if (lodash_1.isBoolean(quoteConfig)) { | ||
if (lodash_isboolean_1.default(quoteConfig)) { | ||
return quoteConfig; | ||
@@ -34,3 +39,3 @@ } | ||
format(field, fieldIndex, isHeader) { | ||
const preparedField = `${lodash_1.isNil(field) ? '' : field}`.replace(/\0/g, ''); | ||
const preparedField = `${lodash_isnil_1.default(field) ? '' : field}`.replace(/\0/g, ''); | ||
const { formatterOptions } = this; | ||
@@ -37,0 +42,0 @@ if (formatterOptions.quote !== '') { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const RowFormatter_1 = require("./RowFormatter"); | ||
const RowFormatter_1 = __importDefault(require("./RowFormatter")); | ||
exports.default = { | ||
@@ -5,0 +8,0 @@ RowFormatter: RowFormatter_1.default, |
@@ -16,3 +16,3 @@ import { FormatterOptions } from '../FormatterOptions'; | ||
constructor(formatterOptions: FormatterOptions); | ||
rowTransform: RowTransformFunction; | ||
set rowTransform(transformFunction: RowTransformFunction); | ||
format(row: Row, cb: RowFormatterCallback): void; | ||
@@ -19,0 +19,0 @@ private checkHeaders; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const FieldFormatter_1 = require("./FieldFormatter"); | ||
const lodash_isfunction_1 = __importDefault(require("lodash.isfunction")); | ||
const lodash_isequal_1 = __importDefault(require("lodash.isequal")); | ||
const FieldFormatter_1 = __importDefault(require("./FieldFormatter")); | ||
class RowFormatter { | ||
@@ -57,3 +61,3 @@ constructor(formatterOptions) { | ||
set rowTransform(transformFunction) { | ||
if (!lodash_1.isFunction(transformFunction)) { | ||
if (!lodash_isfunction_1.default(transformFunction)) { | ||
throw new TypeError('The transform should be a function'); | ||
@@ -102,3 +106,3 @@ } | ||
// if the row is equal to headers dont format | ||
return { shouldFormatColumns: !lodash_1.isEqual(headers, row), headers }; | ||
return { shouldFormatColumns: !lodash_isequal_1.default(headers, row), headers }; | ||
} | ||
@@ -105,0 +109,0 @@ gatherColumns(row) { |
@@ -5,2 +5,3 @@ "use strict"; | ||
constructor(opts = {}) { | ||
var _a, _b, _c, _d; | ||
this.objectMode = true; | ||
@@ -18,17 +19,15 @@ this.delimiter = ','; | ||
this.BOM = '\ufeff'; | ||
if (opts) { | ||
Object.assign(this, opts); | ||
if (typeof opts.quoteHeaders === 'undefined') { | ||
this.quoteHeaders = this.quoteColumns; | ||
} | ||
if (opts.quote === true) { | ||
this.quote = '"'; | ||
} | ||
else if (opts.quote === false) { | ||
this.quote = ''; | ||
} | ||
if (typeof opts.escape !== 'string') { | ||
this.escape = this.quote; | ||
} | ||
Object.assign(this, opts || {}); | ||
if (typeof ((_a = opts) === null || _a === void 0 ? void 0 : _a.quoteHeaders) === 'undefined') { | ||
this.quoteHeaders = this.quoteColumns; | ||
} | ||
if (((_b = opts) === null || _b === void 0 ? void 0 : _b.quote) === true) { | ||
this.quote = '"'; | ||
} | ||
else if (((_c = opts) === null || _c === void 0 ? void 0 : _c.quote) === false) { | ||
this.quote = ''; | ||
} | ||
if (typeof ((_d = opts) === null || _d === void 0 ? void 0 : _d.escape) !== 'string') { | ||
this.escape = this.quote; | ||
} | ||
this.shouldWriteHeaders = !!this.headers; | ||
@@ -35,0 +34,0 @@ this.headers = Array.isArray(this.headers) ? this.headers : null; |
@@ -5,8 +5,18 @@ "use strict"; | ||
} | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const util_1 = require("util"); | ||
const stream_1 = require("stream"); | ||
const fs = require("fs"); | ||
const fs = __importStar(require("fs")); | ||
const FormatterOptions_1 = require("./FormatterOptions"); | ||
const CsvFormatterStream_1 = require("./CsvFormatterStream"); | ||
const CsvFormatterStream_1 = __importDefault(require("./CsvFormatterStream")); | ||
var CsvFormatterStream_2 = require("./CsvFormatterStream"); | ||
@@ -28,4 +38,3 @@ exports.CsvFormatterStream = CsvFormatterStream_2.default; | ||
}; | ||
exports.writeToStream = (ws, rows, options) => exports.write(rows, options) | ||
.pipe(ws); | ||
exports.writeToStream = (ws, rows, options) => exports.write(rows, options).pipe(ws); | ||
exports.writeToBuffer = (rows, opts = {}) => { | ||
@@ -40,10 +49,7 @@ const buffers = []; | ||
return new Promise((res, rej) => { | ||
ws | ||
.on('error', rej) | ||
.on('finish', () => res(Buffer.concat(buffers))); | ||
ws.on('error', rej).on('finish', () => res(Buffer.concat(buffers))); | ||
exports.write(rows, opts).pipe(ws); | ||
}); | ||
}; | ||
exports.writeToString = (rows, options) => exports.writeToBuffer(rows, options) | ||
.then((buffer) => buffer.toString()); | ||
exports.writeToString = (rows, options) => exports.writeToBuffer(rows, options).then((buffer) => buffer.toString()); | ||
exports.writeToPath = (path, rows, options) => { | ||
@@ -50,0 +56,0 @@ const stream = fs.createWriteStream(path, { encoding: 'utf8' }); |
@@ -8,17 +8,6 @@ /** | ||
/// <reference types="node" /> | ||
declare const csv: { | ||
parse: (args?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
parseString: (string: string, options?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
fromString: (string: string, options?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
parseStream: (stream: NodeJS.ReadableStream, options?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
fromStream: (stream: NodeJS.ReadableStream, options?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
parseFile: (location: string, options?: import("./parser").ParserOptionsArgs) => import("./parser").CsvParserStream; | ||
fromPath: (location: string, options?: import("./parser").ParserOptionsArgs) => import("./parser").CsvParserStream; | ||
format: (options?: import("./formatter").FormatterOptionsArgs | undefined) => import("./formatter").CsvFormatterStream; | ||
write: (rows: import("./formatter").Row[], options?: import("./formatter").FormatterOptionsArgs | undefined) => import("./formatter").CsvFormatterStream; | ||
writeToStream: <T extends NodeJS.WritableStream>(ws: T, rows: import("./formatter").Row[], options?: import("./formatter").FormatterOptionsArgs | undefined) => T; | ||
writeToBuffer: (rows: import("./formatter").Row[], opts?: import("./formatter").FormatterOptionsArgs) => Promise<Buffer>; | ||
writeToString: (rows: import("./formatter").Row[], options?: import("./formatter").FormatterOptionsArgs | undefined) => Promise<string>; | ||
writeToPath: (path: string, rows: import("./formatter").Row[], options?: import("./formatter").FormatterOptionsArgs | undefined) => import("fs").WriteStream; | ||
}; | ||
export = csv; | ||
export { format, write, writeToStream, writeToBuffer, writeToString, writeToPath } from './formatter'; | ||
export { parse, parseString, parseStream, parseFile } from './parser'; | ||
export declare const fromString: (string: string, options?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
export declare const fromStream: (stream: NodeJS.ReadableStream, options?: import("./parser").ParserOptionsArgs | undefined) => import("./parser").CsvParserStream; | ||
export declare const fromPath: (location: string, options?: import("./parser").ParserOptionsArgs) => import("./parser").CsvParserStream; |
@@ -8,21 +8,20 @@ "use strict"; | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const util_1 = require("util"); | ||
const formatter_1 = require("./formatter"); | ||
const parser_1 = require("./parser"); | ||
const csv = { | ||
parse: parser_1.parse, | ||
parseString: parser_1.parseString, | ||
fromString: util_1.deprecate(parser_1.parseString, 'csv.fromString has been deprecated in favor of csv.parseString'), | ||
parseStream: parser_1.parseStream, | ||
fromStream: util_1.deprecate(parser_1.parseStream, 'csv.fromStream has been deprecated in favor of csv.parseStream'), | ||
parseFile: parser_1.parseFile, | ||
fromPath: util_1.deprecate(parser_1.parseFile, 'csv.fromPath has been deprecated in favor of csv.parseFile'), | ||
format: formatter_1.format, | ||
write: formatter_1.write, | ||
writeToStream: formatter_1.writeToStream, | ||
writeToBuffer: formatter_1.writeToBuffer, | ||
writeToString: formatter_1.writeToString, | ||
writeToPath: formatter_1.writeToPath, | ||
}; | ||
module.exports = csv; | ||
var formatter_1 = require("./formatter"); | ||
exports.format = formatter_1.format; | ||
exports.write = formatter_1.write; | ||
exports.writeToStream = formatter_1.writeToStream; | ||
exports.writeToBuffer = formatter_1.writeToBuffer; | ||
exports.writeToString = formatter_1.writeToString; | ||
exports.writeToPath = formatter_1.writeToPath; | ||
var parser_2 = require("./parser"); | ||
exports.parse = parser_2.parse; | ||
exports.parseString = parser_2.parseString; | ||
exports.parseStream = parser_2.parseStream; | ||
exports.parseFile = parser_2.parseFile; | ||
exports.fromString = util_1.deprecate(parser_1.parseString, 'csv.fromString has been deprecated in favor of csv.parseString'); | ||
exports.fromStream = util_1.deprecate(parser_1.parseStream, 'csv.fromStream has been deprecated in favor of csv.parseStream'); | ||
exports.fromPath = util_1.deprecate(parser_1.parseFile, 'csv.fromPath has been deprecated in favor of csv.parseFile'); | ||
//# sourceMappingURL=index.js.map |
@@ -41,3 +41,3 @@ "use strict"; | ||
const { lines } = this; | ||
const newLine = (lines + this.decoder.write(data)); | ||
const newLine = lines + this.decoder.write(data); | ||
const rows = this.parse(newLine, true); | ||
@@ -52,3 +52,3 @@ this.processRows(rows, done); | ||
try { | ||
const newLine = (this.lines + this.decoder.end()); | ||
const newLine = this.lines + this.decoder.end(); | ||
const rows = this.parse(newLine, false); | ||
@@ -98,3 +98,3 @@ this.processRows(rows, done); | ||
} | ||
if ((i % 100) === 0) { | ||
if (i % 100 === 0) { | ||
// incase the transform are sync insert a next tick to prevent stack overflow | ||
@@ -101,0 +101,0 @@ setImmediate(() => iterate(i + 1)); |
@@ -5,7 +5,17 @@ "use strict"; | ||
} | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const fs = __importStar(require("fs")); | ||
const stream_1 = require("stream"); | ||
const ParserOptions_1 = require("./ParserOptions"); | ||
const CsvParserStream_1 = require("./CsvParserStream"); | ||
const CsvParserStream_1 = __importDefault(require("./CsvParserStream")); | ||
var CsvParserStream_2 = require("./CsvParserStream"); | ||
@@ -16,7 +26,4 @@ exports.CsvParserStream = CsvParserStream_2.default; | ||
exports.parse = (args) => new CsvParserStream_1.default(new ParserOptions_1.ParserOptions(args)); | ||
exports.parseStream = (stream, options) => stream | ||
.pipe(new CsvParserStream_1.default(new ParserOptions_1.ParserOptions(options))); | ||
exports.parseFile = (location, options = {}) => fs | ||
.createReadStream(location) | ||
.pipe(new CsvParserStream_1.default(new ParserOptions_1.ParserOptions(options))); | ||
exports.parseStream = (stream, options) => stream.pipe(new CsvParserStream_1.default(new ParserOptions_1.ParserOptions(options))); | ||
exports.parseFile = (location, options = {}) => fs.createReadStream(location).pipe(new CsvParserStream_1.default(new ParserOptions_1.ParserOptions(options))); | ||
exports.parseString = (string, options) => { | ||
@@ -23,0 +30,0 @@ const rs = new stream_1.Readable(); |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const NonQuotedColumnParser_1 = require("./NonQuotedColumnParser"); | ||
const QuotedColumnParser_1 = require("./QuotedColumnParser"); | ||
const Scanner_1 = require("../Scanner"); | ||
const NonQuotedColumnParser_1 = __importDefault(require("./NonQuotedColumnParser")); | ||
const QuotedColumnParser_1 = __importDefault(require("./QuotedColumnParser")); | ||
const Token_1 = require("../Token"); | ||
class ColumnParser { | ||
@@ -14,3 +17,3 @@ constructor(parserOptions) { | ||
const { nextNonSpaceToken } = scanner; | ||
if (nextNonSpaceToken !== null && Scanner_1.Token.isTokenQuote(nextNonSpaceToken, this.parserOptions)) { | ||
if (nextNonSpaceToken !== null && Token_1.Token.isTokenQuote(nextNonSpaceToken, this.parserOptions)) { | ||
scanner.advanceToToken(nextNonSpaceToken); | ||
@@ -17,0 +20,0 @@ return this.quotedColumnParser.parse(scanner); |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ColumnFormatter_1 = require("./ColumnFormatter"); | ||
const Scanner_1 = require("../Scanner"); | ||
const { isTokenDelimiter, isTokenRowDelimiter } = Scanner_1.Token; | ||
const ColumnFormatter_1 = __importDefault(require("./ColumnFormatter")); | ||
const Token_1 = require("../Token"); | ||
const { isTokenDelimiter, isTokenRowDelimiter } = Token_1.Token; | ||
class NonQuotedColumnParser { | ||
@@ -7,0 +10,0 @@ constructor(parserOptions) { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ColumnFormatter_1 = require("./ColumnFormatter"); | ||
const Scanner_1 = require("../Scanner"); | ||
const { isTokenDelimiter, isTokenRowDelimiter, isTokenEscapeCharacter, isTokenQuote, } = Scanner_1.Token; | ||
const ColumnFormatter_1 = __importDefault(require("./ColumnFormatter")); | ||
const Token_1 = require("../Token"); | ||
const { isTokenDelimiter, isTokenRowDelimiter, isTokenEscapeCharacter, isTokenQuote } = Token_1.Token; | ||
class QuotedColumnParser { | ||
@@ -7,0 +10,0 @@ constructor(parserOptions) { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Scanner_1 = require("./Scanner"); | ||
const RowParser_1 = require("./RowParser"); | ||
const RowParser_1 = __importDefault(require("./RowParser")); | ||
const Token_1 = require("./Token"); | ||
const EMPTY_ROW_REGEXP = /^\s*(?:''|"")?\s*(?:,\s*(?:''|"")?\s*)*$/; | ||
class Parser { | ||
constructor(parserOptions) { | ||
this.parserOptions = parserOptions; | ||
this.rowParser = new RowParser_1.default(this.parserOptions); | ||
} | ||
static removeBOM(line) { | ||
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string | ||
// conversion translates it to FEFF (UTF-16 BOM) | ||
if (line && line.charCodeAt(0) === 0xFEFF) { | ||
if (line && line.charCodeAt(0) === 0xfeff) { | ||
return line.slice(1); | ||
@@ -15,6 +23,2 @@ } | ||
} | ||
constructor(parserOptions) { | ||
this.parserOptions = parserOptions; | ||
this.rowParser = new RowParser_1.default(this.parserOptions); | ||
} | ||
parse(line, hasMoreData) { | ||
@@ -43,3 +47,3 @@ const scanner = new Scanner_1.Scanner({ | ||
for (let nextToken = scanner.nextCharacterToken; nextToken !== null; nextToken = scanner.nextCharacterToken) { | ||
if (Scanner_1.Token.isTokenComment(nextToken, parserOptions)) { | ||
if (Token_1.Token.isTokenComment(nextToken, parserOptions)) { | ||
const cursor = scanner.advancePastLine(); | ||
@@ -46,0 +50,0 @@ if (cursor === null) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Scanner_1 = require("./Scanner"); | ||
const column_1 = require("./column"); | ||
const { isTokenRowDelimiter, isTokenCarriageReturn, isTokenDelimiter } = Scanner_1.Token; | ||
const Token_1 = require("./Token"); | ||
const { isTokenRowDelimiter, isTokenCarriageReturn, isTokenDelimiter } = Token_1.Token; | ||
class RowParser { | ||
@@ -22,5 +22,5 @@ constructor(parserOptions) { | ||
// coming LF in CRLF | ||
if (!currentScanner.hasMoreCharacters | ||
&& isTokenCarriageReturn(currentToken, parserOptions) | ||
&& hasMoreData) { | ||
if (!currentScanner.hasMoreCharacters && | ||
isTokenCarriageReturn(currentToken, parserOptions) && | ||
hasMoreData) { | ||
return null; | ||
@@ -27,0 +27,0 @@ } |
import { ParserOptions } from '../ParserOptions'; | ||
interface TokenArgs { | ||
token: string; | ||
startCursor: number; | ||
endCursor: number; | ||
} | ||
export declare type MaybeToken = Token | null; | ||
export declare class Token { | ||
static isTokenRowDelimiter(token: Token): boolean; | ||
static isTokenCarriageReturn(token: Token, parserOptions: ParserOptions): boolean; | ||
static isTokenComment(token: Token, parserOptions: ParserOptions): boolean; | ||
static isTokenEscapeCharacter(token: Token, parserOptions: ParserOptions): boolean; | ||
static isTokenQuote(token: Token, parserOptions: ParserOptions): boolean; | ||
static isTokenDelimiter(token: Token, parserOptions: ParserOptions): boolean; | ||
readonly token: string; | ||
readonly startCursor: number; | ||
readonly endCursor: number; | ||
constructor(tokenArgs: TokenArgs); | ||
} | ||
interface ScannerArgs { | ||
import { MaybeToken, Token } from './Token'; | ||
export interface ScannerArgs { | ||
line: string; | ||
@@ -33,6 +16,6 @@ parserOptions: ParserOptions; | ||
constructor(args: ScannerArgs); | ||
readonly hasMoreCharacters: boolean; | ||
readonly nextNonSpaceToken: MaybeToken; | ||
readonly nextCharacterToken: MaybeToken; | ||
readonly lineFromCursor: string; | ||
get hasMoreCharacters(): boolean; | ||
get nextNonSpaceToken(): MaybeToken; | ||
get nextCharacterToken(): MaybeToken; | ||
get lineFromCursor(): string; | ||
advancePastLine(): Scanner | null; | ||
@@ -44,2 +27,1 @@ advanceTo(cursor: number): Scanner; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Token_1 = require("./Token"); | ||
const ROW_DELIMITER = /((?:\r\n)|\n|\r)/; | ||
class Token { | ||
static isTokenRowDelimiter(token) { | ||
const content = token.token; | ||
return content === '\r' || content === '\n' || content === '\r\n'; | ||
} | ||
static isTokenCarriageReturn(token, parserOptions) { | ||
return token.token === parserOptions.carriageReturn; | ||
} | ||
static isTokenComment(token, parserOptions) { | ||
return parserOptions.supportsComments && !!token && token.token === parserOptions.comment; | ||
} | ||
static isTokenEscapeCharacter(token, parserOptions) { | ||
return token.token === parserOptions.escapeChar; | ||
} | ||
static isTokenQuote(token, parserOptions) { | ||
return token.token === parserOptions.quote; | ||
} | ||
static isTokenDelimiter(token, parserOptions) { | ||
return token.token === parserOptions.delimiter; | ||
} | ||
constructor(tokenArgs) { | ||
this.token = tokenArgs.token; | ||
this.startCursor = tokenArgs.startCursor; | ||
this.endCursor = tokenArgs.endCursor; | ||
} | ||
} | ||
exports.Token = Token; | ||
class Scanner { | ||
@@ -55,3 +29,3 @@ constructor(args) { | ||
const startCursor = this.cursor + (match.index || 0); | ||
return new Token({ | ||
return new Token_1.Token({ | ||
token, | ||
@@ -67,3 +41,3 @@ startCursor, | ||
} | ||
return new Token({ | ||
return new Token_1.Token({ | ||
token: this.line[cursor], | ||
@@ -70,0 +44,0 @@ startCursor: cursor, |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const lodash_escaperegexp_1 = __importDefault(require("lodash.escaperegexp")); | ||
const lodash_isnil_1 = __importDefault(require("lodash.isnil")); | ||
class ParserOptions { | ||
constructor(opts) { | ||
var _a; | ||
this.objectMode = true; | ||
@@ -27,5 +32,5 @@ this.delimiter = ','; | ||
} | ||
this.escapedDelimiter = lodash_1.escapeRegExp(this.delimiter); | ||
this.escapeChar = lodash_1.isString(this.escape) ? this.escape : this.quote; | ||
this.supportsComments = !lodash_1.isNil(this.comment); | ||
this.escapedDelimiter = lodash_escaperegexp_1.default(this.delimiter); | ||
this.escapeChar = (_a = this.escape, (_a !== null && _a !== void 0 ? _a : this.quote)); | ||
this.supportsComments = !lodash_isnil_1.default(this.comment); | ||
this.NEXT_TOKEN_REGEXP = new RegExp(`([^\\s]|\\r\\n|\\n|\\r|${this.escapedDelimiter})`); | ||
@@ -32,0 +37,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const lodash_isundefined_1 = __importDefault(require("lodash.isundefined")); | ||
class HeaderTransformer { | ||
@@ -48,5 +51,9 @@ constructor(parserOptions) { | ||
} | ||
return { row, isValid: false, reason: `Column header mismatch expected: ${this.headersLength} columns got: ${row.length}` }; | ||
return { | ||
row, | ||
isValid: false, | ||
reason: `Column header mismatch expected: ${this.headersLength} columns got: ${row.length}`, | ||
}; | ||
} | ||
if (parserOptions.strictColumnHandling && (row.length < this.headersLength)) { | ||
if (parserOptions.strictColumnHandling && row.length < this.headersLength) { | ||
return { | ||
@@ -65,6 +72,6 @@ row, | ||
const header = headers[i]; | ||
if (!lodash_1.isUndefined(header)) { | ||
if (!lodash_isundefined_1.default(header)) { | ||
const val = row[i]; | ||
// eslint-disable-next-line no-param-reassign | ||
if (lodash_1.isUndefined(val)) { | ||
if (lodash_isundefined_1.default(val)) { | ||
rowMap[header] = ''; | ||
@@ -71,0 +78,0 @@ } |
@@ -7,4 +7,4 @@ import { Row, RowTransformFunction, RowValidatorCallback, RowValidate } from '../types'; | ||
private _rowValidator; | ||
rowTransform: RowTransformFunction; | ||
rowValidator: RowValidate; | ||
set rowTransform(transformFunction: RowTransformFunction); | ||
set rowValidator(validateFunction: RowValidate); | ||
transformAndValidate(row: Row, cb: RowValidatorCallback): void; | ||
@@ -11,0 +11,0 @@ private callTransformer; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const lodash_isfunction_1 = __importDefault(require("lodash.isfunction")); | ||
const types_1 = require("../types"); | ||
@@ -44,3 +47,3 @@ class RowTransformerValidator { | ||
set rowTransform(transformFunction) { | ||
if (!lodash_1.isFunction(transformFunction)) { | ||
if (!lodash_isfunction_1.default(transformFunction)) { | ||
throw new TypeError('The transform should be a function'); | ||
@@ -51,3 +54,3 @@ } | ||
set rowValidator(validateFunction) { | ||
if (!lodash_1.isFunction(validateFunction)) { | ||
if (!lodash_isfunction_1.default(validateFunction)) { | ||
throw new TypeError('The validate should be a function'); | ||
@@ -54,0 +57,0 @@ } |
@@ -42,3 +42,2 @@ # Parsing | ||
* **NOTE** this is only valid in the case that there are headers and the number of fields parsed is greater than the number of header fields. | ||
* `comment: | ||
* `strictColumnHandling: {boolean} = false`: If you want to consider empty lines/lines with too few fields as invalid and emit a `data-invalid` event | ||
@@ -159,3 +158,3 @@ * **NOTE** This option is only considered when `headers` are present. | ||
csv | ||
.fromString(CSV_STRING, { headers: true }) | ||
.parseString(CSV_STRING, { headers: true }) | ||
.on('error', error => console.error(error)) | ||
@@ -162,0 +161,0 @@ .on('data', row => console.log(row)) |
@@ -0,1 +1,7 @@ | ||
# v3.5.0 | ||
* Upgraded dependencies | ||
* Reformatted code with prettier | ||
* [FIXED] Entire lodash is imported, bloating dependencies [#281](https://github.com/C2FO/fast-csv/issues/281) | ||
# v3.4.0 | ||
@@ -2,0 +8,0 @@ |
{ | ||
"name": "fast-csv", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "CSV parser and writer", | ||
@@ -11,5 +11,5 @@ "main": "./build/src/index.js", | ||
"mocha": "nyc mocha", | ||
"test": "npm run eslint && npm run mocha", | ||
"eslint": "eslint --ext=.js,.ts src/ test/ examples/", | ||
"eslint-fix": "eslint --ext=.js,.ts src/ test/ examples/", | ||
"test": "npm run lint && npm run mocha", | ||
"lint": "eslint --ext=.js,.ts src/ test/ examples/", | ||
"lint-fix": "eslint --fix --ext=.js,.ts src/ test/ examples/", | ||
"benchmark": "node ./benchmark", | ||
@@ -35,17 +35,29 @@ "coverage": "nyc report --reporter=text-lcov | coveralls" | ||
"@istanbuljs/nyc-config-typescript": "^0.1.3", | ||
"@types/mocha": "^5.2.6", | ||
"@types/sinon": "^7.0.12", | ||
"@typescript-eslint/eslint-plugin": "^1.9.0", | ||
"@typescript-eslint/parser": "^1.9.0", | ||
"coveralls": "^3.0.3", | ||
"eslint": "^5.15.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"husky": "^2.3.0", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.0", | ||
"sinon": "^7.3.2", | ||
"source-map-support": "^0.5.12", | ||
"ts-node": "^8.2.0", | ||
"typescript": "^3.4.5" | ||
"@types/lodash.escaperegexp": "^4.1.6", | ||
"@types/lodash.isboolean": "^3.0.6", | ||
"@types/lodash.isequal": "^4.5.5", | ||
"@types/lodash.isfunction": "^3.0.6", | ||
"@types/lodash.isnil": "^4.0.6", | ||
"@types/lodash.isstring": "^4.0.6", | ||
"@types/lodash.isundefined": "^3.0.6", | ||
"@types/lodash.partition": "^4.6.6", | ||
"@types/mocha": "^5.2.7", | ||
"@types/sinon": "^7.5.1", | ||
"@typescript-eslint/eslint-plugin": "^2.11.0", | ||
"@typescript-eslint/parser": "^2.11.0", | ||
"coveralls": "^3.0.9", | ||
"eslint": "^6.7.2", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-config-prettier": "^6.7.0", | ||
"eslint-plugin-import": "^2.19.1", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"husky": "^3.1.0", | ||
"lodash.partition": "^4.6.0", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.19.1", | ||
"sinon": "^7.5.0", | ||
"source-map-support": "^0.5.16", | ||
"ts-node": "^8.5.4", | ||
"typescript": "^3.7.3" | ||
}, | ||
@@ -56,6 +68,11 @@ "engines": { | ||
"dependencies": { | ||
"@types/lodash": "^4.14.132", | ||
"@types/node": "^12.0.2", | ||
"lodash": "^4.17.13" | ||
"@types/node": "^12.12.17", | ||
"lodash.escaperegexp": "^4.1.2", | ||
"lodash.isboolean": "^3.0.3", | ||
"lodash.isequal": "^4.5.0", | ||
"lodash.isfunction": "^3.0.9", | ||
"lodash.isnil": "^4.0.0", | ||
"lodash.isstring": "^4.0.1", | ||
"lodash.isundefined": "^3.0.1" | ||
} | ||
} |
@@ -11,3 +11,3 @@ import { Transform, TransformCallback } from 'stream'; | ||
private hasWrittenBOM: boolean = false; | ||
private hasWrittenBOM = false; | ||
@@ -14,0 +14,0 @@ public constructor(formatterOptions: FormatterOptions) { |
@@ -1,5 +0,6 @@ | ||
import { isBoolean, isNil, escapeRegExp } from 'lodash'; | ||
import isBoolean from 'lodash.isboolean'; | ||
import isNil from 'lodash.isnil'; | ||
import escapeRegExp from 'lodash.escaperegexp'; | ||
import { FormatterOptions } from '../FormatterOptions'; | ||
export default class FieldFormatter { | ||
@@ -48,5 +49,3 @@ private readonly formatterOptions: FormatterOptions; | ||
if (shouldEscape) { | ||
return this.quoteField( | ||
preparedField.replace(this.REPLACE_REGEXP, formatterOptions.escapedQuote) | ||
); | ||
return this.quoteField(preparedField.replace(this.REPLACE_REGEXP, formatterOptions.escapedQuote)); | ||
} | ||
@@ -53,0 +52,0 @@ } |
@@ -1,11 +0,10 @@ | ||
import { isFunction, isEqual } from 'lodash'; | ||
import isFunction from 'lodash.isfunction'; | ||
import isEqual from 'lodash.isequal'; | ||
import { FormatterOptions } from '../FormatterOptions'; | ||
import FieldFormatter from './FieldFormatter'; | ||
import { | ||
Row, RowHashArray, RowTransformFunction, | ||
} from '../types'; | ||
import { Row, RowHashArray, RowTransformFunction } from '../types'; | ||
type RowCallback = ((err?: Error | null, row?: Row) => void) | ||
type RowCallback = (err?: Error | null, row?: Row) => void; | ||
type RowFormatterTransform = (row: Row, cb: RowCallback) => void | ||
type RowFormatterTransform = (row: Row, cb: RowCallback) => void; | ||
@@ -64,5 +63,4 @@ type RowFormatterCallback = (error: Error | null, data?: string[]) => void; | ||
private rowCount: number = 0; | ||
private rowCount = 0; | ||
public constructor(formatterOptions: FormatterOptions) { | ||
@@ -168,3 +166,3 @@ this.formatterOptions = formatterOptions; | ||
if (rowCount) { | ||
return [ this.formatterOptions.rowDelimiter, formattedCols ].join(''); | ||
return [this.formatterOptions.rowDelimiter, formattedCols].join(''); | ||
} | ||
@@ -171,0 +169,0 @@ return formattedCols; |
import { RowTransformFunction } from './types'; | ||
interface QuoteColumnMap{ | ||
interface QuoteColumnMap { | ||
[s: string]: boolean; | ||
} | ||
type QuoteColumns = boolean | boolean[] | QuoteColumnMap | ||
type QuoteColumns = boolean | boolean[] | QuoteColumnMap; | ||
@@ -53,16 +53,15 @@ export interface FormatterOptionsArgs { | ||
public constructor(opts: FormatterOptionsArgs = {}) { | ||
if (opts) { | ||
Object.assign(this, opts); | ||
if (typeof opts.quoteHeaders === 'undefined') { | ||
this.quoteHeaders = this.quoteColumns; | ||
} | ||
if (opts.quote === true) { | ||
this.quote = '"'; | ||
} else if (opts.quote === false) { | ||
this.quote = ''; | ||
} | ||
if (typeof opts.escape !== 'string') { | ||
this.escape = this.quote; | ||
} | ||
Object.assign(this, opts || {}); | ||
if (typeof opts?.quoteHeaders === 'undefined') { | ||
this.quoteHeaders = this.quoteColumns; | ||
} | ||
if (opts?.quote === true) { | ||
this.quote = '"'; | ||
} else if (opts?.quote === false) { | ||
this.quote = ''; | ||
} | ||
if (typeof opts?.escape !== 'string') { | ||
this.escape = this.quote; | ||
} | ||
this.shouldWriteHeaders = !!this.headers; | ||
@@ -69,0 +68,0 @@ this.headers = Array.isArray(this.headers) ? this.headers : null; |
@@ -12,3 +12,4 @@ import { promisify } from 'util'; | ||
export const format = (options?: FormatterOptionsArgs): CsvFormatterStream => new CsvFormatterStream(new FormatterOptions(options)); | ||
export const format = (options?: FormatterOptionsArgs): CsvFormatterStream => | ||
new CsvFormatterStream(new FormatterOptions(options)); | ||
@@ -22,3 +23,3 @@ export const write = (rows: FormatterRow[], options?: FormatterOptionsArgs): CsvFormatterStream => { | ||
(prev: Promise<void>, row: FormatterRow): Promise<void> => prev.then((): Promise<void> => promiseWrite(row)), | ||
Promise.resolve() | ||
Promise.resolve(), | ||
) | ||
@@ -32,4 +33,7 @@ .then((): void => csvStream.end()) | ||
export const writeToStream = <T extends NodeJS.WritableStream>(ws: T, rows: FormatterRow[], options?: FormatterOptionsArgs): T => write(rows, options) | ||
.pipe(ws); | ||
export const writeToStream = <T extends NodeJS.WritableStream>( | ||
ws: T, | ||
rows: FormatterRow[], | ||
options?: FormatterOptionsArgs, | ||
): T => write(rows, options).pipe(ws); | ||
@@ -45,5 +49,3 @@ export const writeToBuffer = (rows: FormatterRow[], opts: FormatterOptionsArgs = {}): Promise<Buffer> => { | ||
return new Promise((res, rej): void => { | ||
ws | ||
.on('error', rej) | ||
.on('finish', (): void => res(Buffer.concat(buffers))); | ||
ws.on('error', rej).on('finish', (): void => res(Buffer.concat(buffers))); | ||
write(rows, opts).pipe(ws); | ||
@@ -53,6 +55,5 @@ }); | ||
export const writeToString = (rows: FormatterRow[], options?: FormatterOptionsArgs): Promise<string> => | ||
writeToBuffer(rows, options).then((buffer): string => buffer.toString()); | ||
export const writeToString = (rows: FormatterRow[], options?: FormatterOptionsArgs): Promise<string> => writeToBuffer(rows, options) | ||
.then((buffer): string => buffer.toString()); | ||
export const writeToPath = (path: string, rows: FormatterRow[], options?: FormatterOptionsArgs): fs.WriteStream => { | ||
@@ -59,0 +60,0 @@ const stream = fs.createWriteStream(path, { encoding: 'utf8' }); |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export interface RowMap{ | ||
export interface RowMap { | ||
[key: string]: any; | ||
} | ||
export type RowHashArray = [string, any][] | ||
export type RowArray = string[] | ||
export type Row = RowArray | RowMap | RowHashArray | ||
export type RowHashArray = [string, any][]; | ||
export type RowArray = string[]; | ||
export type Row = RowArray | RowMap | RowHashArray; | ||
export type RowTransformCallback = (error?: Error | null, row?: Row) => void | ||
export type RowTransformCallback = (error?: Error | null, row?: Row) => void; | ||
export interface RowTransformFunction { | ||
@@ -13,0 +13,0 @@ (row: Row, callback: RowTransformCallback): void; |
@@ -9,26 +9,9 @@ /** | ||
import { deprecate } from 'util'; | ||
import { | ||
format, write, writeToStream, writeToBuffer, writeToString, writeToPath, | ||
} from './formatter'; | ||
import { | ||
parse, parseString, parseStream, parseFile, | ||
} from './parser'; | ||
import { parseStream, parseString, parseFile } from './parser'; | ||
export { format, write, writeToStream, writeToBuffer, writeToString, writeToPath } from './formatter'; | ||
export { parse, parseString, parseStream, parseFile } from './parser'; | ||
const csv = { | ||
parse, | ||
parseString, | ||
fromString: deprecate(parseString, 'csv.fromString has been deprecated in favor of csv.parseString'), | ||
parseStream, | ||
fromStream: deprecate(parseStream, 'csv.fromStream has been deprecated in favor of csv.parseStream'), | ||
parseFile, | ||
fromPath: deprecate(parseFile, 'csv.fromPath has been deprecated in favor of csv.parseFile'), | ||
format, | ||
write, | ||
writeToStream, | ||
writeToBuffer, | ||
writeToString, | ||
writeToPath, | ||
}; | ||
export = csv; | ||
export const fromString = deprecate(parseString, 'csv.fromString has been deprecated in favor of csv.parseString'); | ||
export const fromStream = deprecate(parseStream, 'csv.fromStream has been deprecated in favor of csv.parseStream'); | ||
export const fromPath = deprecate(parseFile, 'csv.fromPath has been deprecated in favor of csv.parseFile'); |
@@ -1,2 +0,2 @@ | ||
import { NodeStringDecoder, StringDecoder } from 'string_decoder'; | ||
import { StringDecoder } from 'string_decoder'; | ||
import { Transform, TransformCallback } from 'stream'; | ||
@@ -6,6 +6,3 @@ import { ParserOptions } from './ParserOptions'; | ||
import { Parser } from './parser'; | ||
import { | ||
RowArray, | ||
RowTransformFunction, RowValidate, RowValidatorCallback, | ||
} from './types'; | ||
import { RowArray, RowTransformFunction, RowValidate, RowValidatorCallback } from './types'; | ||
@@ -15,3 +12,3 @@ export default class CsvParserStream extends Transform { | ||
private readonly decoder: NodeStringDecoder; | ||
private readonly decoder: StringDecoder; | ||
@@ -24,9 +21,8 @@ private readonly parser: Parser; | ||
private lines: string = ''; | ||
private lines = ''; | ||
private rowCount: number = 0; | ||
private rowCount = 0; | ||
private endEmitted: boolean = false; | ||
private endEmitted = false; | ||
public constructor(parserOptions: ParserOptions) { | ||
@@ -66,3 +62,3 @@ super({ objectMode: parserOptions.objectMode }); | ||
const { lines } = this; | ||
const newLine = (lines + this.decoder.write(data)); | ||
const newLine = lines + this.decoder.write(data); | ||
const rows = this.parse(newLine, true); | ||
@@ -75,6 +71,5 @@ this.processRows(rows, done); | ||
public _flush(done: TransformCallback): void { | ||
try { | ||
const newLine = (this.lines + this.decoder.end()); | ||
const newLine = this.lines + this.decoder.end(); | ||
const rows = this.parse(newLine, false); | ||
@@ -122,3 +117,3 @@ this.processRows(rows, done); | ||
} | ||
if ((i % 100) === 0) { | ||
if (i % 100 === 0) { | ||
// incase the transform are sync insert a next tick to prevent stack overflow | ||
@@ -125,0 +120,0 @@ setImmediate((): void => iterate(i + 1)); |
@@ -12,8 +12,7 @@ import * as fs from 'fs'; | ||
export const parseStream = (stream: NodeJS.ReadableStream, options?: ParserOptionsArgs): CsvParserStream => stream | ||
.pipe(new CsvParserStream(new ParserOptions(options))); | ||
export const parseStream = (stream: NodeJS.ReadableStream, options?: ParserOptionsArgs): CsvParserStream => | ||
stream.pipe(new CsvParserStream(new ParserOptions(options))); | ||
export const parseFile = (location: string, options: ParserOptionsArgs = {}): CsvParserStream => fs | ||
.createReadStream(location) | ||
.pipe(new CsvParserStream(new ParserOptions(options))); | ||
export const parseFile = (location: string, options: ParserOptionsArgs = {}): CsvParserStream => | ||
fs.createReadStream(location).pipe(new CsvParserStream(new ParserOptions(options))); | ||
@@ -20,0 +19,0 @@ export const parseString = (string: string, options?: ParserOptionsArgs): CsvParserStream => { |
import { ParserOptions } from '../../ParserOptions'; | ||
import NonQuotedColumnParser from './NonQuotedColumnParser'; | ||
import QuotedColumnParser from './QuotedColumnParser'; | ||
import { Scanner, Token } from '../Scanner'; | ||
import { Scanner } from '../Scanner'; | ||
import { Token } from '../Token'; | ||
@@ -6,0 +7,0 @@ export default class ColumnParser { |
import { ParserOptions } from '../../ParserOptions'; | ||
import ColumnFormatter from './ColumnFormatter'; | ||
import { Scanner, Token } from '../Scanner'; | ||
import { Scanner } from '../Scanner'; | ||
import { Token } from '../Token'; | ||
const { isTokenDelimiter, isTokenRowDelimiter } = Token; | ||
@@ -7,0 +7,0 @@ |
import ColumnFormatter from './ColumnFormatter'; | ||
import { ParserOptions } from '../../ParserOptions'; | ||
import { Scanner, Token } from '../Scanner'; | ||
import { Scanner } from '../Scanner'; | ||
import { Token } from '../Token'; | ||
const { | ||
isTokenDelimiter, isTokenRowDelimiter, isTokenEscapeCharacter, isTokenQuote, | ||
} = Token; | ||
const { isTokenDelimiter, isTokenRowDelimiter, isTokenEscapeCharacter, isTokenQuote } = Token; | ||
interface DataBetweenQuotes{ | ||
interface DataBetweenQuotes { | ||
foundClosingQuote: boolean; | ||
@@ -36,3 +35,7 @@ col: string; | ||
if (!scanner.hasMoreData) { | ||
throw new Error(`Parse Error: missing closing: '${this.parserOptions.quote}' in line: at '${scanner.lineFromCursor.replace(/[r\n]/g, "\\n'")}'`); | ||
throw new Error( | ||
`Parse Error: missing closing: '${ | ||
this.parserOptions.quote | ||
}' in line: at '${scanner.lineFromCursor.replace(/[r\n]/g, "\\n'")}'`, | ||
); | ||
} | ||
@@ -86,3 +89,2 @@ return null; | ||
private checkForMalformedColumn(scanner: Scanner): void { | ||
@@ -98,3 +100,5 @@ const { parserOptions } = this; | ||
const linePreview = scanner.lineFromCursor.substr(0, 10).replace(/[\r\n]/g, "\\n'"); | ||
throw new Error(`Parse Error: expected: '${parserOptions.escapedDelimiter}' OR new line got: '${nextNonSpaceToken.token}'. at '${linePreview}`); | ||
throw new Error( | ||
`Parse Error: expected: '${parserOptions.escapedDelimiter}' OR new line got: '${nextNonSpaceToken.token}'. at '${linePreview}`, | ||
); | ||
} | ||
@@ -101,0 +105,0 @@ scanner.advanceToToken(nextNonSpaceToken); |
@@ -1,5 +0,6 @@ | ||
import { Scanner, Token } from './Scanner'; | ||
import { Scanner } from './Scanner'; | ||
import RowParser from './RowParser'; | ||
import { ParserOptions } from '../ParserOptions'; | ||
import { RowArray } from '../types'; | ||
import { Token } from './Token'; | ||
@@ -16,3 +17,3 @@ const EMPTY_ROW_REGEXP = /^\s*(?:''|"")?\s*(?:,\s*(?:''|"")?\s*)*$/; | ||
// conversion translates it to FEFF (UTF-16 BOM) | ||
if (line && line.charCodeAt(0) === 0xFEFF) { | ||
if (line && line.charCodeAt(0) === 0xfeff) { | ||
return line.slice(1); | ||
@@ -19,0 +20,0 @@ } |
@@ -1,5 +0,6 @@ | ||
import { Scanner, Token } from './Scanner'; | ||
import { Scanner } from './Scanner'; | ||
import { ColumnParser } from './column'; | ||
import { ParserOptions } from '../ParserOptions'; | ||
import { RowArray } from '../types'; | ||
import { MaybeToken, Token } from './Token'; | ||
@@ -29,5 +30,7 @@ const { isTokenRowDelimiter, isTokenCarriageReturn, isTokenDelimiter } = Token; | ||
// coming LF in CRLF | ||
if (!currentScanner.hasMoreCharacters | ||
&& isTokenCarriageReturn(currentToken, parserOptions) | ||
&& hasMoreData) { | ||
if ( | ||
!currentScanner.hasMoreCharacters && | ||
isTokenCarriageReturn(currentToken, parserOptions) && | ||
hasMoreData | ||
) { | ||
return null; | ||
@@ -54,3 +57,3 @@ } | ||
private getStartToken(scanner: Scanner, columns: RowArray): Token | null { | ||
private getStartToken(scanner: Scanner, columns: RowArray): MaybeToken { | ||
const currentToken = scanner.nextNonSpaceToken; | ||
@@ -57,0 +60,0 @@ if (currentToken !== null && isTokenDelimiter(currentToken, this.parserOptions)) { |
import { ParserOptions } from '../ParserOptions'; | ||
import { MaybeToken, Token } from './Token'; | ||
const ROW_DELIMITER = /((?:\r\n)|\n|\r)/; | ||
interface TokenArgs{ | ||
token: string; | ||
startCursor: number; | ||
endCursor: number; | ||
} | ||
export type MaybeToken = Token | null; | ||
export class Token { | ||
public static isTokenRowDelimiter(token: Token): boolean { | ||
const content = token.token; | ||
return content === '\r' || content === '\n' || content === '\r\n'; | ||
} | ||
public static isTokenCarriageReturn(token: Token, parserOptions: ParserOptions): boolean { | ||
return token.token === parserOptions.carriageReturn; | ||
} | ||
public static isTokenComment(token: Token, parserOptions: ParserOptions): boolean { | ||
return parserOptions.supportsComments && !!token && token.token === parserOptions.comment; | ||
} | ||
public static isTokenEscapeCharacter(token: Token, parserOptions: ParserOptions): boolean { | ||
return token.token === parserOptions.escapeChar; | ||
} | ||
public static isTokenQuote(token: Token, parserOptions: ParserOptions): boolean { | ||
return token.token === parserOptions.quote; | ||
} | ||
public static isTokenDelimiter(token: Token, parserOptions: ParserOptions): boolean { | ||
return token.token === parserOptions.delimiter; | ||
} | ||
public readonly token: string; | ||
public readonly startCursor: number; | ||
public readonly endCursor: number; | ||
public constructor(tokenArgs: TokenArgs) { | ||
this.token = tokenArgs.token; | ||
this.startCursor = tokenArgs.startCursor; | ||
this.endCursor = tokenArgs.endCursor; | ||
} | ||
} | ||
interface ScannerArgs{ | ||
export interface ScannerArgs { | ||
line: string; | ||
@@ -68,3 +22,3 @@ parserOptions: ParserOptions; | ||
public cursor: number = 0; | ||
public cursor = 0; | ||
@@ -71,0 +25,0 @@ public constructor(args: ScannerArgs) { |
@@ -1,4 +0,5 @@ | ||
import { escapeRegExp, isString, isNil } from 'lodash'; | ||
import escapeRegExp from 'lodash.escaperegexp'; | ||
import isNil from 'lodash.isnil'; | ||
export interface ParserOptionsArgs{ | ||
export interface ParserOptionsArgs { | ||
objectMode?: boolean; | ||
@@ -39,3 +40,3 @@ delimiter?: string; | ||
public readonly ltrim: boolean = false ; | ||
public readonly ltrim: boolean = false; | ||
@@ -66,3 +67,3 @@ public readonly rtrim: boolean = false; | ||
this.escapedDelimiter = escapeRegExp(this.delimiter); | ||
this.escapeChar = isString(this.escape) ? this.escape : this.quote; | ||
this.escapeChar = this.escape ?? this.quote; | ||
this.supportsComments = !isNil(this.comment); | ||
@@ -69,0 +70,0 @@ this.NEXT_TOKEN_REGEXP = new RegExp(`([^\\s]|\\r\\n|\\n|\\r|${this.escapedDelimiter})`); |
@@ -1,6 +0,4 @@ | ||
import { isUndefined } from 'lodash'; | ||
import isUndefined from 'lodash.isundefined'; | ||
import { ParserOptions } from '../ParserOptions'; | ||
import { | ||
Row, RowArray, RowMap, RowValidationResult, RowValidatorCallback, | ||
} from '../types'; | ||
import { Row, RowArray, RowMap, RowValidationResult, RowValidatorCallback } from '../types'; | ||
@@ -16,5 +14,5 @@ export default class HeaderTransformer { | ||
private processedFirstRow: boolean = false; | ||
private processedFirstRow = false; | ||
private headersLength: number = 0; | ||
private headersLength = 0; | ||
@@ -63,7 +61,13 @@ public constructor(parserOptions: ParserOptions) { | ||
if (!parserOptions.strictColumnHandling) { | ||
throw new Error(`Unexpected Error: column header mismatch expected: ${this.headersLength} columns got: ${row.length}`); | ||
throw new Error( | ||
`Unexpected Error: column header mismatch expected: ${this.headersLength} columns got: ${row.length}`, | ||
); | ||
} | ||
return { row, isValid: false, reason: `Column header mismatch expected: ${this.headersLength} columns got: ${row.length}` }; | ||
return { | ||
row, | ||
isValid: false, | ||
reason: `Column header mismatch expected: ${this.headersLength} columns got: ${row.length}`, | ||
}; | ||
} | ||
if (parserOptions.strictColumnHandling && (row.length < this.headersLength)) { | ||
if (parserOptions.strictColumnHandling && row.length < this.headersLength) { | ||
return { | ||
@@ -78,3 +82,2 @@ row, | ||
private mapHeaders(row: RowArray): Row { | ||
@@ -81,0 +84,0 @@ const rowMap: RowMap = {}; |
@@ -1,2 +0,2 @@ | ||
import { isFunction } from 'lodash'; | ||
import isFunction from 'lodash.isfunction'; | ||
import { | ||
@@ -14,3 +14,2 @@ Row, | ||
type RowValidator = (row: Row, cb: RowValidatorCallback) => void; | ||
@@ -17,0 +16,0 @@ |
@@ -5,3 +5,3 @@ export interface RowMap { | ||
export type RowArray = string[]; | ||
export type Row = string[] | object | ||
export type Row = string[] | object; | ||
@@ -16,15 +16,16 @@ export interface RowValidationResult { | ||
export type RowTransformCallback = (error?: Error | null, row?: Row) => void | ||
export type SyncRowTransform = (row: Row) => Row | ||
export type AsyncRowTransform = (row: Row, cb: RowTransformCallback) => void | ||
export type RowTransformFunction = SyncRowTransform | AsyncRowTransform | ||
export type RowTransformCallback = (error?: Error | null, row?: Row) => void; | ||
export type SyncRowTransform = (row: Row) => Row; | ||
export type AsyncRowTransform = (row: Row, cb: RowTransformCallback) => void; | ||
export type RowTransformFunction = SyncRowTransform | AsyncRowTransform; | ||
export const isSyncTransform = (transform: RowTransformFunction): transform is SyncRowTransform => transform.length === 1; | ||
export const isSyncTransform = (transform: RowTransformFunction): transform is SyncRowTransform => | ||
transform.length === 1; | ||
export type RowValidateCallback = (error?: Error | null, isValid?: boolean, reason?: string) => void | ||
export type RowValidateCallback = (error?: Error | null, isValid?: boolean, reason?: string) => void; | ||
export type SyncRowValidate = (row: Row) => boolean | ||
export type AsyncRowValidate = (row: Row, cb: RowValidateCallback) => void | ||
export type RowValidate = AsyncRowValidate | SyncRowValidate | ||
export type SyncRowValidate = (row: Row) => boolean; | ||
export type AsyncRowValidate = (row: Row, cb: RowValidateCallback) => void; | ||
export type RowValidate = AsyncRowValidate | SyncRowValidate; | ||
export const isSyncValidate = (validate: RowValidate): validate is SyncRowValidate => validate.length === 1; |
@@ -6,2 +6,3 @@ { | ||
"declaration": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
@@ -8,0 +9,0 @@ "lib": ["es2016"], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2923
237386
8
28
126
+ Addedlodash.escaperegexp@^4.1.2
+ Addedlodash.isboolean@^3.0.3
+ Addedlodash.isequal@^4.5.0
+ Addedlodash.isfunction@^3.0.9
+ Addedlodash.isnil@^4.0.0
+ Addedlodash.isstring@^4.0.1
+ Addedlodash.isundefined@^3.0.1
+ Addedlodash.escaperegexp@4.1.2(transitive)
+ Addedlodash.isboolean@3.0.3(transitive)
+ Addedlodash.isequal@4.5.0(transitive)
+ Addedlodash.isfunction@3.0.9(transitive)
+ Addedlodash.isnil@4.0.0(transitive)
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedlodash.isundefined@3.0.1(transitive)
- Removed@types/lodash@^4.14.132
- Removedlodash@^4.17.13
- Removed@types/lodash@4.17.13(transitive)
- Removedlodash@4.17.21(transitive)
Updated@types/node@^12.12.17