add key()

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-07-13 12:47:04 +08:00
parent e9b99f8243
commit bfc4b9fc67
15 changed files with 221 additions and 76 deletions
+22 -3
View File
@@ -3,15 +3,34 @@ import * as XLSX from "xlsx";
const SKIP_IDS = new Set(["select", "actions"]);
// ─── Flatten a value into something a spreadsheet cell can display ────────────
// Array-of-object columns (e.g. `groups`: [{group_id, name, group_code}]) would
// otherwise hit Array.prototype.toString → "[object Object]" per item.
function flattenForExport(value) {
if (Array.isArray(value)) {
return value
.map((item) => (item && typeof item === "object")
? (item.name ?? item.label ?? item.group_code ?? JSON.stringify(item))
: item)
.join(", ");
}
if (value && typeof value === "object") {
return value.name ?? value.label ?? JSON.stringify(value);
}
return value;
}
// ─── Resolve dot-notation or direct key from a row ────────────────────────────
function resolveValue(row, key) {
if (!key) return "";
// ─── Try direct key first e.g. "email", "acc_type" ────────────────────────
if (key in row) return row[key] ?? "";
const raw = key in row
? row[key]
// ─── Dot-notation fallback e.g. "personal_info.name.full_name" ──────────
: key.split(".").reduce((acc, part) => acc?.[part] ?? "", row);
// ─── Dot-notation fallback e.g. "personal_info.name.full_name" ────────────
return key.split(".").reduce((acc, part) => acc?.[part] ?? "", row) ?? "";
return flattenForExport(raw) ?? "";
}
// ─── Flatten a nested row based on attributes ─────────────────────────────────