fix: 修复中介导入成功条数计算错误

问题:
- 导入成功条数显示为负数
- 原因:成功数量计算使用 validRecords.size() - failures.size()
- 但没有使用实际的数据库操作返回值

修复:
- saveBatchWithUpsert 和 saveBatch 方法现在返回 int
- 累加实际的数据库影响行数
- 使用 actualSuccessCount 变量跟踪真实成功数量

影响范围:
- CcdiIntermediaryPersonImportServiceImpl
- CcdiIntermediaryEntityImportServiceImpl
This commit is contained in:
wkc
2026-02-08 17:18:18 +08:00
parent bb0d68c41d
commit 5ec5913759
2058 changed files with 234134 additions and 269 deletions

Binary file not shown.

View File

@@ -0,0 +1,226 @@
const Excel = require('exceljs');
// 配置
const OUTPUT_FILE = 'purchase_test_data_2000_v2.xlsx';
const RECORD_COUNT = 2000;
// 数据池
const PURCHASE_CATEGORIES = ['货物类', '工程类', '服务类', '软件系统', '办公设备', '家具用具', '专用设备', '通讯设备'];
const PURCHASE_METHODS = ['公开招标', '邀请招标', '询价采购', '单一来源', '竞争性谈判'];
const DEPARTMENTS = ['人事部', '行政部', '财务部', '技术部', '市场部', '采购部', '研发部'];
const EMPLOYEES = [
{ id: 'EMP0001', name: '张伟' },
{ id: 'EMP0002', name: '王芳' },
{ id: 'EMP0003', name: '李娜' },
{ id: 'EMP0004', name: '刘洋' },
{ id: 'EMP0005', name: '陈静' },
{ id: 'EMP0006', name: '杨强' },
{ id: 'EMP0007', name: '赵敏' },
{ id: 'EMP0008', name: '孙杰' },
{ id: 'EMP0009', name: '周涛' },
{ id: 'EMP0010', name: '吴刚' },
{ id: 'EMP0011', name: '郑丽' },
{ id: 'EMP0012', name: '钱勇' },
{ id: 'EMP0013', name: '何静' },
{ id: 'EMP0014', name: '朱涛' },
{ id: 'EMP0015', name: '马超' }
];
// 生成随机整数
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 生成随机浮点数
function randomFloat(min, max, decimals = 2) {
const num = Math.random() * (max - min) + min;
return parseFloat(num.toFixed(decimals));
}
// 从数组中随机选择
function randomChoice(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
// 生成随机日期
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
// 生成采购事项ID
function generatePurchaseId(index) {
const timestamp = Date.now();
const num = String(index + 1).padStart(4, '0');
return `PUR${timestamp}${num}`;
}
// 生成测试数据
function generateTestData(count) {
const data = [];
const startDate = new Date('2023-01-01');
const endDate = new Date('2025-12-31');
for (let i = 0; i < count; i++) {
const purchaseQty = randomFloat(1, 5000, 2);
const unitPrice = randomFloat(100, 50000, 2);
const budgetAmount = parseFloat((purchaseQty * unitPrice).toFixed(2));
const discount = randomFloat(0.85, 0.98, 2);
const actualAmount = parseFloat((budgetAmount * discount).toFixed(2));
const employee = randomChoice(EMPLOYEES);
// 生成Date对象
const applyDateObj = randomDate(startDate, endDate);
// 生成后续日期(都比申请日期晚)
const planApproveDate = new Date(applyDateObj);
planApproveDate.setDate(planApproveDate.getDate() + randomInt(1, 7));
const announceDate = new Date(planApproveDate);
announceDate.setDate(announceDate.getDate() + randomInt(3, 15));
const bidOpenDate = new Date(announceDate);
bidOpenDate.setDate(bidOpenDate.getDate() + randomInt(5, 20));
const contractSignDate = new Date(bidOpenDate);
contractSignDate.setDate(contractSignDate.getDate() + randomInt(3, 10));
const expectedDeliveryDate = new Date(contractSignDate);
expectedDeliveryDate.setDate(expectedDeliveryDate.getDate() + randomInt(15, 60));
const actualDeliveryDate = new Date(expectedDeliveryDate);
actualDeliveryDate.setDate(actualDeliveryDate.getDate() + randomInt(-2, 5));
const acceptanceDate = new Date(actualDeliveryDate);
acceptanceDate.setDate(acceptanceDate.getDate() + randomInt(1, 7));
const settlementDate = new Date(acceptanceDate);
settlementDate.setDate(settlementDate.getDate() + randomInt(7, 30));
data.push({
purchaseId: generatePurchaseId(i),
purchaseCategory: randomChoice(PURCHASE_CATEGORIES),
projectName: `${randomChoice(PURCHASE_CATEGORIES)}采购项目-${String(i + 1).padStart(4, '0')}`,
subjectName: `${randomChoice(PURCHASE_CATEGORIES).replace('类', '')}配件-${String(i + 1).padStart(4, '0')}`,
subjectDesc: `${randomChoice(PURCHASE_CATEGORIES)}采购项目标的物详细描述-${String(i + 1).padStart(4, '0')}`,
purchaseQty: purchaseQty,
budgetAmount: budgetAmount,
bidAmount: actualAmount,
actualAmount: actualAmount,
contractAmount: actualAmount,
settlementAmount: actualAmount,
purchaseMethod: randomChoice(PURCHASE_METHODS),
supplierName: `供应商公司-${String(i + 1).padStart(4, '0')}有限公司`,
contactPerson: `联系人-${String(i + 1).padStart(4, '0')}`,
contactPhone: `13${randomInt(0, 9)}${String(randomInt(10000000, 99999999))}`,
supplierUscc: `91${randomInt(10000000, 99999999)}MA${String(randomInt(1000, 9999))}`,
supplierBankAccount: `6222${String(randomInt(100000000000000, 999999999999999))}`,
applyDate: applyDateObj, // Date对象
planApproveDate: planApproveDate,
announceDate: announceDate,
bidOpenDate: bidOpenDate,
contractSignDate: contractSignDate,
expectedDeliveryDate: expectedDeliveryDate,
actualDeliveryDate: actualDeliveryDate,
acceptanceDate: acceptanceDate,
settlementDate: settlementDate,
applicantId: employee.id,
applicantName: employee.name,
applyDepartment: randomChoice(DEPARTMENTS),
purchaseLeaderId: randomChoice(EMPLOYEES).id,
purchaseLeaderName: randomChoice(EMPLOYEES).name,
purchaseDepartment: '采购部'
});
}
return data;
}
// 创建Excel文件
async function createExcelFile() {
console.log('开始生成测试数据...');
console.log(`记录数: ${RECORD_COUNT}`);
// 生成测试数据
const testData = generateTestData(RECORD_COUNT);
console.log('测试数据生成完成');
// 创建工作簿
const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet('采购交易数据');
// 定义列按照Excel实体类的index顺序
worksheet.columns = [
{ header: '采购事项ID', key: 'purchaseId', width: 25 },
{ header: '采购类别', key: 'purchaseCategory', width: 15 },
{ header: '项目名称', key: 'projectName', width: 30 },
{ header: '标的物名称', key: 'subjectName', width: 30 },
{ header: '标的物描述', key: 'subjectDesc', width: 35 },
{ header: '采购数量', key: 'purchaseQty', width: 15 },
{ header: '预算金额', key: 'budgetAmount', width: 18 },
{ header: '中标金额', key: 'bidAmount', width: 18 },
{ header: '实际采购金额', key: 'actualAmount', width: 18 },
{ header: '合同金额', key: 'contractAmount', width: 18 },
{ header: '结算金额', key: 'settlementAmount', width: 18 },
{ header: '采购方式', key: 'purchaseMethod', width: 15 },
{ header: '中标供应商名称', key: 'supplierName', width: 30 },
{ header: '供应商联系人', key: 'contactPerson', width: 15 },
{ header: '供应商联系电话', key: 'contactPhone', width: 18 },
{ header: '供应商统一信用代码', key: 'supplierUscc', width: 25 },
{ header: '供应商银行账户', key: 'supplierBankAccount', width: 25 },
{ header: '采购申请日期', key: 'applyDate', width: 18 },
{ header: '采购计划批准日期', key: 'planApproveDate', width: 18 },
{ header: '采购公告发布日期', key: 'announceDate', width: 18 },
{ header: '开标日期', key: 'bidOpenDate', width: 18 },
{ header: '合同签订日期', key: 'contractSignDate', width: 18 },
{ header: '预计交货日期', key: 'expectedDeliveryDate', width: 18 },
{ header: '实际交货日期', key: 'actualDeliveryDate', width: 18 },
{ header: '验收日期', key: 'acceptanceDate', width: 18 },
{ header: '结算日期', key: 'settlementDate', width: 18 },
{ header: '申请人工号', key: 'applicantId', width: 15 },
{ header: '申请人姓名', key: 'applicantName', width: 15 },
{ header: '申请部门', key: 'applyDepartment', width: 18 },
{ header: '采购负责人工号', key: 'purchaseLeaderId', width: 15 },
{ header: '采购负责人姓名', key: 'purchaseLeaderName', width: 15 },
{ header: '采购部门', key: 'purchaseDepartment', width: 18 }
];
// 添加数据
worksheet.addRows(testData);
// 设置表头样式
const headerRow = worksheet.getRow(1);
headerRow.font = { bold: true };
headerRow.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFE6E6FA' }
};
// 保存文件
console.log('正在写入Excel文件...');
await workbook.xlsx.writeFile(OUTPUT_FILE);
console.log(`✓ 文件已保存: ${OUTPUT_FILE}`);
// 显示统计信息
console.log('\n========================================');
console.log('数据统计');
console.log('========================================');
console.log(`总记录数: ${testData.length}`);
console.log(`采购数量范围: ${Math.min(...testData.map(d => d.purchaseQty))} - ${Math.max(...testData.map(d => d.purchaseQty))}`);
console.log(`预算金额范围: ${Math.min(...testData.map(d => d.budgetAmount))} - ${Math.max(...testData.map(d => d.budgetAmount))}`);
console.log('\n前3条记录预览:');
testData.slice(0, 3).forEach((record, index) => {
console.log(`\n记录 ${index + 1}:`);
console.log(` 采购事项ID: ${record.purchaseId}`);
console.log(` 项目名称: ${record.projectName}`);
console.log(` 采购数量: ${record.purchaseQty}`);
console.log(` 预算金额: ${record.budgetAmount}`);
console.log(` 申请人: ${record.applicantName} (${record.applicantId})`);
console.log(` 申请部门: ${record.applyDepartment}`);
console.log(` 申请日期: ${record.applyDate}`);
});
}
// 运行
createExcelFile().catch(console.error);

View File

@@ -0,0 +1,16 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../crc-32/bin/crc32.njs" "$@"
else
exec node "$basedir/../crc-32/bin/crc32.njs" "$@"
fi

View File

@@ -0,0 +1,17 @@
@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\crc-32\bin\crc32.njs" %*

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/../crc-32/bin/crc32.njs" $args
} else {
& "$basedir/node$exe" "$basedir/../crc-32/bin/crc32.njs" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/../crc-32/bin/crc32.njs" $args
} else {
& "node$exe" "$basedir/../crc-32/bin/crc32.njs" $args
}
$ret=$LASTEXITCODE
}
exit $ret

View File

@@ -0,0 +1,16 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../mkdirp/bin/cmd.js" "$@"
else
exec node "$basedir/../mkdirp/bin/cmd.js" "$@"
fi

View File

