@fast-csv/format
Advanced tools
Comparing version 4.3.5 to 5.0.0
@@ -7,5 +7,7 @@ "use strict"; | ||
class CsvFormatterStream extends stream_1.Transform { | ||
formatterOptions; | ||
rowFormatter; | ||
hasWrittenBOM = false; | ||
constructor(formatterOptions) { | ||
super({ writableObjectMode: formatterOptions.objectMode }); | ||
this.hasWrittenBOM = false; | ||
this.formatterOptions = formatterOptions; | ||
@@ -12,0 +14,0 @@ this.rowFormatter = new formatter_1.RowFormatter(formatterOptions); |
@@ -11,4 +11,7 @@ "use strict"; | ||
class FieldFormatter { | ||
formatterOptions; | ||
_headers = null; | ||
REPLACE_REGEXP; | ||
ESCAPE_REGEXP; | ||
constructor(formatterOptions) { | ||
this._headers = null; | ||
this.formatterOptions = formatterOptions; | ||
@@ -19,3 +22,3 @@ if (formatterOptions.headers !== null) { | ||
this.REPLACE_REGEXP = new RegExp(formatterOptions.quote, 'g'); | ||
const escapePattern = `[${formatterOptions.delimiter}${lodash_escaperegexp_1.default(formatterOptions.rowDelimiter)}|\r|\n]`; | ||
const escapePattern = `[${formatterOptions.delimiter}${(0, lodash_escaperegexp_1.default)(formatterOptions.rowDelimiter)}|\r|\n]`; | ||
this.ESCAPE_REGEXP = new RegExp(escapePattern); | ||
@@ -28,3 +31,3 @@ } | ||
const quoteConfig = isHeader ? this.formatterOptions.quoteHeaders : this.formatterOptions.quoteColumns; | ||
if (lodash_isboolean_1.default(quoteConfig)) { | ||
if ((0, lodash_isboolean_1.default)(quoteConfig)) { | ||
return quoteConfig; | ||
@@ -41,3 +44,3 @@ } | ||
format(field, fieldIndex, isHeader) { | ||
const preparedField = `${lodash_isnil_1.default(field) ? '' : field}`.replace(/\0/g, ''); | ||
const preparedField = `${(0, lodash_isnil_1.default)(field) ? '' : field}`.replace(/\0/g, ''); | ||
const { formatterOptions } = this; | ||
@@ -44,0 +47,0 @@ if (formatterOptions.quote !== '') { |
import { FormatterOptions } from '../FormatterOptions'; | ||
import { Row, RowArray, RowTransformFunction } from '../types'; | ||
declare type RowFormatterCallback = (error: Error | null, data?: RowArray) => void; | ||
type RowFormatterCallback = (error: Error | null, data?: RowArray) => void; | ||
export declare class RowFormatter<I extends Row, O extends Row> { | ||
@@ -5,0 +5,0 @@ private static isRowHashArray; |
@@ -12,16 +12,2 @@ "use strict"; | ||
class RowFormatter { | ||
constructor(formatterOptions) { | ||
this.rowCount = 0; | ||
this.formatterOptions = formatterOptions; | ||
this.fieldFormatter = new FieldFormatter_1.FieldFormatter(formatterOptions); | ||
this.headers = formatterOptions.headers; | ||
this.shouldWriteHeaders = formatterOptions.shouldWriteHeaders; | ||
this.hasWrittenHeaders = false; | ||
if (this.headers !== null) { | ||
this.fieldFormatter.headers = this.headers; | ||
} | ||
if (formatterOptions.transform) { | ||
this.rowTransform = formatterOptions.transform; | ||
} | ||
} | ||
static isRowHashArray(row) { | ||
@@ -40,3 +26,5 @@ if (Array.isArray(row)) { | ||
// lets assume a multi-dimesional array with item 0 being the header | ||
return row.map((it) => it[0]); | ||
return row.map((it) => { | ||
return it[0]; | ||
}); | ||
} | ||
@@ -50,3 +38,3 @@ if (Array.isArray(row)) { | ||
static createTransform(transformFunction) { | ||
if (types_1.isSyncTransform(transformFunction)) { | ||
if ((0, types_1.isSyncTransform)(transformFunction)) { | ||
return (row, cb) => { | ||
@@ -67,4 +55,24 @@ let transformedRow = null; | ||
} | ||
formatterOptions; | ||
fieldFormatter; | ||
shouldWriteHeaders; | ||
_rowTransform; | ||
headers; | ||
hasWrittenHeaders; | ||
rowCount = 0; | ||
constructor(formatterOptions) { | ||
this.formatterOptions = formatterOptions; | ||
this.fieldFormatter = new FieldFormatter_1.FieldFormatter(formatterOptions); | ||
this.headers = formatterOptions.headers; | ||
this.shouldWriteHeaders = formatterOptions.shouldWriteHeaders; | ||
this.hasWrittenHeaders = false; | ||
if (this.headers !== null) { | ||
this.fieldFormatter.headers = this.headers; | ||
} | ||
if (formatterOptions.transform) { | ||
this.rowTransform = formatterOptions.transform; | ||
} | ||
} | ||
set rowTransform(transformFunction) { | ||
if (!lodash_isfunction_1.default(transformFunction)) { | ||
if (!(0, lodash_isfunction_1.default)(transformFunction)) { | ||
throw new TypeError('The transform should be a function'); | ||
@@ -127,3 +135,3 @@ } | ||
// if the row is equal to headers dont format | ||
return { shouldFormatColumns: !lodash_isequal_1.default(headers, row), headers }; | ||
return { shouldFormatColumns: !(0, lodash_isequal_1.default)(headers, row), headers }; | ||
} | ||
@@ -136,3 +144,5 @@ // todo change this method to unknown[] | ||
if (!Array.isArray(row)) { | ||
return this.headers.map((header) => row[header]); | ||
return this.headers.map((header) => { | ||
return row[header]; | ||
}); | ||
} | ||
@@ -153,3 +163,5 @@ if (RowFormatter.isRowHashArray(row)) { | ||
} | ||
return this.headers.map((header, i) => row[i]); | ||
return this.headers.map((header, i) => { | ||
return row[i]; | ||
}); | ||
} | ||
@@ -164,3 +176,5 @@ callTransformer(row, cb) { | ||
const formattedCols = columns | ||
.map((field, i) => this.fieldFormatter.format(field, i, isHeadersRow)) | ||
.map((field, i) => { | ||
return this.fieldFormatter.format(field, i, isHeadersRow); | ||
}) | ||
.join(this.formatterOptions.delimiter); | ||
@@ -167,0 +181,0 @@ const { rowCount } = this; |
@@ -5,3 +5,3 @@ import { Row, RowTransformFunction } from './types'; | ||
} | ||
declare type QuoteColumns = boolean | boolean[] | QuoteColumnMap; | ||
type QuoteColumns = boolean | boolean[] | QuoteColumnMap; | ||
export interface FormatterOptionsArgs<I extends Row, O extends Row> { | ||
@@ -8,0 +8,0 @@ objectMode?: boolean; |
@@ -5,30 +5,32 @@ "use strict"; | ||
class FormatterOptions { | ||
objectMode = true; | ||
delimiter = ','; | ||
rowDelimiter = '\n'; | ||
quote = '"'; | ||
escape = this.quote; | ||
quoteColumns = false; | ||
quoteHeaders = this.quoteColumns; | ||
headers = null; | ||
includeEndRowDelimiter = false; | ||
transform; | ||
shouldWriteHeaders; | ||
writeBOM = false; | ||
escapedQuote; | ||
BOM = '\ufeff'; | ||
alwaysWriteHeaders = false; | ||
constructor(opts = {}) { | ||
var _a; | ||
this.objectMode = true; | ||
this.delimiter = ','; | ||
this.rowDelimiter = '\n'; | ||
this.quote = '"'; | ||
this.escape = this.quote; | ||
this.quoteColumns = false; | ||
this.quoteHeaders = this.quoteColumns; | ||
this.headers = null; | ||
this.includeEndRowDelimiter = false; | ||
this.writeBOM = false; | ||
this.BOM = '\ufeff'; | ||
this.alwaysWriteHeaders = false; | ||
Object.assign(this, opts || {}); | ||
if (typeof (opts === null || opts === void 0 ? void 0 : opts.quoteHeaders) === 'undefined') { | ||
if (typeof opts?.quoteHeaders === 'undefined') { | ||
this.quoteHeaders = this.quoteColumns; | ||
} | ||
if ((opts === null || opts === void 0 ? void 0 : opts.quote) === true) { | ||
if (opts?.quote === true) { | ||
this.quote = '"'; | ||
} | ||
else if ((opts === null || opts === void 0 ? void 0 : opts.quote) === false) { | ||
else if (opts?.quote === false) { | ||
this.quote = ''; | ||
} | ||
if (typeof (opts === null || opts === void 0 ? void 0 : opts.escape) !== 'string') { | ||
if (typeof opts?.escape !== 'string') { | ||
this.escape = this.quote; | ||
} | ||
this.shouldWriteHeaders = !!this.headers && ((_a = opts.writeHeaders) !== null && _a !== void 0 ? _a : true); | ||
this.shouldWriteHeaders = !!this.headers && (opts.writeHeaders ?? true); | ||
this.headers = Array.isArray(this.headers) ? this.headers : null; | ||
@@ -35,0 +37,0 @@ this.escapedQuote = `${this.escape}${this.quote}`; |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import * as fs from 'fs'; | ||
@@ -3,0 +5,0 @@ import { Row } from './types'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -36,10 +40,19 @@ if (k2 === undefined) k2 = k; | ||
Object.defineProperty(exports, "FormatterOptions", { enumerable: true, get: function () { return FormatterOptions_2.FormatterOptions; } }); | ||
exports.format = (options) => new CsvFormatterStream_1.CsvFormatterStream(new FormatterOptions_1.FormatterOptions(options)); | ||
exports.write = (rows, options) => { | ||
const csvStream = exports.format(options); | ||
const promiseWrite = util_1.promisify((row, cb) => { | ||
const format = (options) => { | ||
return new CsvFormatterStream_1.CsvFormatterStream(new FormatterOptions_1.FormatterOptions(options)); | ||
}; | ||
exports.format = format; | ||
const write = (rows, options) => { | ||
const csvStream = (0, exports.format)(options); | ||
const promiseWrite = (0, util_1.promisify)((row, cb) => { | ||
csvStream.write(row, undefined, cb); | ||
}); | ||
rows.reduce((prev, row) => prev.then(() => promiseWrite(row)), Promise.resolve()) | ||
.then(() => csvStream.end()) | ||
rows.reduce((prev, row) => { | ||
return prev.then(() => { | ||
return promiseWrite(row); | ||
}); | ||
}, Promise.resolve()) | ||
.then(() => { | ||
csvStream.end(); | ||
}) | ||
.catch((err) => { | ||
@@ -50,4 +63,8 @@ csvStream.emit('error', err); | ||
}; | ||
exports.writeToStream = (ws, rows, options) => exports.write(rows, options).pipe(ws); | ||
exports.writeToBuffer = (rows, opts = {}) => { | ||
exports.write = write; | ||
const writeToStream = (ws, rows, options) => { | ||
return (0, exports.write)(rows, options).pipe(ws); | ||
}; | ||
exports.writeToStream = writeToStream; | ||
const writeToBuffer = (rows, opts = {}) => { | ||
const buffers = []; | ||
@@ -61,11 +78,20 @@ const ws = new stream_1.Writable({ | ||
return new Promise((res, rej) => { | ||
ws.on('error', rej).on('finish', () => res(Buffer.concat(buffers))); | ||
exports.write(rows, opts).pipe(ws); | ||
ws.on('error', rej).on('finish', () => { | ||
return res(Buffer.concat(buffers)); | ||
}); | ||
(0, exports.write)(rows, opts).pipe(ws); | ||
}); | ||
}; | ||
exports.writeToString = (rows, options) => exports.writeToBuffer(rows, options).then((buffer) => buffer.toString()); | ||
exports.writeToPath = (path, rows, options) => { | ||
exports.writeToBuffer = writeToBuffer; | ||
const writeToString = (rows, options) => { | ||
return (0, exports.writeToBuffer)(rows, options).then((buffer) => { | ||
return buffer.toString(); | ||
}); | ||
}; | ||
exports.writeToString = writeToString; | ||
const writeToPath = (path, rows, options) => { | ||
const stream = fs.createWriteStream(path, { encoding: 'utf8' }); | ||
return exports.write(rows, options).pipe(stream); | ||
return (0, exports.write)(rows, options).pipe(stream); | ||
}; | ||
exports.writeToPath = writeToPath; | ||
//# sourceMappingURL=index.js.map |
@@ -1,9 +0,9 @@ | ||
export declare type RowMap<V = any> = Record<string, V>; | ||
export declare type RowHashArray<V = any> = [string, V][]; | ||
export declare type RowArray = string[]; | ||
export declare type Row = RowArray | RowHashArray | RowMap; | ||
export declare type RowTransformCallback<R extends Row> = (error?: Error | null, row?: R) => void; | ||
export declare type SyncRowTransform<I extends Row, O extends Row> = (row: I) => O; | ||
export declare type AsyncRowTransform<I extends Row, O extends Row> = (row: I, cb: RowTransformCallback<O>) => void; | ||
export declare type RowTransformFunction<I extends Row, O extends Row> = SyncRowTransform<I, O> | AsyncRowTransform<I, O>; | ||
export type RowMap<V = any> = Record<string, V>; | ||
export type RowHashArray<V = any> = [string, V][]; | ||
export type RowArray = string[]; | ||
export type Row = RowArray | RowHashArray | RowMap; | ||
export type RowTransformCallback<R extends Row> = (error?: Error | null, row?: R) => void; | ||
export type SyncRowTransform<I extends Row, O extends Row> = (row: I) => O; | ||
export type AsyncRowTransform<I extends Row, O extends Row> = (row: I, cb: RowTransformCallback<O>) => void; | ||
export type RowTransformFunction<I extends Row, O extends Row> = SyncRowTransform<I, O> | AsyncRowTransform<I, O>; | ||
export declare const isSyncTransform: <I extends Row, O extends Row>(transform: RowTransformFunction<I, O>) => transform is SyncRowTransform<I, O>; |
@@ -5,3 +5,6 @@ "use strict"; | ||
exports.isSyncTransform = void 0; | ||
exports.isSyncTransform = (transform) => transform.length === 1; | ||
const isSyncTransform = (transform) => { | ||
return transform.length === 1; | ||
}; | ||
exports.isSyncTransform = isSyncTransform; | ||
//# sourceMappingURL=types.js.map |
106
package.json
{ | ||
"name": "@fast-csv/format", | ||
"version": "4.3.5", | ||
"description": "fast-csv formatting module", | ||
"keywords": [ | ||
"csv", | ||
"format", | ||
"write" | ||
], | ||
"author": "doug-martin <doug@dougamartin.com>", | ||
"homepage": "http://c2fo.github.com/fast-csv/packages/format", | ||
"license": "MIT", | ||
"main": "build/src/index.js", | ||
"types": "build/src/index.d.ts", | ||
"directories": { | ||
"lib": "src", | ||
"test": "__tests__" | ||
}, | ||
"files": [ | ||
"build/src/**" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/C2FO/fast-csv.git", | ||
"directory": "packages/format" | ||
}, | ||
"scripts": { | ||
"prepublishOnly": "npm run build", | ||
"build": "npm run clean && npm run compile", | ||
"clean": "rm -rf ./build && rm -rf tsconfig.tsbuildinfo", | ||
"compile": "tsc" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/C2FO/fast-csv/issues" | ||
}, | ||
"dependencies": { | ||
"@types/node": "^14.0.1", | ||
"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" | ||
}, | ||
"devDependencies": { | ||
"@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" | ||
}, | ||
"gitHead": "b908170cb49398ae12847d050af5c8e5b0dc812f" | ||
"name": "@fast-csv/format", | ||
"version": "5.0.0", | ||
"description": "fast-csv formatting module", | ||
"keywords": [ | ||
"csv", | ||
"format", | ||
"write" | ||
], | ||
"author": "doug-martin <doug@dougamartin.com>", | ||
"homepage": "https://c2fo.github.io/fast-csv/docs/formatting/getting-started/", | ||
"license": "MIT", | ||
"main": "build/src/index.js", | ||
"types": "build/src/index.d.ts", | ||
"directories": { | ||
"lib": "src", | ||
"test": "__tests__" | ||
}, | ||
"files": [ | ||
"build/src/**" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/C2FO/fast-csv.git", | ||
"directory": "packages/format" | ||
}, | ||
"scripts": { | ||
"prepublishOnly": "npm run build", | ||
"build": "npm run clean && npm run compile", | ||
"clean": "rm -rf ./build && rm -rf tsconfig.tsbuildinfo", | ||
"compile": "tsc" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/C2FO/fast-csv/issues" | ||
}, | ||
"dependencies": { | ||
"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" | ||
}, | ||
"devDependencies": { | ||
"@types/lodash.escaperegexp": "4.1.9", | ||
"@types/lodash.isboolean": "3.0.9", | ||
"@types/lodash.isequal": "4.5.8", | ||
"@types/lodash.isfunction": "3.0.9", | ||
"@types/lodash.isnil": "4.0.9", | ||
"@types/node": "^20.10.5" | ||
}, | ||
"gitHead": "3305fb8cd9e96d65528cb396862005a737297866" | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
40477
5
569
0
6
24
1
- Removed@types/node@^14.0.1
- Removed@types/node@14.18.63(transitive)