fix(web): CSV downloads open in Excel with CJK names intact
Without a byte order mark Excel reads a CSV in the system's legacy code page, Japanese Windows included, and every Japanese, Chinese or Korean name came out as mojibake. csvForExcel() prepends the mark; JSON is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d644a9fbc
commit
51b7da62b6
2 files changed
+31
-1
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { csvForExcel, recordsToCsv } from "./export";
|
||||
import type { GameRecord } from "./schema";
|
||||
|
||||
describe("CSV export", () => {
|
||||
it("starts with a byte order mark, so Excel reads it as UTF-8", () => {
|
||||
const out = csvForExcel("name\nトロイ\n");
|
||||
expect(out.codePointAt(0)).toBe(0xfeff);
|
||||
expect(out.slice(1)).toBe("name\nトロイ\n");
|
||||
expect([...new TextEncoder().encode(out).slice(0, 3)]).toEqual([0xef, 0xbb, 0xbf]);
|
||||
});
|
||||
|
||||
it("keeps CJK names as written and quotes only an ASCII comma", () => {
|
||||
const game = { link: "https://store.steampowered.com/app/5118700/", name: "//トロイメライ・ノクターン" };
|
||||
const other = { link: "https://store.steampowered.com/app/1/", name: "无主之地,银河, again" };
|
||||
const [, first, second] = recordsToCsv([game, other] as unknown as GameRecord[]).split("\n");
|
||||
expect(first.startsWith("https://store.steampowered.com/app/5118700/,//トロイメライ・ノクターン,")).toBe(true);
|
||||
expect(second.startsWith('https://store.steampowered.com/app/1/,"无主之地,银河, again",')).toBe(true);
|
||||
});
|
||||
});
|
||||
+11
-1
@@ -75,8 +75,18 @@ export function downloadBlob(content: string, filename: string, mime: string) {
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* The CSV as a spreadsheet should open it. Without a byte order mark Excel
|
||||
* reads a CSV in the system's legacy code page and turns every Japanese,
|
||||
* Chinese or Korean name into mojibake - on Japanese Windows too, whose code
|
||||
* page is not UTF-8 either. Other programs skip the mark; JSON needs none.
|
||||
*/
|
||||
export function csvForExcel(csv: string): string {
|
||||
return "\uFEFF" + csv;
|
||||
}
|
||||
|
||||
export function exportCsv(records: GameRecord[], filename = "f2p-games.csv") {
|
||||
downloadBlob(recordsToCsv(records), filename, "text/csv");
|
||||
downloadBlob(csvForExcel(recordsToCsv(records)), filename, "text/csv");
|
||||
}
|
||||
|
||||
export function exportJson(records: GameRecord[], filename = "f2p-games.json") {
|
||||
|
||||
Reference in new issue
Block a user