文件夹整理

This commit is contained in:
wkc
2026-02-09 14:28:25 +08:00
parent 056d239041
commit 02249c402e
2429 changed files with 3159 additions and 239710 deletions

View File

@@ -1,73 +0,0 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [4.3.5](https://github.com/C2FO/fast-csv/compare/v4.3.4...v4.3.5) (2020-11-03)
### Bug Fixes
* **formatting,#446:** Do not quote fields that do not contain a quote ([13e688c](https://github.com/C2FO/fast-csv/commit/13e688cb38dcb67c7182211968c794146be54692)), closes [#446](https://github.com/C2FO/fast-csv/issues/446)
## [4.3.4](https://github.com/C2FO/fast-csv/compare/v4.3.3...v4.3.4) (2020-11-03)
### Bug Fixes
* **formatter,#503:** Do not ignore rows when headers is false ([1560564](https://github.com/C2FO/fast-csv/commit/1560564819c8b1254ca4ad43487830a4296570f6)), closes [#503](https://github.com/C2FO/fast-csv/issues/503)
## [4.3.3](https://github.com/C2FO/fast-csv/compare/v4.3.2...v4.3.3) (2020-10-30)
**Note:** Version bump only for package @fast-csv/format
## [4.3.1](https://github.com/C2FO/fast-csv/compare/v4.3.0...v4.3.1) (2020-06-23)
**Note:** Version bump only for package @fast-csv/format
# [4.3.0](https://github.com/C2FO/fast-csv/compare/v4.2.0...v4.3.0) (2020-05-27)
**Note:** Version bump only for package @fast-csv/format
# [4.2.0](https://github.com/C2FO/fast-csv/compare/v4.1.6...v4.2.0) (2020-05-19)
### Features
* **parsing:** Less restrictive row parsing type [#356](https://github.com/C2FO/fast-csv/issues/356) ([87d74ec](https://github.com/C2FO/fast-csv/commit/87d74ecd2cb16f3700b1942ebbbec221afe38790))
## [4.1.5](https://github.com/C2FO/fast-csv/compare/v4.1.4...v4.1.5) (2020-05-15)
**Note:** Version bump only for package @fast-csv/format
## [4.1.4](https://github.com/C2FO/fast-csv/compare/v4.1.3...v4.1.4) (2020-05-15)
**Note:** Version bump only for package @fast-csv/format

View File

@@ -1,21 +0,0 @@
The MIT License
Copyright (c) 2011-2019 C2FO
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -1,20 +0,0 @@
<p align="center">
<a href="https://c2fo.io/fast-csv" target="blank"><img src="https://c2fo.io/fast-csv/img/logo.svg" width="200" alt="fast-csv Logo" /></a>
</p>
[![npm version](https://img.shields.io/npm/v/@fast-csv/format.svg)](https://www.npmjs.org/package/@fast-csv/format)
[![Build Status](https://travis-ci.org/C2FO/fast-csv.svg?branch=master)](https://travis-ci.org/C2FO/fast-csv)
[![Coverage Status](https://coveralls.io/repos/github/C2FO/fast-csv/badge.svg?branch=master)](https://coveralls.io/github/C2FO/fast-csv?branch=master)
[![Known Vulnerabilities](https://snyk.io/test/github/C2FO/fast-csv/badge.svg?targetFile=packages/format/package.json)](https://snyk.io/test/github/C2FO/fast-csv?targetFile=packages/format/package.json)
# `@fast-csv/format`
`fast-csv` package to format CSVs.
## Installation
[Install Guide](https://c2fo.io/fast-csv/docs/introduction/install)
## Usage
To get started with `@fast-csv/format` [check out the docs](https://c2fo.io/fast-csv/docs/formatting/getting-started)

View File

@@ -1,13 +0,0 @@
/// <reference types="node" />
import { Transform, TransformCallback } from 'stream';
import { FormatterOptions } from './FormatterOptions';
import { Row, RowTransformFunction } from './types';
export declare class CsvFormatterStream<I extends Row, O extends Row> extends Transform {
private formatterOptions;
private rowFormatter;
private hasWrittenBOM;
constructor(formatterOptions: FormatterOptions<I, O>);
transform(transformFunction: RowTransformFunction<I, O>): CsvFormatterStream<I, O>;
_transform(row: I, encoding: string, cb: TransformCallback): void;
_flush(cb: TransformCallback): void;
}

View File

@@ -1,63 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsvFormatterStream = void 0;
const stream_1 = require("stream");
const formatter_1 = require("./formatter");
class CsvFormatterStream extends stream_1.Transform {
constructor(formatterOptions) {
super({ writableObjectMode: formatterOptions.objectMode });
this.hasWrittenBOM = false;
this.formatterOptions = formatterOptions;
this.rowFormatter = new formatter_1.RowFormatter(formatterOptions);
// if writeBOM is false then set to true
// if writeBOM is true then set to false by default so it is written out
this.hasWrittenBOM = !formatterOptions.writeBOM;
}
transform(transformFunction) {
this.rowFormatter.rowTransform = transformFunction;
return this;
}
_transform(row, encoding, cb) {
let cbCalled = false;
try {
if (!this.hasWrittenBOM) {
this.push(this.formatterOptions.BOM);
this.hasWrittenBOM = true;
}
this.rowFormatter.format(row, (err, rows) => {
if (err) {
cbCalled = true;
return cb(err);
}
if (rows) {
rows.forEach((r) => {
this.push(Buffer.from(r, 'utf8'));
});
}
cbCalled = true;
return cb();
});
}
catch (e) {
if (cbCalled) {
throw e;
}
cb(e);
}
}
_flush(cb) {
this.rowFormatter.finish((err, rows) => {
if (err) {
return cb(err);
}
if (rows) {
rows.forEach((r) => {
this.push(Buffer.from(r, 'utf8'));
});
}
return cb();
});
}
}
exports.CsvFormatterStream = CsvFormatterStream;
//# sourceMappingURL=CsvFormatterStream.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"CsvFormatterStream.js","sourceRoot":"","sources":["../../src/CsvFormatterStream.ts"],"names":[],"mappings":";;;AAAA,mCAAs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AChB,OAAO,EAAE,EAAE,CAAC;YAChB,CAAC,CAAC,CAAC;SACN;QAAC,OAAO,CAAC,EAAE;YACR,IAAI,QAAQ,EAAE;gBACV,MAAM,CAAC,CAAC;aACX;YACD,EAAE,CAAC,CAAC,CAAC,CAAC;SACT;IACL,CAAC;IAEM,MAAM,CAAC,EAAqB;QAC/B,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAQ,EAAE;YACzC,IAAI,GAAG,EAAE;gBACL,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;aAClB;YACD,IAAI,IAAI,EAAE;gBACN,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAQ,EAAE;oBACrB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;gBACtC,CAAC,CAAC,CAAC;aACN;YACD,OAAO,EAAE,EAAE,CAAC;QAChB,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AA9DD,gDA8DC"}

View File

@@ -1,39 +0,0 @@
import { Row, RowTransformFunction } from './types';
interface QuoteColumnMap {
[s: string]: boolean;
}
declare type QuoteColumns = boolean | boolean[] | QuoteColumnMap;
export interface FormatterOptionsArgs<I extends Row, O extends Row> {
objectMode?: boolean;
delimiter?: string;
rowDelimiter?: string;
quote?: string | boolean;
escape?: string;
quoteColumns?: QuoteColumns;
quoteHeaders?: QuoteColumns;
headers?: null | boolean | string[];
writeHeaders?: boolean;
includeEndRowDelimiter?: boolean;
writeBOM?: boolean;
transform?: RowTransformFunction<I, O>;
alwaysWriteHeaders?: boolean;
}
export declare class FormatterOptions<I extends Row, O extends Row> {
readonly objectMode: boolean;
readonly delimiter: string;
readonly rowDelimiter: string;
readonly quote: string;
readonly escape: string;
readonly quoteColumns: QuoteColumns;
readonly quoteHeaders: QuoteColumns;
readonly headers: null | string[];
readonly includeEndRowDelimiter: boolean;
readonly transform?: RowTransformFunction<I, O>;
readonly shouldWriteHeaders: boolean;
readonly writeBOM: boolean;
readonly escapedQuote: string;
readonly BOM: string;
readonly alwaysWriteHeaders: boolean;
constructor(opts?: FormatterOptionsArgs<I, O>);
}
export {};

View File

@@ -1,38 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormatterOptions = void 0;
class FormatterOptions {
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') {
this.quoteHeaders = this.quoteColumns;
}
if ((opts === null || opts === void 0 ? void 0 : opts.quote) === true) {
this.quote = '"';
}
else if ((opts === null || opts === void 0 ? void 0 : opts.quote) === false) {
this.quote = '';
}
if (typeof (opts === null || opts === void 0 ? void 0 : opts.escape) !== 'string') {
this.escape = this.quote;
}
this.shouldWriteHeaders = !!this.headers && ((_a = opts.writeHeaders) !== null && _a !== void 0 ? _a : true);
this.headers = Array.isArray(this.headers) ? this.headers : null;
this.escapedQuote = `${this.escape}${this.quote}`;
}
}
exports.FormatterOptions = FormatterOptions;
//# sourceMappingURL=FormatterOptions.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"FormatterOptions.js","sourceRoot":"","sources":["../../src/FormatterOptions.ts"],"names":[],"mappings":";;;AAwBA,MAAa,gBAAgB;IA+BzB,YAAmB,OAAmC,EAAE;;QA9BxC,eAAU,GAAY,IAAI,CAAC;QAE3B,cAAS,GAAW,GAAG,CAAC;QAExB,iBAAY,GAAW,IAAI,CAAC;QAE5B,UAAK,GAAW,GAAG,CAAC;QAEpB,WAAM,GAAW,IAAI,CAAC,KAAK,CAAC;QAE5B,iBAAY,GAAiB,KAAK,CAAC;QAEnC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-1,13 +0,0 @@
import { FormatterOptions } from '../FormatterOptions';
import { Row } from '../types';
export declare class FieldFormatter<I extends Row, O extends Row> {
private readonly formatterOptions;
private _headers;
private readonly REPLACE_REGEXP;
private readonly ESCAPE_REGEXP;
constructor(formatterOptions: FormatterOptions<I, O>);
set headers(headers: string[]);
private shouldQuote;
format(field: string, fieldIndex: number, isHeader: boolean): string;
private quoteField;
}

View File

@@ -1,58 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldFormatter = void 0;
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 {
constructor(formatterOptions) {
this._headers = null;
this.formatterOptions = formatterOptions;
if (formatterOptions.headers !== null) {
this.headers = formatterOptions.headers;
}
this.REPLACE_REGEXP = new RegExp(formatterOptions.quote, 'g');
const escapePattern = `[${formatterOptions.delimiter}${lodash_escaperegexp_1.default(formatterOptions.rowDelimiter)}|\r|\n]`;
this.ESCAPE_REGEXP = new RegExp(escapePattern);
}
set headers(headers) {
this._headers = headers;
}
shouldQuote(fieldIndex, isHeader) {
const quoteConfig = isHeader ? this.formatterOptions.quoteHeaders : this.formatterOptions.quoteColumns;
if (lodash_isboolean_1.default(quoteConfig)) {
return quoteConfig;
}
if (Array.isArray(quoteConfig)) {
return quoteConfig[fieldIndex];
}
if (this._headers !== null) {
return quoteConfig[this._headers[fieldIndex]];
}
return false;
}
format(field, fieldIndex, isHeader) {
const preparedField = `${lodash_isnil_1.default(field) ? '' : field}`.replace(/\0/g, '');
const { formatterOptions } = this;
if (formatterOptions.quote !== '') {
const shouldEscape = preparedField.indexOf(formatterOptions.quote) !== -1;
if (shouldEscape) {
return this.quoteField(preparedField.replace(this.REPLACE_REGEXP, formatterOptions.escapedQuote));
}
}
const hasEscapeCharacters = preparedField.search(this.ESCAPE_REGEXP) !== -1;
if (hasEscapeCharacters || this.shouldQuote(fieldIndex, isHeader)) {
return this.quoteField(preparedField);
}
return preparedField;
}
quoteField(field) {
const { quote } = this.formatterOptions;
return `${quote}${field}${quote}`;
}
}
exports.FieldFormatter = FieldFormatter;
//# sourceMappingURL=FieldFormatter.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"FieldFormatter.js","sourceRoot":"","sources":["../../../src/formatter/FieldFormatter.ts"],"names":[],"mappings":";;;;;;AAAA,wEAAyC;AACzC,gEAAiC;AACjC,8EAA+C;AAI/C,MAAa,cAAc;IASvB,YAAmB,gBAAwC;QANnD,aAAQ,GAAoB,IAAI,CAAC;QAOrC,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,gBAAgB,CAAC,OAAO,KAAK,IAAI,EAAE;YACnC,IAAI,CAAC,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC;SAC3C;QACD,IAAI,CAAC,cAAc,GAAG,IAAI,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC9D,MAAM,aAAa,GAAG,IAAI,gBAAgB,CAAC,SAAS,GAAG,6BAAY,CAAC,gBAAgB,CAAC,YAAY,CAAC,SAAS,CAAC;QAC5G,IAAI,CAAC,aAAa,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,CAAC;IACnD,CAAC;IAED,IAAW,OAAO,CAAC,OAAiB;QAChC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAEO,WAAW,CAAC,UAAkB,EAAE,QAAiB;QACrD,MAAM,WAAW,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC;QACvG,IAAI,0BAAS,CAAC,WAAW,CAAC,EAAE;YACxB,OAAO,WAAW,CAAC;SACtB;Q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K,GAAG,KAAK,EAAE,CAAC;IACtC,CAAC;CACJ;AAzDD,wCAyDC"}

View File

@@ -1,25 +0,0 @@
import { FormatterOptions } from '../FormatterOptions';
import { Row, RowArray, RowTransformFunction } from '../types';
declare type RowFormatterCallback = (error: Error | null, data?: RowArray) => void;
export declare class RowFormatter<I extends Row, O extends Row> {
private static isRowHashArray;
private static isRowArray;
private static gatherHeaders;
private static createTransform;
private readonly formatterOptions;
private readonly fieldFormatter;
private readonly shouldWriteHeaders;
private _rowTransform?;
private headers;
private hasWrittenHeaders;
private rowCount;
constructor(formatterOptions: FormatterOptions<I, O>);
set rowTransform(transformFunction: RowTransformFunction<I, O>);
format(row: I, cb: RowFormatterCallback): void;
finish(cb: RowFormatterCallback): void;
private checkHeaders;
private gatherColumns;
private callTransformer;
private formatColumns;
}
export {};

View File

@@ -1,168 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RowFormatter = void 0;
const lodash_isfunction_1 = __importDefault(require("lodash.isfunction"));
const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
const FieldFormatter_1 = require("./FieldFormatter");
const types_1 = require("../types");
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) {
if (Array.isArray(row)) {
return Array.isArray(row[0]) && row[0].length === 2;
}
return false;
}
static isRowArray(row) {
return Array.isArray(row) && !this.isRowHashArray(row);
}
// get headers from a row item
static gatherHeaders(row) {
if (RowFormatter.isRowHashArray(row)) {
// lets assume a multi-dimesional array with item 0 being the header
return row.map((it) => it[0]);
}
if (Array.isArray(row)) {
return row;
}
return Object.keys(row);
}
// eslint-disable-next-line @typescript-eslint/no-shadow
static createTransform(transformFunction) {
if (types_1.isSyncTransform(transformFunction)) {
return (row, cb) => {
let transformedRow = null;
try {
transformedRow = transformFunction(row);
}
catch (e) {
return cb(e);
}
return cb(null, transformedRow);
};
}
return (row, cb) => {
transformFunction(row, cb);
};
}
set rowTransform(transformFunction) {
if (!lodash_isfunction_1.default(transformFunction)) {
throw new TypeError('The transform should be a function');
}
this._rowTransform = RowFormatter.createTransform(transformFunction);
}
format(row, cb) {
this.callTransformer(row, (err, transformedRow) => {
if (err) {
return cb(err);
}
if (!row) {
return cb(null);
}
const rows = [];
if (transformedRow) {
const { shouldFormatColumns, headers } = this.checkHeaders(transformedRow);
if (this.shouldWriteHeaders && headers && !this.hasWrittenHeaders) {
rows.push(this.formatColumns(headers, true));
this.hasWrittenHeaders = true;
}
if (shouldFormatColumns) {
const columns = this.gatherColumns(transformedRow);
rows.push(this.formatColumns(columns, false));
}
}
return cb(null, rows);
});
}
finish(cb) {
const rows = [];
// check if we should write headers and we didnt get any rows
if (this.formatterOptions.alwaysWriteHeaders && this.rowCount === 0) {
if (!this.headers) {
return cb(new Error('`alwaysWriteHeaders` option is set to true but `headers` option not provided.'));
}
rows.push(this.formatColumns(this.headers, true));
}
if (this.formatterOptions.includeEndRowDelimiter) {
rows.push(this.formatterOptions.rowDelimiter);
}
return cb(null, rows);
}
// check if we need to write header return true if we should also write a row
// could be false if headers is true and the header row(first item) is passed in
checkHeaders(row) {
if (this.headers) {
// either the headers were provided by the user or we have already gathered them.
return { shouldFormatColumns: true, headers: this.headers };
}
const headers = RowFormatter.gatherHeaders(row);
this.headers = headers;
this.fieldFormatter.headers = headers;
if (!this.shouldWriteHeaders) {
// if we are not supposed to write the headers then
// always format the columns
return { shouldFormatColumns: true, headers: null };
}
// if the row is equal to headers dont format
return { shouldFormatColumns: !lodash_isequal_1.default(headers, row), headers };
}
// todo change this method to unknown[]
gatherColumns(row) {
if (this.headers === null) {
throw new Error('Headers is currently null');
}
if (!Array.isArray(row)) {
return this.headers.map((header) => row[header]);
}
if (RowFormatter.isRowHashArray(row)) {
return this.headers.map((header, i) => {
const col = row[i];
if (col) {
return col[1];
}
return '';
});
}
// if its a one dimensional array and headers were not provided
// then just return the row
if (RowFormatter.isRowArray(row) && !this.shouldWriteHeaders) {
return row;
}
return this.headers.map((header, i) => row[i]);
}
callTransformer(row, cb) {
if (!this._rowTransform) {
return cb(null, row);
}
return this._rowTransform(row, cb);
}
formatColumns(columns, isHeadersRow) {
const formattedCols = columns
.map((field, i) => this.fieldFormatter.format(field, i, isHeadersRow))
.join(this.formatterOptions.delimiter);
const { rowCount } = this;
this.rowCount += 1;
if (rowCount) {
return [this.formatterOptions.rowDelimiter, formattedCols].join('');
}
return formattedCols;
}
}
exports.RowFormatter = RowFormatter;
//# sourceMappingURL=RowFormatter.js.map

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +0,0 @@
export { RowFormatter } from './RowFormatter';
export { FieldFormatter } from './FieldFormatter';

View File

@@ -1,8 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldFormatter = exports.RowFormatter = void 0;
var RowFormatter_1 = require("./RowFormatter");
Object.defineProperty(exports, "RowFormatter", { enumerable: true, get: function () { return RowFormatter_1.RowFormatter; } });
var FieldFormatter_1 = require("./FieldFormatter");
Object.defineProperty(exports, "FieldFormatter", { enumerable: true, get: function () { return FieldFormatter_1.FieldFormatter; } });
//# sourceMappingURL=index.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/formatter/index.ts"],"names":[],"mappings":";;;AAAA,+CAA8C;AAArC,4GAAA,YAAY,OAAA;AACrB,mDAAkD;AAAzC,gHAAA,cAAc,OAAA"}

View File

@@ -1,14 +0,0 @@
/// <reference types="node" />
import * as fs from 'fs';
import { Row } from './types';
import { FormatterOptionsArgs } from './FormatterOptions';
import { CsvFormatterStream } from './CsvFormatterStream';
export * from './types';
export { CsvFormatterStream } from './CsvFormatterStream';
export { FormatterOptions, FormatterOptionsArgs } from './FormatterOptions';
export declare const format: <I extends Row, O extends Row>(options?: FormatterOptionsArgs<I, O> | undefined) => CsvFormatterStream<I, O>;
export declare const write: <I extends Row, O extends Row>(rows: I[], options?: FormatterOptionsArgs<I, O> | undefined) => CsvFormatterStream<I, O>;
export declare const writeToStream: <T extends NodeJS.WritableStream, I extends Row, O extends Row>(ws: T, rows: I[], options?: FormatterOptionsArgs<I, O> | undefined) => T;
export declare const writeToBuffer: <I extends Row, O extends Row>(rows: I[], opts?: FormatterOptionsArgs<I, O>) => Promise<Buffer>;
export declare const writeToString: <I extends Row, O extends Row>(rows: I[], options?: FormatterOptionsArgs<I, O> | undefined) => Promise<string>;
export declare const writeToPath: <I extends Row, O extends Row>(path: string, rows: I[], options?: FormatterOptionsArgs<I, O> | undefined) => fs.WriteStream;

View File

@@ -1,68 +0,0 @@
"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]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeToPath = exports.writeToString = exports.writeToBuffer = exports.writeToStream = exports.write = exports.format = exports.FormatterOptions = exports.CsvFormatterStream = void 0;
const util_1 = require("util");
const stream_1 = require("stream");
const fs = __importStar(require("fs"));
const FormatterOptions_1 = require("./FormatterOptions");
const CsvFormatterStream_1 = require("./CsvFormatterStream");
__exportStar(require("./types"), exports);
var CsvFormatterStream_2 = require("./CsvFormatterStream");
Object.defineProperty(exports, "CsvFormatterStream", { enumerable: true, get: function () { return CsvFormatterStream_2.CsvFormatterStream; } });
var FormatterOptions_2 = require("./FormatterOptions");
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) => {
csvStream.write(row, undefined, cb);
});
rows.reduce((prev, row) => prev.then(() => promiseWrite(row)), Promise.resolve())
.then(() => csvStream.end())
.catch((err) => {
csvStream.emit('error', err);
});
return csvStream;
};
exports.writeToStream = (ws, rows, options) => exports.write(rows, options).pipe(ws);
exports.writeToBuffer = (rows, opts = {}) => {
const buffers = [];
const ws = new stream_1.Writable({
write(data, enc, writeCb) {
buffers.push(data);
writeCb();
},
});
return new Promise((res, rej) => {
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.writeToPath = (path, rows, options) => {
const stream = fs.createWriteStream(path, { encoding: 'utf8' });
return exports.write(rows, options).pipe(stream);
};
//# sourceMappingURL=index.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAAiC;AACjC,mCAAkC;AAClC,uCAAyB;AAEzB,yDAA4E;AAC5E,6DAA0D;AAE1D,0CAAwB;AACxB,2DAA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,GAAG,EAAE,OAAO;YACpB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACnB,OAAO,EAAE,CAAC;QACd,CAAC;KACJ,CAAC,CAAC;IACH,OAAO,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,EAAQ,EAAE;QAClC,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAS,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAC1E,aAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC/B,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEW,QAAA,aAAa,GAAG,CACzB,IAAS,EACT,OAAoC,EACrB,EAAE,CAAC,qBAAa,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAU,EAAE,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;AAElF,QAAA,WAAW,GAAG,CACvB,IAAY,EACZ,IAAS,EACT,OAAoC,EACtB,EAAE;IAChB,MAAM,MAAM,GAAG,EAAE,CAAC,iBAAiB,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IAChE,OAAO,aAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC7C,CAAC,CAAC"}

View File

@@ -1,9 +0,0 @@
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 declare const isSyncTransform: <I extends Row, O extends Row>(transform: RowTransformFunction<I, O>) => transform is SyncRowTransform<I, O>;

View File

@@ -1,6 +0,0 @@
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSyncTransform = void 0;
exports.isSyncTransform = (transform) => transform.length === 1;
//# sourceMappingURL=types.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";AAAA,uDAAuD;;;AAY1C,QAAA,eAAe,GAAG,CAC3B,SAAqC,EACF,EAAE,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,CAAC"}

View File

@@ -1,55 +0,0 @@
{
"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"
}