feat: ツールチップ文言のプレースホルダー置換ロジックを追加
This commit is contained in:
parent
16732f0ab0
commit
0405c3a089
26
NodeSrv/apps/healthcheck-survey-bot/src/lib/templateFill.js
Normal file
26
NodeSrv/apps/healthcheck-survey-bot/src/lib/templateFill.js
Normal file
@ -0,0 +1,26 @@
|
||||
function isUnsetSentinel(value) {
|
||||
return typeof value === "string" && value.startsWith("1899");
|
||||
}
|
||||
|
||||
function fillTemplate(template, columns, valueHash) {
|
||||
const labelToColumnName = new Map();
|
||||
for (const column of columns) {
|
||||
if (column.LabelText) {
|
||||
labelToColumnName.set(column.LabelText, column.ColumnName);
|
||||
}
|
||||
}
|
||||
|
||||
return template.replace(/\{([^{}]+)\}/g, (matched, label) => {
|
||||
const columnName = labelToColumnName.get(label);
|
||||
if (!columnName) {
|
||||
return matched;
|
||||
}
|
||||
const value = valueHash[columnName];
|
||||
if (value === undefined || value === null || value === "" || isUnsetSentinel(value)) {
|
||||
return "未設定";
|
||||
}
|
||||
return String(value);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { fillTemplate };
|
||||
@ -0,0 +1,35 @@
|
||||
// test/templateFill.test.js
|
||||
const { test } = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const { fillTemplate } = require("../src/lib/templateFill");
|
||||
|
||||
const columns = [
|
||||
{ ColumnName: "Class003", LabelText: "検査機関" },
|
||||
{ ColumnName: "Date001", LabelText: "検査日" },
|
||||
];
|
||||
|
||||
test("プレースホルダーをレコード値で置換する", () => {
|
||||
const result = fillTemplate(
|
||||
"検査機関: {検査機関}\n日程: {検査日}",
|
||||
columns,
|
||||
{ Class003: "next健診クリニック", Date001: "2026-04-01T00:00:00" }
|
||||
);
|
||||
assert.strictEqual(result, "検査機関: next健診クリニック\n日程: 2026-04-01T00:00:00");
|
||||
});
|
||||
|
||||
test("未設定の日付センチネル値は「未設定」に変換する", () => {
|
||||
const result = fillTemplate("日程: {検査日}", columns, {
|
||||
Date001: "1899-12-30T00:00:00",
|
||||
});
|
||||
assert.strictEqual(result, "日程: 未設定");
|
||||
});
|
||||
|
||||
test("値が無い列は「未設定」に変換する", () => {
|
||||
const result = fillTemplate("検査機関: {検査機関}", columns, {});
|
||||
assert.strictEqual(result, "検査機関: 未設定");
|
||||
});
|
||||
|
||||
test("対応するラベルが見つからないプレースホルダーはそのまま残す", () => {
|
||||
const result = fillTemplate("不明: {存在しないラベル}", columns, {});
|
||||
assert.strictEqual(result, "不明: {存在しないラベル}");
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user