feat: add csv exporter

This commit is contained in:
2026-04-20 20:09:06 +08:00
parent 26bdff8daa
commit c846ab9f4a
3 changed files with 130 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
import { normalizeRateDisplay } from "../../shared/rate-normalizer";
import { escapeCsvCell } from "../../shared/csv";
import type { MarketRecord } from "./types";
const CSV_COLUMNS = [
{
header: "达人ID",
readValue: (record: MarketRecord) => record.authorId
},
{
header: "达人名称",
readValue: (record: MarketRecord) => record.authorName
},
{
header: "地区",
readValue: (record: MarketRecord) => record.location ?? ""
},
{
header: "21-60s报价",
readValue: (record: MarketRecord) => record.price21To60s ?? ""
},
{
header: "单视频看后搜率",
readValue: (record: MarketRecord) =>
record.rates?.singleVideoAfterSearchRate
? normalizeRateDisplay(record.rates.singleVideoAfterSearchRate)
: ""
},
{
header: "个人视频看后搜率",
readValue: (record: MarketRecord) =>
record.rates?.personalVideoAfterSearchRate
? normalizeRateDisplay(record.rates.personalVideoAfterSearchRate)
: ""
},
{
header: "插件数据状态",
readValue: (record: MarketRecord) => record.status
}
] as const;
export function buildMarketCsv(records: MarketRecord[]): string {
const headerLine = CSV_COLUMNS.map((column) => column.header).join(",");
const rowLines = records.map((record) =>
CSV_COLUMNS.map((column) => escapeCsvCell(column.readValue(record))).join(",")
);
return [headerLine, ...rowLines].join("\n");
}
+7
View File
@@ -0,0 +1,7 @@
export function escapeCsvCell(value: string): string {
if (/[",\n]/.test(value)) {
return `"${value.replace(/"/g, "\"\"")}"`;
}
return value;
}