@@ -0,0 +1,17 @@
@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\mkdirp\bin\cmd.js" %*

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/../mkdirp/bin/cmd.js" $args
} else {
& "$basedir/node$exe" "$basedir/../mkdirp/bin/cmd.js" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/../mkdirp/bin/cmd.js" $args
} else {
& "node$exe" "$basedir/../mkdirp/bin/cmd.js" $args
}
$ret=$LASTEXITCODE
}
exit $ret

View File

@@ -0,0 +1,16 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../rimraf/bin.js" "$@"
else
exec node "$basedir/../rimraf/bin.js" "$@"
fi

View File

@@ -0,0 +1,17 @@
@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\rimraf\bin.js" %*

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/../rimraf/bin.js" $args
} else {
& "$basedir/node$exe" "$basedir/../rimraf/bin.js" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/../rimraf/bin.js" $args
} else {
& "node$exe" "$basedir/../rimraf/bin.js" $args
}
$ret=$LASTEXITCODE
}
exit $ret

View File

@@ -0,0 +1,16 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../uuid/dist/bin/uuid" "$@"
else
exec node "$basedir/../uuid/dist/bin/uuid" "$@"
fi

View File

@@ -0,0 +1,17 @@
@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\uuid\dist\bin\uuid" %*

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/../uuid/dist/bin/uuid" $args
} else {
& "$basedir/node$exe" "$basedir/../uuid/dist/bin/uuid" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/../uuid/dist/bin/uuid" $args
} else {
& "node$exe" "$basedir/../uuid/dist/bin/uuid" $args
}
$ret=$LASTEXITCODE
}
exit $ret

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,73 @@
# 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

@@ -0,0 +1,21 @@
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

@@ -0,0 +1,20 @@
<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

@@ -0,0 +1,13 @@
/// <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

@@ -0,0 +1,63 @@
"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

