feat(import): auto-map headerless data by target order
This commit is contained in:
1 parent
b57b3873d9
commit
0b360aac7d
5 files changed
+57
-4
No files matched your search
@@ -95,6 +95,16 @@ describe("tableImport", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("maps headerless columns by position even when generated names match a different target", () => {
|
||||
expect(autoMapImportColumns(["column_1", "column_2"], ["column_2", "column_1"], "position")).toEqual({ column_1: "column_2", column_2: "column_1" });
|
||||
});
|
||||
|
||||
it("skips excess source columns and leaves excess target columns unmapped", () => {
|
||||
expect(autoMapImportColumns(["column_1", "column_2", "column_3"], ["id", "name"], "position")).toEqual({ column_1: "id", column_2: "name", column_3: "" });
|
||||
expect(autoMapImportColumns(["column_1"], ["id", "name"], "position")).toEqual({ column_1: "id" });
|
||||
expect(autoMapImportColumns(["column_1"], [], "position")).toEqual({ column_1: "" });
|
||||
});
|
||||
|
||||
it("rejects empty mappings and duplicate target columns", () => {
|
||||
expect(validateImportMappings([])).toEqual({
|
||||
valid: false,
|
||||
|
||||
@@ -136,7 +136,10 @@ export function normalizeImportColumnName(name: string): string {
|
||||
return name.trim().toLowerCase().replace(/[_-]+/g, " ").replace(/\s+/g, " ");
|
||||
}
|
||||
|
||||
export function autoMapImportColumns(sourceColumns: string[], targetColumns: string[]): Record<string, string> {
|
||||
export function autoMapImportColumns(sourceColumns: string[], targetColumns: string[], mode: "name" | "position" = "name"): Record<string, string> {
|
||||
if (mode === "position") {
|
||||
return Object.fromEntries(sourceColumns.map((source, index) => [source, targetColumns[index] ?? IMPORT_SKIP_TARGET]));
|
||||
}
|
||||
const exactTargets = new Map(targetColumns.map((column) => [column, column]));
|
||||
const normalizedTargets = new Map(targetColumns.map((column) => [normalizeImportColumnName(column), column]));
|
||||
|
||||
|
||||
Reference in new issue
Block a user