Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ file had it. A field that is given must be right. This holds for `hints` too: `d
| Note `code` | When | `params` |
| --- | --- | --- |
| `ENCODING` | Always, for text | `encoding` |
| `SEPARATOR` | Always, for delimited text | `separator`, `name` |
| `SEPARATOR` | Always, for delimited text | `separator` |
| `LINES_SKIPPED` | A title block above the header | `count` |
| `COLUMN_RENAMED` | An empty or repeated name | `position`, `from`, `to` |
| `COLUMNS_RENAMED_MORE` | More than 20 renamed | `count` |
Expand Down
3 changes: 1 addition & 2 deletions packages/pivotkit-core/src/import-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { decodeText } from "./import-text.js";
import { localeDecimal } from "./locale-decimal.js";

const CANDIDATES = [",", ";", "\t", "|"];
const SEPARATOR_NAMES = { ",": "commas", ";": "semicolons", "\t": "tabs", "|": "vertical bars" };
/** Characters of the start of a file the separator is guessed from. */
const SAMPLE_CHARS = 65_536;
const SAMPLE_ROWS = 100;
Expand Down Expand Up @@ -187,7 +186,7 @@ export async function read(file, options) {
hints: { decimal: sep === ";" ? "," : localeDecimal(o.locale) }
});
table.note("ENCODING", { encoding });
table.note("SEPARATOR", { separator: sep, name: SEPARATOR_NAMES[sep] ?? `"${sep}"` });
table.note("SEPARATOR", { separator: sep });

// splitRows runs synchronously; between steps of ROW_STEP rows the event loop gets a turn
let at = start;
Expand Down
9 changes: 8 additions & 1 deletion packages/pivotkit-core/src/import-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import { resolveLimits } from "./limits.js";
import { isDateMs, parseIso } from "./time.js";
import { CONTROLS, HIDDEN } from "./util.js";

const SEPARATOR_NAMES = Object.freeze({
",": "commas",
";": "semicolons",
"\t": "tabs",
"|": "vertical bars"
});

/** The column types a loaded table can have (`encode`'s, without "null"). */
export const IMPORT_TYPES = Object.freeze(["string", "number", "date", "boolean"]);

Expand Down Expand Up @@ -194,7 +201,7 @@ export function noteMessage(code, p) {
case "ENCODING":
return `Read as ${p.encoding}.`;
case "SEPARATOR":
return `Values separated by ${p.name}.`;
return `Values separated by ${SEPARATOR_NAMES[p.separator] ?? `"${p.separator}"`}.`;
case "LINES_SKIPPED":
return `${n(p.count)} line(s) above the column names were skipped (a title block).`;
case "COLUMN_RENAMED":
Expand Down
10 changes: 9 additions & 1 deletion packages/pivotkit-core/test/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readFileSync } from "node:fs";
import { BUILT_IN_READERS, PLUGIN_API_VERSION, csvReader, jsonReader, loadReader, readFile, readerFor, retype, validatePlugin } from "pivotkit-core/import";
import { LIMITS, PivotkitError, computePivot, encode, toGrid } from "pivotkit-core";

import { fileName } from "../src/import-table.js";
import { fileName, noteMessage } from "../src/import-table.js";
import { cpuClock } from "../../../tests/cpu-time.js";

const fixture = (name) => readFileSync(new URL(`./import-fixtures/${name}`, import.meta.url));
Expand All @@ -18,6 +18,14 @@ const typeOf = (table, name) => table.types[table.columns.indexOf(name)];
const codes = (table) => table.notes.map((n) => n.code);
const rejects = (promise, check) => assert.rejects(promise, (e) => e instanceof PivotkitError && check(e));

test("separator note names", () => {
assert.equal(noteMessage("SEPARATOR", { separator: "," }), "Values separated by commas.");
assert.equal(noteMessage("SEPARATOR", { separator: ";" }), "Values separated by semicolons.");
assert.equal(noteMessage("SEPARATOR", { separator: "\t" }), "Values separated by tabs.");
assert.equal(noteMessage("SEPARATOR", { separator: "|" }), "Values separated by vertical bars.");
assert.equal(noteMessage("SEPARATOR", { separator: ":" }), 'Values separated by ":".');
});

test("German election results: BOM, semicolons, a title block, German numbers and dates, leading zeros", async () => {
const t = await readFile(fileOf("kerg2-btw25.csv"));
assert.equal(t.columns[0], "Wahlart");
Expand Down
12 changes: 9 additions & 3 deletions packages/pivotkit-react/src/import-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ export const en = {
// the preview of a sheet a saved view needs: which view and sheet; another sheet loaded there does not open the view
sheetForView: "Opening “{name}” needs the sheet “{sheet}”.",
sheetOther: "“{name}” was not opened: it was made for the sheet “{sheet}”.",
separatorNames: { ",": "commas", ";": "semicolons", "\t": "tabs", "|": "vertical bars" },
notes: {
ENCODING: "Read as {encoding}.",
SEPARATOR: "Values separated by {name}.",
SEPARATOR: "Values separated by {separatorName}.",
LINES_SKIPPED: { one: "{count} line above the column names was skipped (a title block).", other: "{count} lines above the column names were skipped (a title block)." },
COLUMN_RENAMED: "Column “{from}” is named “{to}”.",
COLUMN_UNNAMED: "Column {position} has no name; it is named “{to}”.",
Expand Down Expand Up @@ -142,9 +143,10 @@ export const de = {
sheetRefused: "„{name}“ wurde nicht geöffnet: das Blatt „{sheet}“ wurde nicht geladen.",
sheetForView: "Zum Öffnen von „{name}“ wird das Blatt „{sheet}“ gebraucht.",
sheetOther: "„{name}“ wurde nicht geöffnet: es wurde für das Blatt „{sheet}“ gemacht.",
separatorNames: { ",": "Kommas", ";": "Semikolons", "\t": "Tabs", "|": "senkrechte Striche" },
notes: {
ENCODING: "Gelesen als {encoding}.",
SEPARATOR: "Werte getrennt durch {name}.",
SEPARATOR: "Werte getrennt durch {separatorName}.",
LINES_SKIPPED: { one: "{count} Zeile über den Spaltennamen wurde übersprungen (ein Titelblock).", other: "{count} Zeilen über den Spaltennamen wurden übersprungen (ein Titelblock)." },
COLUMN_RENAMED: "Die Spalte „{from}“ heißt jetzt „{to}“.",
COLUMN_UNNAMED: "Spalte {position} hat keinen Namen; sie heißt jetzt „{to}“.",
Expand Down Expand Up @@ -251,7 +253,11 @@ export function noteText(note, texts, where) {
return String(note.message ?? code);
}
const values = Object.fromEntries(Object.entries(p).map(([k, v]) => [k, typeof v === "number" ? v.toLocaleString(where.locale) : String(v ?? "")]));
if (code === "DATE_ORDER") {
if (code === "SEPARATOR") {
values.separatorName = Object.hasOwn(texts.separatorNames, String(p.separator))
? texts.separatorNames[String(p.separator)]
: `"${p.separator}"`;
} else if (code === "DATE_ORDER") {
values.order = Object.hasOwn(texts.dateOrders, String(p.order)) ? /** @type {any} */ (texts.dateOrders)[String(p.order)] : values.order;
} else if (code === "VALUES_NOT_CONVERTED") {
values.type = Object.hasOwn(texts.types, String(p.type)) ? /** @type {any} */ (texts.types)[String(p.type)] : values.type;
Expand Down
10 changes: 10 additions & 0 deletions packages/pivotkit-react/test/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ test("every note code of the readers has a text in English and German; German ha

test("a note in the report's language: counts in their plural form and the locale's digits, the variants of a code, a plugin's own code in its words", () => {
const where = { lang: "en", locale: "en-US" };
const separators = [
[",", "commas", "Kommas"],
[";", "semicolons", "Semikolons"],
["\t", "tabs", "Tabs"],
["|", "vertical bars", "senkrechte Striche"],
];
for (const [separator, enName, deName] of separators) {
assert.equal(noteText({ code: "SEPARATOR", params: { separator } }, en, where), `Values separated by ${enName}.`);
assert.equal(noteText({ code: "SEPARATOR", params: { separator } }, de, { lang: "de", locale: "de-DE" }), `Werte getrennt durch ${deName}.`);
}
assert.equal(noteText({ code: "ROWS_EMPTY", params: { count: 1 } }, en, where), "1 empty row was left out.");
assert.equal(noteText({ code: "ROWS_EMPTY", params: { count: 12345 } }, en, where), "12,345 empty rows were left out.");
assert.equal(noteText({ code: "ROWS_EMPTY", params: { count: 12345 } }, de, { lang: "de", locale: "de-DE" }), "12.345 leere Zeilen wurden weggelassen.");
Expand Down
Loading