@@ -0,0 +1 @@
{"version":3,"file":"CsvFormatterStream.js","sourceRoot":"","sources":["../../src/CsvFormatterStream.ts"],"names":[],"mappings":";;;AAAA,mCAAsD;AAGtD,2CAA2C;AAE3C,MAAa,kBAAiD,SAAQ,kBAAS;IAO3E,YAAmB,gBAAwC;QACvD,KAAK,CAAC,EAAE,kBAAkB,EAAE,gBAAgB,CAAC,UAAU,EAAE,CAAC,CAAC;QAHvD,kBAAa,GAAG,KAAK,CAAC;QAI1B,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,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,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

@@ -0,0 +1,39 @@
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

@@ -0,0 +1,38 @@
"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

@@ -0,0 +1 @@
{"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,iBAAY,GAAiB,IAAI,CAAC,YAAY,CAAC;QAE/C,YAAO,GAAoB,IAAI,CAAC;QAEhC,2BAAsB,GAAY,KAAK,CAAC;QAMxC,aAAQ,GAAY,KAAK,CAAC;QAI1B,QAAG,GAAW,QAAQ,CAAC;QAEvB,uBAAkB,GAAY,KAAK,CAAC;QAGhD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;QAEhC,IAAI,QAAO,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,YAAY,CAAA,KAAK,WAAW,EAAE;YAC3C,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;SACzC;QACD,IAAI,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,MAAK,IAAI,EAAE;YACtB,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC;SACpB;aAAM,IAAI,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,MAAK,KAAK,EAAE;YAC9B,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;SACnB;QACD,IAAI,QAAO,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,MAAM,CAAA,KAAK,QAAQ,EAAE;YAClC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC;SAC5B;QACD,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,OAAC,IAAI,CAAC,YAAY,mCAAI,IAAI,CAAC,CAAC;QACxE,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;QACjE,IAAI,CAAC,YAAY,GAAG,GAAG,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;IACtD,CAAC;CACJ;AAjDD,4CAiDC"}

View File

@@ -0,0 +1,13 @@
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

@@ -0,0 +1,58 @@
"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

@@ -0,0 +1 @@
{"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ACjB,CAAC;IAEM,MAAM,CAAC,KAAa,EAAE,UAAkB,EAAE,QAAiB;QAC9D,MAAM,aAAa,GAAG,GAAG,sBAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACxE,MAAM,EAAE,gBAAgB,EAAE,GAAG,IAAI,CAAC;QAClC,IAAI,gBAAgB,CAAC,KAAK,KAAK,EAAE,EAAE;YAC/B,MAAM,YAAY,GAAG,aAAa,CAAC,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;YAC1E,IAAI,YAAY,EAAE;gBACd,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC;aACrG;SACJ;QACD,MAAM,mBAAmB,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;QAC5E,IAAI,mBAAmB,IAAI,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,QAAQ,CAAC,EAAE;YAC/D,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;SACzC;QACD,OAAO,aAAa,CAAC;IACzB,CAAC;IAEO,UAAU,CAAC,KAAa;QAC5B,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,gBAAgB,CAAC;QACxC,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,EAAE,CAAC;IACtC,CAAC;CACJ;AAzDD,wCAyDC"}

View File

@@ -0,0 +1,25 @@
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

@@ -0,0 +1,168 @@
"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

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

View File

@@ -0,0 +1,8 @@
"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

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

@@ -0,0 +1,14 @@
/// <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

@@ -0,0 +1,68 @@
"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

@@ -0,0 +1 @@
{"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,2DAA0D;AAAjD,wHAAA,kBAAkB,OAAA;AAC3B,uDAA4E;AAAnE,oHAAA,gBAAgB,OAAA;AAEZ,QAAA,MAAM,GAAG,CAA+B,OAAoC,EAA4B,EAAE,CACnH,IAAI,uCAAkB,CAAC,IAAI,mCAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAE7C,QAAA,KAAK,GAAG,CACjB,IAAS,EACT,OAAoC,EAC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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

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

View File

@@ -0,0 +1,6 @@
"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

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

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

View File

@@ -0,0 +1,87 @@
# 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.6](https://github.com/C2FO/fast-csv/compare/v4.3.5...v4.3.6) (2020-12-04)
### Bug Fixes
* Simplify empty row check by removing complex regex ([4bbd39f](https://github.com/C2FO/fast-csv/commit/4bbd39f26a8cd7382151ab4f5fb102234b2f829e))
## [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/parse
## [4.3.2](https://github.com/C2FO/fast-csv/compare/v4.3.1...v4.3.2) (2020-09-02)
### Bug Fixes
* **parsing, #423:** Prevent callback from being called multiple times ([040febe](https://github.com/C2FO/fast-csv/commit/040febe17f5fe763a00f45b1d83c5acd47bbbe0b)), closes [#423](https://github.com/C2FO/fast-csv/issues/423)
## [4.3.1](https://github.com/C2FO/fast-csv/compare/v4.3.0...v4.3.1) (2020-06-23)
### Bug Fixes
* **parsing:** Pass errors through callbacks ([84ecdf6](https://github.com/C2FO/fast-csv/commit/84ecdf6ed18b15d68b4ed3e2bfec7eb41b438ad8))
# [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/parse
# [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.6](https://github.com/C2FO/fast-csv/compare/v4.1.5...v4.1.6) (2020-05-15)
### Bug Fixes
* **parse:** Handle escaped escape properly [#340](https://github.com/C2FO/fast-csv/issues/340) ([78d9b16](https://github.com/C2FO/fast-csv/commit/78d9b160152ee399f31086cc6b5f66a7ca7f9e24))
## [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/parse
## [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/parse

View File

@@ -0,0 +1,21 @@
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

@@ -0,0 +1,20 @@
<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/parse.svg)](https://www.npmjs.org/package/@fast-csv/parse)
[![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/parse/package.json)](https://snyk.io/test/github/C2FO/fast-csv?targetFile=packages/parse/package.json)
# `@fast-csv/parse`
`fast-csv` package to parse CSVs.
## Installation
[Install Guide](https://c2fo.io/fast-csv/docs/introduction/install)
## Usage
To get started with `@fast-csv/parse` [check out the docs](https://c2fo.io/fast-csv/docs/parsing/getting-started)

View File

@@ -0,0 +1,33 @@
/// <reference types="node" />
import { Transform, TransformCallback } from 'stream';
import { ParserOptions } from './ParserOptions';
import { Row, RowTransformFunction, RowValidate } from './types';
export declare class CsvParserStream<I extends Row, O extends Row> extends Transform {
private readonly parserOptions;
private readonly decoder;
private readonly parser;
private readonly headerTransformer;
private readonly rowTransformerValidator;
private lines;
private rowCount;
private parsedRowCount;
private parsedLineCount;
private endEmitted;
private headersEmitted;
constructor(parserOptions: ParserOptions);
private get hasHitRowLimit();
private get shouldEmitRows();
private get shouldSkipLine();
transform(transformFunction: RowTransformFunction<I, O>): CsvParserStream<I, O>;
validate(validateFunction: RowValidate<O>): CsvParserStream<I, O>;
emit(event: string | symbol, ...rest: any[]): boolean;
_transform(data: Buffer, encoding: string, done: TransformCallback): void;
_flush(done: TransformCallback): void;
private parse;
private processRows;
private transformRow;
private checkAndEmitHeaders;
private skipRow;
private pushRow;
private static wrapDoneCallback;
}

View File

@@ -0,0 +1,212 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsvParserStream = void 0;
const string_decoder_1 = require("string_decoder");
const stream_1 = require("stream");
const transforms_1 = require("./transforms");
const parser_1 = require("./parser");
class CsvParserStream extends stream_1.Transform {
constructor(parserOptions) {
super({ objectMode: parserOptions.objectMode });
this.lines = '';
this.rowCount = 0;
this.parsedRowCount = 0;
this.parsedLineCount = 0;
this.endEmitted = false;
this.headersEmitted = false;
this.parserOptions = parserOptions;
this.parser = new parser_1.Parser(parserOptions);
this.headerTransformer = new transforms_1.HeaderTransformer(parserOptions);
this.decoder = new string_decoder_1.StringDecoder(parserOptions.encoding);
this.rowTransformerValidator = new transforms_1.RowTransformerValidator();
}
get hasHitRowLimit() {
return this.parserOptions.limitRows && this.rowCount >= this.parserOptions.maxRows;
}
get shouldEmitRows() {
return this.parsedRowCount > this.parserOptions.skipRows;
}
get shouldSkipLine() {
return this.parsedLineCount <= this.parserOptions.skipLines;
}
transform(transformFunction) {
this.rowTransformerValidator.rowTransform = transformFunction;
return this;
}
validate(validateFunction) {
this.rowTransformerValidator.rowValidator = validateFunction;
return this;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
emit(event, ...rest) {
if (event === 'end') {
if (!this.endEmitted) {
this.endEmitted = true;
super.emit('end', this.rowCount);
}
return false;
}
return super.emit(event, ...rest);
}
_transform(data, encoding, done) {
// if we have hit our maxRows parsing limit then skip parsing
if (this.hasHitRowLimit) {
return done();
}
const wrappedCallback = CsvParserStream.wrapDoneCallback(done);
try {
const { lines } = this;
const newLine = lines + this.decoder.write(data);
const rows = this.parse(newLine, true);
return this.processRows(rows, wrappedCallback);
}
catch (e) {
return wrappedCallback(e);
}
}
_flush(done) {
const wrappedCallback = CsvParserStream.wrapDoneCallback(done);
// if we have hit our maxRows parsing limit then skip parsing
if (this.hasHitRowLimit) {
return wrappedCallback();
}
try {
const newLine = this.lines + this.decoder.end();
const rows = this.parse(newLine, false);
return this.processRows(rows, wrappedCallback);
}
catch (e) {
return wrappedCallback(e);
}
}
parse(data, hasMoreData) {
if (!data) {
return [];
}
const { line, rows } = this.parser.parse(data, hasMoreData);
this.lines = line;
return rows;
}
processRows(rows, cb) {
const rowsLength = rows.length;
const iterate = (i) => {
const callNext = (err) => {
if (err) {
return cb(err);
}
if (i % 100 === 0) {
// incase the transform are sync insert a next tick to prevent stack overflow
setImmediate(() => iterate(i + 1));
return undefined;
}
return iterate(i + 1);
};
this.checkAndEmitHeaders();
// if we have emitted all rows or we have hit the maxRows limit option
// then end
if (i >= rowsLength || this.hasHitRowLimit) {
return cb();
}
this.parsedLineCount += 1;
if (this.shouldSkipLine) {
return callNext();
}
const row = rows[i];
this.rowCount += 1;
this.parsedRowCount += 1;
const nextRowCount = this.rowCount;
return this.transformRow(row, (err, transformResult) => {
if (err) {
this.rowCount -= 1;
return callNext(err);
}
if (!transformResult) {
return callNext(new Error('expected transform result'));
}
if (!transformResult.isValid) {
this.emit('data-invalid', transformResult.row, nextRowCount, transformResult.reason);
}
else if (transformResult.row) {
return this.pushRow(transformResult.row, callNext);
}
return callNext();
});
};
iterate(0);
}
transformRow(parsedRow, cb) {
try {
this.headerTransformer.transform(parsedRow, (err, withHeaders) => {
if (err) {
return cb(err);
}
if (!withHeaders) {
return cb(new Error('Expected result from header transform'));
}
if (!withHeaders.isValid) {
if (this.shouldEmitRows) {
return cb(null, { isValid: false, row: parsedRow });
}
// skipped because of skipRows option remove from total row count
return this.skipRow(cb);
}
if (withHeaders.row) {
if (this.shouldEmitRows) {
return this.rowTransformerValidator.transformAndValidate(withHeaders.row, cb);
}
// skipped because of skipRows option remove from total row count
return this.skipRow(cb);
}
// this is a header row dont include in the rowCount or parsedRowCount
this.rowCount -= 1;
this.parsedRowCount -= 1;
return cb(null, { row: null, isValid: true });
});
}
catch (e) {
cb(e);
}
}
checkAndEmitHeaders() {
if (!this.headersEmitted && this.headerTransformer.headers) {
this.headersEmitted = true;
this.emit('headers', this.headerTransformer.headers);
}
}
skipRow(cb) {
// skipped because of skipRows option remove from total row count
this.rowCount -= 1;
return cb(null, { row: null, isValid: true });
}
pushRow(row, cb) {
try {
if (!this.parserOptions.objectMode) {
this.push(JSON.stringify(row));
}
else {
this.push(row);
}
cb();
}
catch (e) {
cb(e);
}
}
static wrapDoneCallback(done) {
let errorCalled = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (err, ...args) => {
if (err) {
if (errorCalled) {
throw err;
}
errorCalled = true;
done(err);
return;
}
done(...args);
};
}
}
exports.CsvParserStream = CsvParserStream;
//# sourceMappingURL=CsvParserStream.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,47 @@
/// <reference types="node" />
import { HeaderArray, HeaderTransformFunction } from './types';
export interface ParserOptionsArgs {
objectMode?: boolean;
delimiter?: string;
quote?: string | null;
escape?: string;
headers?: boolean | HeaderTransformFunction | HeaderArray;
renameHeaders?: boolean;
ignoreEmpty?: boolean;
comment?: string;
strictColumnHandling?: boolean;
discardUnmappedColumns?: boolean;
trim?: boolean;
ltrim?: boolean;
rtrim?: boolean;
encoding?: string;
maxRows?: number;
skipLines?: number;
skipRows?: number;
}
export declare class ParserOptions {
readonly escapedDelimiter: string;
readonly objectMode: boolean;
readonly delimiter: string;
readonly ignoreEmpty: boolean;
readonly quote: string | null;
readonly escape: string | null;
readonly escapeChar: string | null;
readonly comment: string | null;
readonly supportsComments: boolean;
readonly ltrim: boolean;
readonly rtrim: boolean;
readonly trim: boolean;
readonly headers: boolean | HeaderTransformFunction | HeaderArray | null;
readonly renameHeaders: boolean;
readonly strictColumnHandling: boolean;
readonly discardUnmappedColumns: boolean;
readonly carriageReturn: string;
readonly NEXT_TOKEN_REGEXP: RegExp;
readonly encoding: BufferEncoding;
readonly limitRows: boolean;
readonly maxRows: number;
readonly skipLines: number;
readonly skipRows: number;
constructor(opts?: ParserOptionsArgs);
}

View File

@@ -0,0 +1,47 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParserOptions = void 0;
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;
this.delimiter = ',';
this.ignoreEmpty = false;
this.quote = '"';
this.escape = null;
this.escapeChar = this.quote;
this.comment = null;
this.supportsComments = false;
this.ltrim = false;
this.rtrim = false;
this.trim = false;
this.headers = null;
this.renameHeaders = false;
this.strictColumnHandling = false;
this.discardUnmappedColumns = false;
this.carriageReturn = '\r';
this.encoding = 'utf8';
this.limitRows = false;
this.maxRows = 0;
this.skipLines = 0;
this.skipRows = 0;
Object.assign(this, opts || {});
if (this.delimiter.length > 1) {
throw new Error('delimiter option must be one character long');
}
this.escapedDelimiter = lodash_escaperegexp_1.default(this.delimiter);
this.escapeChar = (_a = this.escape) !== 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})`);
if (this.maxRows > 0) {
this.limitRows = true;
}
}
}
exports.ParserOptions = ParserOptions;
//# sourceMappingURL=ParserOptions.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"ParserOptions.js","sourceRoot":"","sources":["../../src/ParserOptions.ts"],"names":[],"mappings":";;;;;;AAAA,8EAA+C;AAC/C,gEAAiC;AAuBjC,MAAa,aAAa;IA+CtB,YAAmB,IAAwB;;QA5C3B,eAAU,GAAY,IAAI,CAAC;QAE3B,cAAS,GAAW,GAAG,CAAC;QAExB,gBAAW,GAAY,KAAK,CAAC;QAE7B,UAAK,GAAkB,GAAG,CAAC;QAE3B,WAAM,GAAkB,IAAI,CAAC;QAE7B,eAAU,GAAkB,IAAI,CAAC,KAAK,CAAC;QAEvC,YAAO,GAAkB,IAAI,CAAC;QAE9B,qBAAgB,GAAY,KAAK,CAAC;QAElC,UAAK,GAAY,KAAK,CAAC;QAEvB,UAAK,GAAY,KAAK,CAAC;QAEvB,SAAI,GAAY,KAAK,CAAC;QAEtB,YAAO,GAA2D,IAAI,CAAC;QAEvE,kBAAa,GAAY,KAAK,CAAC;QAE/B,yBAAoB,GAAY,KAAK,CAAC;QAEtC,2BAAsB,GAAY,KAAK,CAAC;QAExC,mBAAc,GAAW,IAAI,CAAC;QAI9B,aAAQ,GAAmB,MAAM,CAAC;QAElC,cAAS,GAAY,KAAK,CAAC;QAE3B,YAAO,GAAW,CAAC,CAAC;QAEpB,cAAS,GAAW,CAAC,CAAC;QAEtB,aAAQ,GAAW,CAAC,CAAC;QAGjC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;QAChC,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;SAClE;QACD,IAAI,CAAC,gBAAgB,GAAG,6BAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACrD,IAAI,CAAC,UAAU,SAAG,IAAI,CAAC,MAAM,mCAAI,IAAI,CAAC,KAAK,CAAC;QAC5C,IAAI,CAAC,gBAAgB,GAAG,CAAC,sBAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC7C,IAAI,CAAC,iBAAiB,GAAG,IAAI,MAAM,CAAC,0BAA0B,IAAI,CAAC,gBAAgB,GAAG,CAAC,CAAC;QAExF,IAAI,IAAI,CAAC,OAAO,GAAG,CAAC,EAAE;YAClB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;SACzB;IACL,CAAC;CACJ;AA7DD,sCA6DC"}

View File

@@ -0,0 +1,11 @@
/// <reference types="node" />
import { ParserOptionsArgs } from './ParserOptions';
import { CsvParserStream } from './CsvParserStream';
import { Row } from './types';
export * from './types';
export { CsvParserStream } from './CsvParserStream';
export { ParserOptions, ParserOptionsArgs } from './ParserOptions';
export declare const parse: <I extends Row<any>, O extends Row<any>>(args?: ParserOptionsArgs | undefined) => CsvParserStream<I, O>;
export declare const parseStream: <I extends Row<any>, O extends Row<any>>(stream: NodeJS.ReadableStream, options?: ParserOptionsArgs | undefined) => CsvParserStream<I, O>;
export declare const parseFile: <I extends Row<any>, O extends Row<any>>(location: string, options?: ParserOptionsArgs) => CsvParserStream<I, O>;
export declare const parseString: <I extends Row<any>, O extends Row<any>>(string: string, options?: ParserOptionsArgs | undefined) => CsvParserStream<I, O>;

View File

@@ -0,0 +1,44 @@
"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.parseString = exports.parseFile = exports.parseStream = exports.parse = exports.ParserOptions = exports.CsvParserStream = void 0;
const fs = __importStar(require("fs"));
const stream_1 = require("stream");
const ParserOptions_1 = require("./ParserOptions");
const CsvParserStream_1 = require("./CsvParserStream");
__exportStar(require("./types"), exports);
var CsvParserStream_2 = require("./CsvParserStream");
Object.defineProperty(exports, "CsvParserStream", { enumerable: true, get: function () { return CsvParserStream_2.CsvParserStream; } });
var ParserOptions_2 = require("./ParserOptions");
Object.defineProperty(exports, "ParserOptions", { enumerable: true, get: function () { return ParserOptions_2.ParserOptions; } });
exports.parse = (args) => new CsvParserStream_1.CsvParserStream(new ParserOptions_1.ParserOptions(args));
exports.parseStream = (stream, options) => stream.pipe(new CsvParserStream_1.CsvParserStream(new ParserOptions_1.ParserOptions(options)));
exports.parseFile = (location, options = {}) => fs.createReadStream(location).pipe(new CsvParserStream_1.CsvParserStream(new ParserOptions_1.ParserOptions(options)));
exports.parseString = (string, options) => {
const rs = new stream_1.Readable();
rs.push(string);
rs.push(null);
return rs.pipe(new CsvParserStream_1.CsvParserStream(new ParserOptions_1.ParserOptions(options)));
};
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAyB;AACzB,mCAAkC;AAClC,mDAAmE;AACnE,uDAAoD;AAGpD,0CAAwB;AACxB,qDAAoD;AAA3C,kHAAA,eAAe,OAAA;AACxB,iDAAmE;AAA1D,8GAAA,aAAa,OAAA;AAET,QAAA,KAAK,GAAG,CAA+B,IAAwB,EAAyB,EAAE,CACnG,IAAI,iCAAe,CAAC,IAAI,6BAAa,CAAC,IAAI,CAAC,CAAC,CAAC;AAEpC,QAAA,WAAW,GAAG,CACvB,MAA6B,EAC7B,OAA2B,EACN,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,iCAAe,CAAC,IAAI,6BAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAE5E,QAAA,SAAS,GAAG,CACrB,QAAgB,EAChB,UAA6B,EAAE,EACV,EAAE,CAAC,EAAE,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,iCAAe,CAAC,IAAI,6BAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAEnG,QAAA,WAAW,GAAG,CACvB,MAAc,EACd,OAA2B,EACN,EAAE;IACvB,MAAM,EAAE,GAAG,IAAI,iBAAQ,EAAE,CAAC;IAC1B,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAChB,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,iCAAe,CAAC,IAAI,6BAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AACpE,CAAC,CAAC"}

View File

@@ -0,0 +1,15 @@
import { ParserOptions } from '../ParserOptions';
export interface ParseResult {
line: string;
rows: string[][];
}
export declare class Parser {
private static removeBOM;
private readonly parserOptions;
private readonly rowParser;
constructor(parserOptions: ParserOptions);
parse(line: string, hasMoreData: boolean): ParseResult;
private parseWithoutComments;
private parseWithComments;
private parseRow;
}

View File

@@ -0,0 +1,76 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = void 0;
const Scanner_1 = require("./Scanner");
const RowParser_1 = require("./RowParser");
const Token_1 = require("./Token");
class Parser {
constructor(parserOptions) {
this.parserOptions = parserOptions;
this.rowParser = new RowParser_1.RowParser(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) {
return line.slice(1);
}
return line;
}
parse(line, hasMoreData) {
const scanner = new Scanner_1.Scanner({
line: Parser.removeBOM(line),
parserOptions: this.parserOptions,
hasMoreData,
});
if (this.parserOptions.supportsComments) {
return this.parseWithComments(scanner);
}
return this.parseWithoutComments(scanner);
}
parseWithoutComments(scanner) {
const rows = [];
let shouldContinue = true;
while (shouldContinue) {
shouldContinue = this.parseRow(scanner, rows);
}
return { line: scanner.line, rows };
}
parseWithComments(scanner) {
const { parserOptions } = this;
const rows = [];
for (let nextToken = scanner.nextCharacterToken; nextToken !== null; nextToken = scanner.nextCharacterToken) {
if (Token_1.Token.isTokenComment(nextToken, parserOptions)) {
const cursor = scanner.advancePastLine();
if (cursor === null) {
return { line: scanner.lineFromCursor, rows };
}
if (!scanner.hasMoreCharacters) {
return { line: scanner.lineFromCursor, rows };
}
scanner.truncateToCursor();
}
else if (!this.parseRow(scanner, rows)) {
break;
}
}
return { line: scanner.line, rows };
}
parseRow(scanner, rows) {
const nextToken = scanner.nextNonSpaceToken;
if (!nextToken) {
return false;
}
const row = this.rowParser.parse(scanner);
if (row === null) {
return false;
}
if (this.parserOptions.ignoreEmpty && RowParser_1.RowParser.isEmptyRow(row)) {
return true;
}
rows.push(row);
return true;
}
}
exports.Parser = Parser;
//# sourceMappingURL=Parser.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"Parser.js","sourceRoot":"","sources":["../../../src/parser/Parser.ts"],"names":[],"mappings":";;;AAAA,uCAAoC;AACpC,2CAAwC;AAGxC,mCAAgC;AAMhC,MAAa,MAAM;IAcf,YAAmB,aAA4B;QAC3C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,SAAS,GAAG,IAAI,qBAAS,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACvD,CAAC;IAhBO,MAAM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CAAC,eAAe,EAAE,CAAC;gBACzC,IAAI,MAAM,KAAK,IAAI,EAAE;oBACjB,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,cAAc,EAAE,IAAI,EAAE,CAAC;iBACjD;gBACD,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE;oBAC5B,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,cAAc,EAAE,IAAI,EAAE,CAAC;iBACjD;gBACD,OAAO,CAAC,gBAAgB,EAAE,CAAC;aAC9B;iBAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE;gBACtC,MAAM;aACT;SACJ;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC;IACxC,CAAC;IAEO,QAAQ,CAAC,OAAgB,EAAE,IAAgB;QAC/C,MAAM,SAAS,GAAG,OAAO,CAAC,iBAAiB,CAAC;QAC5C,IAAI,CAAC,SAAS,EAAE;YACZ,OAAO,KAAK,CAAC;SAChB;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,GAAG,KAAK,IAAI,EAAE;YACd,OAAO,KAAK,CAAC;SAChB;QACD,IAAI,IAAI,CAAC,aAAa,CAAC,WAAW,IAAI,qBAAS,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YAC7D,OAAO,IAAI,CAAC;SACf;QACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AA3ED,wBA2EC"}

View File

@@ -0,0 +1,12 @@
import { Scanner } from './Scanner';
import { ParserOptions } from '../ParserOptions';
import { RowArray } from '../types';
export declare class RowParser {
static isEmptyRow(row: RowArray): boolean;
private readonly parserOptions;
private readonly columnParser;
constructor(parserOptions: ParserOptions);
parse(scanner: Scanner): RowArray | null;
private getStartToken;
private shouldSkipColumnParse;
}

View File

@@ -0,0 +1,76 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RowParser = void 0;
const column_1 = require("./column");
const Token_1 = require("./Token");
const EMPTY_STRING = '';
class RowParser {
constructor(parserOptions) {
this.parserOptions = parserOptions;
this.columnParser = new column_1.ColumnParser(parserOptions);
}
static isEmptyRow(row) {
return row.join(EMPTY_STRING).replace(/\s+/g, EMPTY_STRING) === EMPTY_STRING;
}
parse(scanner) {
const { parserOptions } = this;
const { hasMoreData } = scanner;
const currentScanner = scanner;
const columns = [];
let currentToken = this.getStartToken(currentScanner, columns);
while (currentToken) {
if (Token_1.Token.isTokenRowDelimiter(currentToken)) {
currentScanner.advancePastToken(currentToken);
// if ends with CR and there is more data, keep unparsed due to possible
// coming LF in CRLF
if (!currentScanner.hasMoreCharacters &&
Token_1.Token.isTokenCarriageReturn(currentToken, parserOptions) &&
hasMoreData) {
return null;
}
currentScanner.truncateToCursor();
return columns;
}
if (!this.shouldSkipColumnParse(currentScanner, currentToken, columns)) {
const item = this.columnParser.parse(currentScanner);
if (item === null) {
return null;
}
columns.push(item);
}
currentToken = currentScanner.nextNonSpaceToken;
}
if (!hasMoreData) {
currentScanner.truncateToCursor();
return columns;
}
return null;
}
getStartToken(scanner, columns) {
const currentToken = scanner.nextNonSpaceToken;
if (currentToken !== null && Token_1.Token.isTokenDelimiter(currentToken, this.parserOptions)) {
columns.push('');
return scanner.nextNonSpaceToken;
}
return currentToken;
}
shouldSkipColumnParse(scanner, currentToken, columns) {
const { parserOptions } = this;
if (Token_1.Token.isTokenDelimiter(currentToken, parserOptions)) {
scanner.advancePastToken(currentToken);
// if the delimiter is at the end of a line
const nextToken = scanner.nextCharacterToken;
if (!scanner.hasMoreCharacters || (nextToken !== null && Token_1.Token.isTokenRowDelimiter(nextToken))) {
columns.push('');
return true;
}
if (nextToken !== null && Token_1.Token.isTokenDelimiter(nextToken, parserOptions)) {
columns.push('');
return true;
}
}
return false;
}
}
exports.RowParser = RowParser;
//# sourceMappingURL=RowParser.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"RowParser.js","sourceRoot":"","sources":["../../../src/parser/RowParser.ts"],"names":[],"mappings":";;;AACA,qCAAwC;AAGxC,mCAA4C;AAE5C,MAAM,YAAY,GAAG,EAAE,CAAC;AAExB,MAAa,SAAS;IASlB,YAAmB,aAA4B;QAC3C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,YAAY,GAAG,IAAI,qBAAY,CAAC,aAAa,CAAC,CAAC;IACxD,CAAC;IAXD,MAAM,CAAC,UAAU,CAAC,GAAa;QAC3B,OAAO,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,KAAK,YAAY,CAAC;IACjF,CAAC;IAWM,KAAK,CAAC,OAAgB;QACzB,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC;QAC/B,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;QAChC,MAAM,cAAc,GAAG,OAAO,CAAC;QAC/B,MAAM,OAAO,GAAqB,EAAE,CAAC;QACrC,IAAI,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;QAC/D,OAAO,YAAY,EAAE;YACjB,IAAI,aAAK,CAAC,mBAAmB,CAAC,YAAY,CAAC,EAAE;gBACzC,cAAc,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAC9C,wEAAwE;gBACxE,oBAAoB;gBACpB,IACI,CAAC,cAAc,CAAC,iBAAiB;oBACjC,aAAK,CAAC,qBAAqB,CAAC,YAAY,EAAE,aAAa,CAAC;oBACxD,WAAW,EACb;oBACE,OAAO,IAAI,CAAC;iBACf;gBACD,cAAc,CAAC,gBAAgB,EAAE,CAAC;gBAClC,OAAO,OAAO,CAAC;aAClB;YACD,IAAI,CAAC,IAAI,CAAC,qBAAqB,CAAC,cAAc,EAAE,YAAY,EAAE,OAAO,CAA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AAI,IAAI,aAAK,CAAC,gBAAgB,CAAC,SAAS,EAAE,aAAa,CAAC,EAAE;gBACxE,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBACjB,OAAO,IAAI,CAAC;aACf;SACJ;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ;AA7ED,8BA6EC"}

View File

@@ -0,0 +1,25 @@
import { ParserOptions } from '../ParserOptions';
import { MaybeToken, Token } from './Token';
export interface ScannerArgs {
line: string;
parserOptions: ParserOptions;
hasMoreData: boolean;
cursor?: number;
}
export declare class Scanner {
line: string;
private readonly parserOptions;
lineLength: number;
readonly hasMoreData: boolean;
cursor: number;
constructor(args: ScannerArgs);
get hasMoreCharacters(): boolean;
get nextNonSpaceToken(): MaybeToken;
get nextCharacterToken(): MaybeToken;
get lineFromCursor(): string;
advancePastLine(): Scanner | null;
advanceTo(cursor: number): Scanner;
advanceToToken(token: Token): Scanner;
advancePastToken(token: Token): Scanner;
truncateToCursor(): Scanner;
}

View File

@@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scanner = void 0;
const Token_1 = require("./Token");
const ROW_DELIMITER = /((?:\r\n)|\n|\r)/;
class Scanner {
constructor(args) {
this.cursor = 0;
this.line = args.line;
this.lineLength = this.line.length;
this.parserOptions = args.parserOptions;
this.hasMoreData = args.hasMoreData;
this.cursor = args.cursor || 0;
}
get hasMoreCharacters() {
return this.lineLength > this.cursor;
}
get nextNonSpaceToken() {
const { lineFromCursor } = this;
const regex = this.parserOptions.NEXT_TOKEN_REGEXP;
if (lineFromCursor.search(regex) === -1) {
return null;
}
const match = regex.exec(lineFromCursor);
if (match == null) {
return null;
}
const token = match[1];
const startCursor = this.cursor + (match.index || 0);
return new Token_1.Token({
token,
startCursor,
endCursor: startCursor + token.length - 1,
});
}
get nextCharacterToken() {
const { cursor, lineLength } = this;
if (lineLength <= cursor) {
return null;
}
return new Token_1.Token({
token: this.line[cursor],
startCursor: cursor,
endCursor: cursor,
});
}
get lineFromCursor() {
return this.line.substr(this.cursor);
}
advancePastLine() {
const match = ROW_DELIMITER.exec(this.lineFromCursor);
if (!match) {
if (this.hasMoreData) {
return null;
}
this.cursor = this.lineLength;
return this;
}
this.cursor += (match.index || 0) + match[0].length;
return this;
}
advanceTo(cursor) {
this.cursor = cursor;
return this;
}
advanceToToken(token) {
this.cursor = token.startCursor;
return this;
}
advancePastToken(token) {
this.cursor = token.endCursor + 1;
return this;
}
truncateToCursor() {
this.line = this.lineFromCursor;
this.lineLength = this.line.length;
this.cursor = 0;
return this;
}
}
exports.Scanner = Scanner;
//# sourceMappingURL=Scanner.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"Scanner.js","sourceRoot":"","sources":["../../../src/parser/Scanner.ts"],"names":[],"mappings":";;;AACA,mCAA4C;AAE5C,MAAM,aAAa,GAAG,kBAAkB,CAAC;AASzC,MAAa,OAAO;IAWhB,YAAmB,IAAiB;QAF7B,WAAM,GAAG,CAAC,CAAC;QAGd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;QACnC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QACxC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;IACnC,CAAC;IAED,IAAW,iBAAiB;QACxB,OAAO,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC;IACzC,CAAC;IAED,IAAW,iBAAiB;QACxB,MAAM,EAAE,cAAc,EAAE,GAAG,IAAI,CAAC;QAChC,MAAM,KAAK,GAAG,IAAI,CAAC,aAAa,CAAC,iBAAiB,CAAC;QACnD,IAAI,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE;YACrC,OAAO,I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QACpD,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,SAAS,CAAC,MAAc;QAC3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,cAAc,CAAC,KAAY;QAC9B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC;QAChC,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,gBAAgB,CAAC,KAAY;QAChC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC;QAClC,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,gBAAgB;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,cAAc,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAChB,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AA5FD,0BA4FC"}

View File

@@ -0,0 +1,19 @@
import { ParserOptions } from '../ParserOptions';
export declare type MaybeToken = Token | null;
export interface TokenArgs {
token: string;
startCursor: number;
endCursor: number;
}
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);
}

View File

@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Token = void 0;
class Token {
constructor(tokenArgs) {
this.token = tokenArgs.token;
this.startCursor = tokenArgs.startCursor;
this.endCursor = tokenArgs.endCursor;
}
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;
}
}
exports.Token = Token;
//# sourceMappingURL=Token.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"Token.js","sourceRoot":"","sources":["../../../src/parser/Token.ts"],"names":[],"mappings":";;;AAUA,MAAa,KAAK;IAgCd,YAAmB,SAAoB;QACnC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC;QAC7B,IAAI,CAAC,WAAW,GAAG,SAAS,CAAC,WAAW,CAAC;QACzC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;IACzC,CAAC;IAnCM,MAAM,CAAC,mBAAmB,CAAC,KAAY;QAC1C,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC;QAC5B,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,MAAM,CAAC;IACtE,CAAC;IAEM,MAAM,CAAC,qBAAqB,CAAC,KAAY,EAAE,aAA4B;QAC1E,OAAO,KAAK,CAAC,KAAK,KAAK,aAAa,CAAC,cAAc,CAAC;IACxD,CAAC;IAEM,MAAM,CAAC,cAAc,CAAC,KAAY,EAAE,aAA4B;QACnE,OAAO,aAAa,CAAC,gBAAgB,IAAI,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,aAAa,CAAC,OAAO,CAAC;IAC9F,CAAC;IAEM,MAAM,CAAC,sBAAsB,CAAC,KAAY,EAAE,aAA4B;QAC3E,OAAO,KAAK,CAAC,KAAK,KAAK,aAAa,CAAC,UAAU,CAAC;IACpD,CAAC;IAEM,MAAM,CAAC,YAAY,CAAC,KAAY,EAAE,aAA4B;QACjE,OAAO,KAAK,CAAC,KAAK,KAAK,aAAa,CAAC,KAAK,CAAC;IAC/C,CAAC;IAEM,MAAM,CAAC,gBAAgB,CAAC,KAAY,EAAE,aAA4B;QACrE,OAAO,KAAK,CAAC,KAAK,KAAK,aAAa,CAAC,SAAS,CAAC;IACnD,CAAC;CAaJ;AArCD,sBAqCC"}

View File

@@ -0,0 +1,5 @@
import { ParserOptions } from '../../ParserOptions';
export declare class ColumnFormatter {
readonly format: (col: string) => string;
constructor(parserOptions: ParserOptions);
}

View File

@@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnFormatter = void 0;
class ColumnFormatter {
constructor(parserOptions) {
if (parserOptions.trim) {
this.format = (col) => col.trim();
}
else if (parserOptions.ltrim) {
this.format = (col) => col.trimLeft();
}
else if (parserOptions.rtrim) {
this.format = (col) => col.trimRight();
}
else {
this.format = (col) => col;
}
}
}
exports.ColumnFormatter = ColumnFormatter;
//# sourceMappingURL=ColumnFormatter.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"ColumnFormatter.js","sourceRoot":"","sources":["../../../../src/parser/column/ColumnFormatter.ts"],"names":[],"mappings":";;;AAEA,MAAa,eAAe;IAGxB,YAAmB,aAA4B;QAC3C,IAAI,aAAa,CAAC,IAAI,EAAE;YACpB,IAAI,CAAC,MAAM,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;SACrD;aAAM,IAAI,aAAa,CAAC,KAAK,EAAE;YAC5B,IAAI,CAAC,MAAM,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;SACzD;aAAM,IAAI,aAAa,CAAC,KAAK,EAAE;YAC5B,IAAI,CAAC,MAAM,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC;SAC1D;aAAM;YACH,IAAI,CAAC,MAAM,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,CAAC;SAC9C;IACL,CAAC;CACJ;AAdD,0CAcC"}

View File

@@ -0,0 +1,11 @@
import { ParserOptions } from '../../ParserOptions';
import { NonQuotedColumnParser } from './NonQuotedColumnParser';
import { QuotedColumnParser } from './QuotedColumnParser';
import { Scanner } from '../Scanner';
export declare class ColumnParser {
private readonly parserOptions;
readonly nonQuotedColumnParser: NonQuotedColumnParser;
readonly quotedColumnParser: QuotedColumnParser;
constructor(parserOptions: ParserOptions);
parse(scanner: Scanner): string | null;
}

View File

@@ -0,0 +1,23 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnParser = void 0;
const NonQuotedColumnParser_1 = require("./NonQuotedColumnParser");
const QuotedColumnParser_1 = require("./QuotedColumnParser");
const Token_1 = require("../Token");
class ColumnParser {
constructor(parserOptions) {
this.parserOptions = parserOptions;
this.quotedColumnParser = new QuotedColumnParser_1.QuotedColumnParser(parserOptions);
this.nonQuotedColumnParser = new NonQuotedColumnParser_1.NonQuotedColumnParser(parserOptions);
}
parse(scanner) {
const { nextNonSpaceToken } = scanner;
if (nextNonSpaceToken !== null && Token_1.Token.isTokenQuote(nextNonSpaceToken, this.parserOptions)) {
scanner.advanceToToken(nextNonSpaceToken);
return this.quotedColumnParser.parse(scanner);
}
return this.nonQuotedColumnParser.parse(scanner);
}
}
exports.ColumnParser = ColumnParser;
//# sourceMappingURL=ColumnParser.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"ColumnParser.js","sourceRoot":"","sources":["../../../../src/parser/column/ColumnParser.ts"],"names":[],"mappings":";;;AACA,mEAAgE;AAChE,6DAA0D;AAE1D,oCAAiC;AAEjC,MAAa,YAAY;IAOrB,YAAmB,aAA4B;QAC3C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,kBAAkB,GAAG,IAAI,uCAAkB,CAAC,aAAa,CAAC,CAAC;QAChE,IAAI,CAAC,qBAAqB,GAAG,IAAI,6CAAqB,CAAC,aAAa,CAAC,CAAC;IAC1E,CAAC;IAEM,KAAK,CAAC,OAAgB;QACzB,MAAM,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC;QACtC,IAAI,iBAAiB,KAAK,IAAI,IAAI,aAAK,CAAC,YAAY,CAAC,iBAAiB,EAAE,IAAI,CAAC,aAAa,CAAC,EAAE;YACzF,OAAO,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;YAC1C,OAAO,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;SACjD;QACD,OAAO,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACrD,CAAC;CACJ;AArBD,oCAqBC"}

View File

@@ -0,0 +1,8 @@
import { ParserOptions } from '../../ParserOptions';
import { Scanner } from '../Scanner';
export declare class NonQuotedColumnParser {
private readonly parserOptions;
private readonly columnFormatter;
constructor(parserOptions: ParserOptions);
parse(scanner: Scanner): string | null;
}

View File

@@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NonQuotedColumnParser = void 0;
const ColumnFormatter_1 = require("./ColumnFormatter");
const Token_1 = require("../Token");
class NonQuotedColumnParser {
constructor(parserOptions) {
this.parserOptions = parserOptions;
this.columnFormatter = new ColumnFormatter_1.ColumnFormatter(parserOptions);
}
parse(scanner) {
if (!scanner.hasMoreCharacters) {
return null;
}
const { parserOptions } = this;
const characters = [];
let nextToken = scanner.nextCharacterToken;
for (; nextToken; nextToken = scanner.nextCharacterToken) {
if (Token_1.Token.isTokenDelimiter(nextToken, parserOptions) || Token_1.Token.isTokenRowDelimiter(nextToken)) {
break;
}
characters.push(nextToken.token);
scanner.advancePastToken(nextToken);
}
return this.columnFormatter.format(characters.join(''));
}
}
exports.NonQuotedColumnParser = NonQuotedColumnParser;
//# sourceMappingURL=NonQuotedColumnParser.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"NonQuotedColumnParser.js","sourceRoot":"","sources":["../../../../src/parser/column/NonQuotedColumnParser.ts"],"names":[],"mappings":";;;AACA,uDAAoD;AAEpD,oCAAiC;AAEjC,MAAa,qBAAqB;IAK9B,YAAmB,aAA4B;QAC3C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,eAAe,GAAG,IAAI,iCAAe,CAAC,aAAa,CAAC,CAAC;IAC9D,CAAC;IAEM,KAAK,CAAC,OAAgB;QACzB,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE;YAC5B,OAAO,IAAI,CAAC;SACf;QACD,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC;QAC/B,MAAM,UAAU,GAAG,EAAE,CAAC;QACtB,IAAI,SAAS,GAAG,OAAO,CAAC,kBAAkB,CAAC;QAC3C,OAAO,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,kBAAkB,EAAE;YACtD,IAAI,aAAK,CAAC,gBAAgB,CAAC,SAAS,EAAE,aAAa,CAAC,IAAI,aAAK,CAAC,mBAAmB,CAAC,SAAS,CAAC,EAAE;gBAC1F,MAAM;aACT;YACD,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACjC,OAAO,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;SACvC;QACD,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IAC5D,CAAC;CACJ;AA1BD,sDA0BC"}

View File

@@ -0,0 +1,10 @@
import { ParserOptions } from '../../ParserOptions';
import { Scanner } from '../Scanner';
export declare class QuotedColumnParser {
private readonly parserOptions;
private readonly columnFormatter;
constructor(parserOptions: ParserOptions);
parse(scanner: Scanner): string | null;
private gatherDataBetweenQuotes;
private checkForMalformedColumn;
}

View File

@@ -0,0 +1,97 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuotedColumnParser = void 0;
const ColumnFormatter_1 = require("./ColumnFormatter");
const Token_1 = require("../Token");
class QuotedColumnParser {
constructor(parserOptions) {
this.parserOptions = parserOptions;
this.columnFormatter = new ColumnFormatter_1.ColumnFormatter(parserOptions);
}
parse(scanner) {
if (!scanner.hasMoreCharacters) {
return null;
}
const originalCursor = scanner.cursor;
const { foundClosingQuote, col } = this.gatherDataBetweenQuotes(scanner);
if (!foundClosingQuote) {
// reset the cursor to the original
scanner.advanceTo(originalCursor);
// if we didnt find a closing quote but we potentially have more data then skip the parsing
// and return the original scanner.
if (!scanner.hasMoreData) {
throw new Error(`Parse Error: missing closing: '${this.parserOptions.quote || ''}' in line: at '${scanner.lineFromCursor.replace(/[\r\n]/g, "\\n'")}'`);
}
return null;
}
this.checkForMalformedColumn(scanner);
return col;
}
gatherDataBetweenQuotes(scanner) {
const { parserOptions } = this;
let foundStartingQuote = false;
let foundClosingQuote = false;
const characters = [];
let nextToken = scanner.nextCharacterToken;
for (; !foundClosingQuote && nextToken !== null; nextToken = scanner.nextCharacterToken) {
const isQuote = Token_1.Token.isTokenQuote(nextToken, parserOptions);
// ignore first quote
if (!foundStartingQuote && isQuote) {
foundStartingQuote = true;
}
else if (foundStartingQuote) {
if (Token_1.Token.isTokenEscapeCharacter(nextToken, parserOptions)) {
// advance past the escape character so we can get the next one in line
scanner.advancePastToken(nextToken);
const tokenFollowingEscape = scanner.nextCharacterToken;
// if the character following the escape is a quote character then just add
// the quote and advance to that character
if (tokenFollowingEscape !== null &&
(Token_1.Token.isTokenQuote(tokenFollowingEscape, parserOptions) ||
Token_1.Token.isTokenEscapeCharacter(tokenFollowingEscape, parserOptions))) {
characters.push(tokenFollowingEscape.token);
nextToken = tokenFollowingEscape;
}
else if (isQuote) {
// if the escape is also a quote then we found our closing quote and finish early
foundClosingQuote = true;
}
else {
// other wise add the escape token to the characters since it wast escaping anything
characters.push(nextToken.token);
}
}
else if (isQuote) {
// we found our closing quote!
foundClosingQuote = true;
}
else {
// add the token to the characters
characters.push(nextToken.token);
}
}
scanner.advancePastToken(nextToken);
}
return { col: this.columnFormatter.format(characters.join('')), foundClosingQuote };
}
checkForMalformedColumn(scanner) {
const { parserOptions } = this;
const { nextNonSpaceToken } = scanner;
if (nextNonSpaceToken) {
const isNextTokenADelimiter = Token_1.Token.isTokenDelimiter(nextNonSpaceToken, parserOptions);
const isNextTokenARowDelimiter = Token_1.Token.isTokenRowDelimiter(nextNonSpaceToken);
if (!(isNextTokenADelimiter || isNextTokenARowDelimiter)) {
// if the final quote was NOT followed by a column (,) or row(\n) delimiter then its a bad column
// tldr: only part of the column was quoted
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}`);
}
scanner.advanceToToken(nextNonSpaceToken);
}
else if (!scanner.hasMoreData) {
scanner.advancePastLine();
}
}
}
exports.QuotedColumnParser = QuotedColumnParser;
//# sourceMappingURL=QuotedColumnParser.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"QuotedColumnParser.js","sourceRoot":"","sources":["../../../../src/parser/column/QuotedColumnParser.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AAGpD,oCAAiC;AAOjC,MAAa,kBAAkB;IAK3B,YAAmB,aAA4B;QAC3C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,eAAe,GAAG,IAAI,iCAAe,CAAC,aAAa,CAAC,CAAC;IAC9D,CAAC;IAEM,KAAK,CAAC,OAAgB;QACzB,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE;YAC5B,OAAO,IAAI,CAAC;SACf;QACD,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;QACtC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAAC;QACzE,IAAI,CAAC,iBAAiB,EAAE;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CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;gBACpF,MAAM,IAAI,KAAK,CACX,2BAA2B,aAAa,CAAC,gBAAgB,uBAAuB,iBAAiB,CAAC,KAAK,UAAU,WAAW,EAAE,CACjI,CAAC;aACL;YACD,OAAO,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;SAC7C;aAAM,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;YAC7B,OAAO,CAAC,eAAe,EAAE,CAAC;SAC7B;IACL,CAAC;CACJ;AAlGD,gDAkGC"}

View File

@@ -0,0 +1,4 @@
export { ColumnParser } from './ColumnParser';
export { NonQuotedColumnParser } from './NonQuotedColumnParser';
export { QuotedColumnParser } from './QuotedColumnParser';
export { ColumnFormatter } from './ColumnFormatter';

View File

@@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnFormatter = exports.QuotedColumnParser = exports.NonQuotedColumnParser = exports.ColumnParser = void 0;
var ColumnParser_1 = require("./ColumnParser");
Object.defineProperty(exports, "ColumnParser", { enumerable: true, get: function () { return ColumnParser_1.ColumnParser; } });
var NonQuotedColumnParser_1 = require("./NonQuotedColumnParser");
Object.defineProperty(exports, "NonQuotedColumnParser", { enumerable: true, get: function () { return NonQuotedColumnParser_1.NonQuotedColumnParser; } });
var QuotedColumnParser_1 = require("./QuotedColumnParser");
Object.defineProperty(exports, "QuotedColumnParser", { enumerable: true, get: function () { return QuotedColumnParser_1.QuotedColumnParser; } });
var ColumnFormatter_1 = require("./ColumnFormatter");
Object.defineProperty(exports, "ColumnFormatter", { enumerable: true, get: function () { return ColumnFormatter_1.ColumnFormatter; } });
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/parser/column/index.ts"],"names":[],"mappings":";;;AAAA,+CAA8C;AAArC,4GAAA,YAAY,OAAA;AACrB,iEAAgE;AAAvD,8HAAA,qBAAqB,OAAA;AAC9B,2DAA0D;AAAjD,wHAAA,kBAAkB,OAAA;AAC3B,qDAAoD;AAA3C,kHAAA,eAAe,OAAA"}

View File

@@ -0,0 +1,5 @@
export { Parser } from './Parser';
export { RowParser } from './RowParser';
export { Scanner } from './Scanner';
export { Token, MaybeToken } from './Token';
export { ColumnParser, NonQuotedColumnParser, QuotedColumnParser } from './column';

View File

@@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuotedColumnParser = exports.NonQuotedColumnParser = exports.ColumnParser = exports.Token = exports.Scanner = exports.RowParser = exports.Parser = void 0;
var Parser_1 = require("./Parser");
Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return Parser_1.Parser; } });
var RowParser_1 = require("./RowParser");
Object.defineProperty(exports, "RowParser", { enumerable: true, get: function () { return RowParser_1.RowParser; } });
var Scanner_1 = require("./Scanner");
Object.defineProperty(exports, "Scanner", { enumerable: true, get: function () { return Scanner_1.Scanner; } });
var Token_1 = require("./Token");
Object.defineProperty(exports, "Token", { enumerable: true, get: function () { return Token_1.Token; } });
var column_1 = require("./column");
Object.defineProperty(exports, "ColumnParser", { enumerable: true, get: function () { return column_1.ColumnParser; } });
Object.defineProperty(exports, "NonQuotedColumnParser", { enumerable: true, get: function () { return column_1.NonQuotedColumnParser; } });
Object.defineProperty(exports, "QuotedColumnParser", { enumerable: true, get: function () { return column_1.QuotedColumnParser; } });
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/parser/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAClB,qCAAoC;AAA3B,kGAAA,OAAO,OAAA;AAChB,iCAA4C;AAAnC,8FAAA,KAAK,OAAA;AACd,mCAAmF;AAA1E,sGAAA,YAAY,OAAA;AAAE,+GAAA,qBAAqB,OAAA;AAAE,4GAAA,kBAAkB,OAAA"}

View File

@@ -0,0 +1,17 @@
import { ParserOptions } from '../ParserOptions';
import { HeaderArray, Row, RowArray, RowValidatorCallback } from '../types';
export declare class HeaderTransformer<O extends Row> {
private readonly parserOptions;
headers: HeaderArray | null;
private receivedHeaders;
private readonly shouldUseFirstRow;
private processedFirstRow;
private headersLength;
private readonly headersTransform?;
constructor(parserOptions: ParserOptions);
transform(row: RowArray, cb: RowValidatorCallback<O>): void;
private shouldMapRow;
private processRow;
private mapHeaders;
private setHeaders;
}

View File

@@ -0,0 +1,115 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HeaderTransformer = void 0;
const lodash_isundefined_1 = __importDefault(require("lodash.isundefined"));
const lodash_isfunction_1 = __importDefault(require("lodash.isfunction"));
const lodash_uniq_1 = __importDefault(require("lodash.uniq"));
const lodash_groupby_1 = __importDefault(require("lodash.groupby"));
class HeaderTransformer {
constructor(parserOptions) {
this.headers = null;
this.receivedHeaders = false;
this.shouldUseFirstRow = false;
this.processedFirstRow = false;
this.headersLength = 0;
this.parserOptions = parserOptions;
if (parserOptions.headers === true) {
this.shouldUseFirstRow = true;
}
else if (Array.isArray(parserOptions.headers)) {
this.setHeaders(parserOptions.headers);
}
else if (lodash_isfunction_1.default(parserOptions.headers)) {
this.headersTransform = parserOptions.headers;
}
}
transform(row, cb) {
if (!this.shouldMapRow(row)) {
return cb(null, { row: null, isValid: true });
}
return cb(null, this.processRow(row));
}
shouldMapRow(row) {
const { parserOptions } = this;
if (!this.headersTransform && parserOptions.renameHeaders && !this.processedFirstRow) {
if (!this.receivedHeaders) {
throw new Error('Error renaming headers: new headers must be provided in an array');
}
this.processedFirstRow = true;
return false;
}
if (!this.receivedHeaders && Array.isArray(row)) {
if (this.headersTransform) {
this.setHeaders(this.headersTransform(row));
}
else if (this.shouldUseFirstRow) {
this.setHeaders(row);
}
else {
// dont do anything with the headers if we didnt receive a transform or shouldnt use the first row.
return true;
}
return false;
}
return true;
}
processRow(row) {
if (!this.headers) {
return { row: row, isValid: true };
}
const { parserOptions } = this;
if (!parserOptions.discardUnmappedColumns && row.length > this.headersLength) {
if (!parserOptions.strictColumnHandling) {
throw new Error(`Unexpected Error: column header mismatch expected: ${this.headersLength} columns got: ${row.length}`);
}
return {
row: row,
isValid: false,
reason: `Column header mismatch expected: ${this.headersLength} columns got: ${row.length}`,
};
}
if (parserOptions.strictColumnHandling && row.length < this.headersLength) {
return {
row: row,
isValid: false,
reason: `Column header mismatch expected: ${this.headersLength} columns got: ${row.length}`,
};
}
return { row: this.mapHeaders(row), isValid: true };
}
mapHeaders(row) {
const rowMap = {};
const { headers, headersLength } = this;
for (let i = 0; i < headersLength; i += 1) {
const header = headers[i];
if (!lodash_isundefined_1.default(header)) {
const val = row[i];
// eslint-disable-next-line no-param-reassign
if (lodash_isundefined_1.default(val)) {
rowMap[header] = '';
}
else {
rowMap[header] = val;
}
}
}
return rowMap;
}
setHeaders(headers) {
var _a;
const filteredHeaders = headers.filter((h) => !!h);
if (lodash_uniq_1.default(filteredHeaders).length !== filteredHeaders.length) {
const grouped = lodash_groupby_1.default(filteredHeaders);
const duplicates = Object.keys(grouped).filter((dup) => grouped[dup].length > 1);
throw new Error(`Duplicate headers found ${JSON.stringify(duplicates)}`);
}
this.headers = headers;
this.receivedHeaders = true;
this.headersLength = ((_a = this.headers) === null || _a === void 0 ? void 0 : _a.length) || 0;
}
}
exports.HeaderTransformer = HeaderTransformer;
//# sourceMappingURL=HeaderTransformer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"HeaderTransformer.js","sourceRoot":"","sources":["../../../src/transforms/HeaderTransformer.ts"],"names":[],"mappings":";;;;;;AAAA,4EAA6C;AAC7C,0EAA2C;AAC3C,8DAA+B;AAC/B,oEAAqC;AAYrC,MAAa,iBAAiB;IAe1B,YAAmB,aAA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CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;SAC5E;QACD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAC5B,IAAI,CAAC,aAAa,GAAG,OAAA,IAAI,CAAC,OAAO,0CAAE,MAAM,KAAI,CAAC,CAAC;IACnD,CAAC;CACJ;AAhHD,8CAgHC"}

View File

@@ -0,0 +1,12 @@
import { Row, RowTransformFunction, RowValidatorCallback, RowValidate } from '../types';
export declare class RowTransformerValidator<I extends Row, O extends Row> {
private static createTransform;
private static createValidator;
private _rowTransform;
private _rowValidator;
set rowTransform(transformFunction: RowTransformFunction<I, O>);
set rowValidator(validateFunction: RowValidate<O>);
transformAndValidate(row: I, cb: RowValidatorCallback<O>): void;
private callTransformer;
private callValidator;
}

View File

@@ -0,0 +1,93 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RowTransformerValidator = void 0;
const lodash_isfunction_1 = __importDefault(require("lodash.isfunction"));
const types_1 = require("../types");
class RowTransformerValidator {
constructor() {
this._rowTransform = null;
this._rowValidator = null;
}
// eslint-disable-next-line @typescript-eslint/no-shadow
static createTransform(transformFunction) {
if (types_1.isSyncTransform(transformFunction)) {
return (row, cb) => {
let transformed = null;
try {
transformed = transformFunction(row);
}
catch (e) {
return cb(e);
}
return cb(null, transformed);
};
}
return transformFunction;
}
static createValidator(validateFunction) {
if (types_1.isSyncValidate(validateFunction)) {
return (row, cb) => {
cb(null, { row, isValid: validateFunction(row) });
};
}
return (row, cb) => {
validateFunction(row, (err, isValid, reason) => {
if (err) {
return cb(err);
}
if (isValid) {
return cb(null, { row, isValid, reason });
}
return cb(null, { row, isValid: false, reason });
});
};
}
set rowTransform(transformFunction) {
if (!lodash_isfunction_1.default(transformFunction)) {
throw new TypeError('The transform should be a function');
}
this._rowTransform = RowTransformerValidator.createTransform(transformFunction);
}
set rowValidator(validateFunction) {
if (!lodash_isfunction_1.default(validateFunction)) {
throw new TypeError('The validate should be a function');
}
this._rowValidator = RowTransformerValidator.createValidator(validateFunction);
}
transformAndValidate(row, cb) {
return this.callTransformer(row, (transformErr, transformedRow) => {
if (transformErr) {
return cb(transformErr);
}
if (!transformedRow) {
return cb(null, { row: null, isValid: true });
}
return this.callValidator(transformedRow, (validateErr, validationResult) => {
if (validateErr) {
return cb(validateErr);
}
if (validationResult && !validationResult.isValid) {
return cb(null, { row: transformedRow, isValid: false, reason: validationResult.reason });
}
return cb(null, { row: transformedRow, isValid: true });
});
});
}
callTransformer(row, cb) {
if (!this._rowTransform) {
return cb(null, row);
}
return this._rowTransform(row, cb);
}
callValidator(row, cb) {
if (!this._rowValidator) {
return cb(null, { row, isValid: true });
}
return this._rowValidator(row, cb);
}
}
exports.RowTransformerValidator = RowTransformerValidator;
//# sourceMappingURL=RowTransformerValidator.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"RowTransformerValidator.js","sourceRoot":"","sources":["../../../src/transforms/RowTransformerValidator.ts"],"names":[],"mappings":";;;;;;AAAA,0EAA2C;AAC3C,oCASkB;AAIlB,MAAa,uBAAuB;IAApC;QAsCY,kBAAa,GAAmC,IAAI,CAAC;QAErD,kBAAa,GAA2B,IAAI,CAAC;IAiDzD,CAAC;IAxFG,wDAAwD;IAChD,MAAM,CAAC,eAAe,CAC1B,iBAA6C;QAE7C,IAAI,uBAAe,CAAC,iBAAiB,CAAC,EAAE;YACpC,OAAO,CAAC,GAAM,EAAE,EAA2B,EAAQ,EAAE;gBACjD,IAAI,WAAW,GAAa,IAAI,CAAC;gBACjC,IAAI;oBACA,WAAW,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;iBACxC;gBAAC,OAAO,CAAC,EAAE;oBACR,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;iBAChB;gBACD,OAAO,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;YACjC,CA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AE,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;SAC3C;QACD,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACvC,CAAC;CACJ;AAzFD,0DAyFC"}

View File

@@ -0,0 +1,2 @@
export { RowTransformerValidator } from './RowTransformerValidator';
export { HeaderTransformer } from './HeaderTransformer';

View File

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

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transforms/index.ts"],"names":[],"mappings":";;;AAAA,qEAAoE;AAA3D,kIAAA,uBAAuB,OAAA;AAChC,yDAAwD;AAA/C,sHAAA,iBAAiB,OAAA"}

View File

@@ -0,0 +1,21 @@
export declare type RowMap<V = any> = Record<string, V>;
export declare type RowArray<V = any> = V[];
export declare type Row<V = any> = RowMap<V> | RowArray<V>;
export interface RowValidationResult<R extends Row> {
row: R | null;
isValid: boolean;
reason?: string;
}
export declare type RowValidatorCallback<R extends Row> = (error: Error | null, result?: RowValidationResult<R>) => void;
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<any>, O extends Row<any>>(transform: RowTransformFunction<I, O>) => transform is SyncRowTransform<I, O>;
export declare type RowValidateCallback = (error?: Error | null, isValid?: boolean, reason?: string) => void;
export declare type SyncRowValidate<R extends Row> = (row: R) => boolean;
export declare type AsyncRowValidate<R extends Row> = (row: R, cb: RowValidateCallback) => void;
export declare type RowValidate<R extends Row> = AsyncRowValidate<R> | SyncRowValidate<R>;
export declare const isSyncValidate: <R extends Row<any>>(validate: RowValidate<R>) => validate is SyncRowValidate<R>;
export declare type HeaderArray = (string | undefined | null)[];
export declare type HeaderTransformFunction = (headers: HeaderArray) => HeaderArray;

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSyncValidate = exports.isSyncTransform = void 0;
exports.isSyncTransform = (transform) => transform.length === 1;
exports.isSyncValidate = (validate) => validate.length === 1;
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";;;AAoBa,QAAA,eAAe,GAAG,CAC3B,SAAqC,EACF,EAAE,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,CAAC;AAQpD,QAAA,cAAc,GAAG,CAAgB,QAAwB,EAAkC,EAAE,CACtG,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC"}

View File

@@ -0,0 +1,62 @@
{
"name": "@fast-csv/parse",
"version": "4.3.6",
"description": "fast-csv parsing package",
"keywords": [
"csv",
"parse",
"fast-csv",
"parser"
],
"author": "doug-martin <doug@dougamartin.com>",
"homepage": "http://c2fo.github.com/fast-csv/packages/parse",
"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/parse"
},
"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.groupby": "^4.6.0",
"lodash.isfunction": "^3.0.9",
"lodash.isnil": "^4.0.0",
"lodash.isundefined": "^3.0.1",
"lodash.uniq": "^4.5.0"
},
"devDependencies": {
"@types/lodash.escaperegexp": "4.1.6",
"@types/lodash.groupby": "4.6.6",
"@types/lodash.isfunction": "3.0.6",
"@types/lodash.isnil": "4.0.6",
"@types/lodash.isundefined": "3.0.6",
"@types/lodash.partition": "4.6.6",
"@types/lodash.uniq": "4.5.6",
"@types/sinon": "9.0.9",
"lodash.partition": "4.6.0",
"sinon": "9.2.1"
},
"gitHead": "3dc859edb19924b315051e4c87d6273808a0de73"
}

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) Microsoft Corporation.
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

@@ -0,0 +1,16 @@
# Installation
> `npm install --save @types/node`
# Summary
This package contains type definitions for Node.js (https://nodejs.org/).
# Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node/v14.
### Additional Details
* Last updated: Sat, 23 Sep 2023 20:38:33 GMT
* Dependencies: none
* Global values: `AbortController`, `AbortSignal`, `Buffer`, `__dirname`, `__filename`, `clearImmediate`, `clearInterval`, `clearTimeout`, `console`, `exports`, `global`, `module`, `process`, `queueMicrotask`, `require`, `setImmediate`, `setInterval`, `setTimeout`
# Credits
These definitions were written by [Microsoft TypeScript](https://github.com/Microsoft), [DefinitelyTyped](https://github.com/DefinitelyTyped), [Alberto Schiabel](https://github.com/jkomyno), [Alvis HT Tang](https://github.com/alvis), [Andrew Makarov](https://github.com/r3nya), [Benjamin Toueg](https://github.com/btoueg), [Chigozirim C.](https://github.com/smac89), [David Junger](https://github.com/touffy), [Deividas Bakanas](https://github.com/DeividasBakanas), [Eugene Y. Q. Shen](https://github.com/eyqs), [Hannes Magnusson](https://github.com/Hannes-Magnusson-CK), [Hoàng Văn Khải](https://github.com/KSXGitHub), [Huw](https://github.com/hoo29), [Kelvin Jin](https://github.com/kjin), [Klaus Meinhardt](https://github.com/ajafff), [Lishude](https://github.com/islishude), [Mariusz Wiktorczyk](https://github.com/mwiktorczyk), [Mohsen Azimi](https://github.com/mohsen1), [Nicolas Even](https://github.com/n-e), [Nikita Galkin](https://github.com/galkin), [Parambir Singh](https://github.com/parambirs), [Sebastian Silbermann](https://github.com/eps1lon), [Seth Westphal](https://github.com/westy92), [Simon Schick](https://github.com/SimonSchick), [Thomas den Hollander](https://github.com/ThomasdenH), [Wilco Bakker](https://github.com/WilcoBakker), [wwwy3y3](https://github.com/wwwy3y3), [Samuel Ainsworth](https://github.com/samuela), [Kyle Uehlein](https://github.com/kuehlein), [Thanik Bhongbhibhat](https://github.com/bhongy), [Marcin Kopacz](https://github.com/chyzwar), [Trivikram Kamat](https://github.com/trivikr), [Junxiao Shi](https://github.com/yoursunny), [Ilia Baryshnikov](https://github.com/qwelias), [ExE Boss](https://github.com/ExE-Boss), [Piotr Błażejewicz](https://github.com/peterblazejewicz), [Anna Henningsen](https://github.com/addaleax), [Victor Perin](https://github.com/victorperin), [Yongsheng Zhang](https://github.com/ZYSzys), [Bond](https://github.com/bondz), and [Linus Unnebäck](https://github.com/LinusU).

View File

@@ -0,0 +1,130 @@
declare module "assert" {
/** An alias of `assert.ok()`. */
function assert(value: any, message?: string | Error): asserts value;
namespace assert {
class AssertionError extends Error {
actual: any;
expected: any;
operator: string;
generatedMessage: boolean;
code: "ERR_ASSERTION";
constructor(options?: {
/** If provided, the error message is set to this value. */
message?: string | undefined;
/** The `actual` property on the error instance. */
actual?: any;
/** The `expected` property on the error instance. */
expected?: any;
/** The `operator` property on the error instance. */
operator?: string | undefined;
/** If provided, the generated stack trace omits frames before this function. */
// tslint:disable-next-line:ban-types
stackStartFn?: Function | undefined;
});
}
class CallTracker {
calls(exact?: number): () => void;
calls<Func extends (...args: any[]) => any>(fn?: Func, exact?: number): Func;
report(): CallTrackerReportInformation[];
verify(): void;
}
interface CallTrackerReportInformation {
message: string;
/** The actual number of times the function was called. */
actual: number;
/** The number of times the function was expected to be called. */
expected: number;
/** The name of the function that is wrapped. */
operator: string;
/** A stack trace of the function. */
stack: object;
}
type AssertPredicate = RegExp | (new() => object) | ((thrown: any) => boolean) | object | Error;
function fail(message?: string | Error): never;
/** @deprecated since v10.0.0 - use fail([message]) or other assert functions instead. */
function fail(
actual: any,
expected: any,
message?: string | Error,
operator?: string,
// tslint:disable-next-line:ban-types
stackStartFn?: Function,
): never;
function ok(value: any, message?: string | Error): asserts value;
/** @deprecated since v9.9.0 - use strictEqual() instead. */
function equal(actual: any, expected: any, message?: string | Error): void;
/** @deprecated since v9.9.0 - use notStrictEqual() instead. */
function notEqual(actual: any, expected: any, message?: string | Error): void;
/** @deprecated since v9.9.0 - use deepStrictEqual() instead. */
function deepEqual(actual: any, expected: any, message?: string | Error): void;
/** @deprecated since v9.9.0 - use notDeepStrictEqual() instead. */
function notDeepEqual(actual: any, expected: any, message?: string | Error): void;
function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
function notStrictEqual(actual: any, expected: any, message?: string | Error): void;
function deepStrictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
function notDeepStrictEqual(actual: any, expected: any, message?: string | Error): void;
function throws(block: () => any, message?: string | Error): void;
function throws(block: () => any, error: AssertPredicate, message?: string | Error): void;
function doesNotThrow(block: () => any, message?: string | Error): void;
function doesNotThrow(block: () => any, error: AssertPredicate, message?: string | Error): void;
function ifError(value: any): asserts value is null | undefined;
function rejects(block: (() => Promise<any>) | Promise<any>, message?: string | Error): Promise<void>;
function rejects(
block: (() => Promise<any>) | Promise<any>,
error: AssertPredicate,
message?: string | Error,
): Promise<void>;
function doesNotReject(block: (() => Promise<any>) | Promise<any>, message?: string | Error): Promise<void>;
function doesNotReject(
block: (() => Promise<any>) | Promise<any>,
error: AssertPredicate,
message?: string | Error,
): Promise<void>;
function match(value: string, regExp: RegExp, message?: string | Error): void;
function doesNotMatch(value: string, regExp: RegExp, message?: string | Error): void;
const strict:
& Omit<
typeof assert,
| "equal"
| "notEqual"
| "deepEqual"
| "notDeepEqual"
| "ok"
| "strictEqual"
| "deepStrictEqual"
| "ifError"
| "strict"
>
& {
(value: any, message?: string | Error): asserts value;
equal: typeof strictEqual;
notEqual: typeof notStrictEqual;
deepEqual: typeof deepStrictEqual;
notDeepEqual: typeof notDeepStrictEqual;
// Mapped types and assertion functions are incompatible?
// TS2775: Assertions require every name in the call target
// to be declared with an explicit type annotation.
ok: typeof ok;
strictEqual: typeof strictEqual;
deepStrictEqual: typeof deepStrictEqual;
ifError: typeof ifError;
strict: typeof strict;
};
}
export = assert;
}
declare module "node:assert" {
import assert = require("assert");
export = assert;
}

View File

@@ -0,0 +1,237 @@
/**
* Async Hooks module: https://nodejs.org/api/async_hooks.html
*/
declare module "async_hooks" {
/**
* Returns the asyncId of the current execution context.
*/
function executionAsyncId(): number;
/**
* The resource representing the current execution.
* Useful to store data within the resource.
*
* Resource objects returned by `executionAsyncResource()` are most often internal
* Node.js handle objects with undocumented APIs. Using any functions or properties
* on the object is likely to crash your application and should be avoided.
*
* Using `executionAsyncResource()` in the top-level execution context will
* return an empty object as there is no handle or request object to use,
* but having an object representing the top-level can be helpful.
*/
function executionAsyncResource(): object;
/**
* Returns the ID of the resource responsible for calling the callback that is currently being executed.
*/
function triggerAsyncId(): number;
interface HookCallbacks {
/**
* Called when a class is constructed that has the possibility to emit an asynchronous event.
* @param asyncId a unique ID for the async resource
* @param type the type of the async resource
* @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created
* @param resource reference to the resource representing the async operation, needs to be released during destroy
*/
init?(asyncId: number, type: string, triggerAsyncId: number, resource: object): void;
/**
* When an asynchronous operation is initiated or completes a callback is called to notify the user.
* The before callback is called just before said callback is executed.
* @param asyncId the unique identifier assigned to the resource about to execute the callback.
*/
before?(asyncId: number): void;
/**
* Called immediately after the callback specified in before is completed.
* @param asyncId the unique identifier assigned to the resource which has executed the callback.
*/
after?(asyncId: number): void;
/**
* Called when a promise has resolve() called. This may not be in the same execution id
* as the promise itself.
* @param asyncId the unique id for the promise that was resolve()d.
*/
promiseResolve?(asyncId: number): void;
/**
* Called after the resource corresponding to asyncId is destroyed
* @param asyncId a unique ID for the async resource
*/
destroy?(asyncId: number): void;
}
interface AsyncHook {
/**
* Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.
*/
enable(): this;
/**
* Disable the callbacks for a given AsyncHook instance from the global pool of AsyncHook callbacks to be executed. Once a hook has been disabled it will not be called again until enabled.
*/
disable(): this;
}
/**
* Registers functions to be called for different lifetime events of each async operation.
* @param options the callbacks to register
* @return an AsyncHooks instance used for disabling and enabling hooks
*/
function createHook(options: HookCallbacks): AsyncHook;
interface AsyncResourceOptions {
/**
* The ID of the execution context that created this async event.
* @default executionAsyncId()
*/
triggerAsyncId?: number | undefined;
/**
* Disables automatic `emitDestroy` when the object is garbage collected.
* This usually does not need to be set (even if `emitDestroy` is called
* manually), unless the resource's `asyncId` is retrieved and the
* sensitive API's `emitDestroy` is called with it.
* @default false
*/
requireManualDestroy?: boolean | undefined;
}
/**
* The class AsyncResource was designed to be extended by the embedder's async resources.
* Using this users can easily trigger the lifetime events of their own resources.
*/
class AsyncResource {
/**
* AsyncResource() is meant to be extended. Instantiating a
* new AsyncResource() also triggers init. If triggerAsyncId is omitted then
* async_hook.executionAsyncId() is used.
* @param type The type of async event.
* @param triggerAsyncId The ID of the execution context that created
* this async event (default: `executionAsyncId()`), or an
* AsyncResourceOptions object (since v9.3.0)
*/
constructor(type: string, triggerAsyncId?: number | AsyncResourceOptions);
/**
* Binds the given function to the current execution context.
* @param fn The function to bind to the current execution context.
* @param type An optional name to associate with the underlying `AsyncResource`.
*/
static bind<Func extends (...args: any[]) => any>(
fn: Func,
type?: string,
): Func & { asyncResource: AsyncResource };
/**
* Binds the given function to execute to this `AsyncResource`'s scope.
* @param fn The function to bind to the current `AsyncResource`.
*/
bind<Func extends (...args: any[]) => any>(fn: Func): Func & { asyncResource: AsyncResource };
/**
* Call the provided function with the provided arguments in the
* execution context of the async resource. This will establish the
* context, trigger the AsyncHooks before callbacks, call the function,
* trigger the AsyncHooks after callbacks, and then restore the original
* execution context.
* @param fn The function to call in the execution context of this
* async resource.
* @param thisArg The receiver to be used for the function call.
* @param args Optional arguments to pass to the function.
*/
runInAsyncScope<This, Result>(
fn: (this: This, ...args: any[]) => Result,
thisArg?: This,
...args: any[]
): Result;
/**
* Call AsyncHooks destroy callbacks.
*/
emitDestroy(): this;
/**
* @return the unique ID assigned to this AsyncResource instance.
*/
asyncId(): number;
/**
* @return the trigger ID for this AsyncResource instance.
*/
triggerAsyncId(): number;
}
/**
* When having multiple instances of `AsyncLocalStorage`, they are independent
* from each other. It is safe to instantiate this class multiple times.
*/
class AsyncLocalStorage<T> {
/**
* This method disables the instance of `AsyncLocalStorage`. All subsequent calls
* to `asyncLocalStorage.getStore()` will return `undefined` until
* `asyncLocalStorage.run()` is called again.
*
* When calling `asyncLocalStorage.disable()`, all current contexts linked to the
* instance will be exited.
*
* Calling `asyncLocalStorage.disable()` is required before the
* `asyncLocalStorage` can be garbage collected. This does not apply to stores
* provided by the `asyncLocalStorage`, as those objects are garbage collected
* along with the corresponding async resources.
*
* This method is to be used when the `asyncLocalStorage` is not in use anymore
* in the current process.
*/
disable(): void;
/**
* This method returns the current store. If this method is called outside of an
* asynchronous context initialized by calling `asyncLocalStorage.run`, it will
* return `undefined`.
*/
getStore(): T | undefined;
/**
* This methods runs a function synchronously within a context and return its
* return value. The store is not accessible outside of the callback function or
* the asynchronous operations created within the callback.
*
* Optionally, arguments can be passed to the function. They will be passed to the
* callback function.
*
* I the callback function throws an error, it will be thrown by `run` too. The
* stacktrace will not be impacted by this call and the context will be exited.
*/
// TODO: Apply generic vararg once available
run<R>(store: T, callback: () => R): R;
run<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R;
/**
* This methods runs a function synchronously outside of a context and return its
* return value. The store is not accessible within the callback function or the
* asynchronous operations created within the callback.
*
* Optionally, arguments can be passed to the function. They will be passed to the
* callback function.
*
* If the callback function throws an error, it will be thrown by `exit` too. The
* stacktrace will not be impacted by this call and the context will be
* re-entered.
*/
// TODO: Apply generic vararg once available
exit<R>(callback: (...args: any[]) => R, ...args: any[]): R;
/**
* Calling `asyncLocalStorage.enterWith(store)` will transition into the context
* for the remainder of the current synchronous execution and will persist
* through any following asynchronous calls.
*/
enterWith(store: T): void;
}
}
declare module "node:async_hooks" {
export * from "async_hooks";
}

Some files were not shown because too many files have changed in this diff Show More