From 0405c3a089b257d9e1ef7912173bfd18090f2cca Mon Sep 17 00:00:00 2001 From: Kenichiro NOGI Date: Sat, 5 Sep 2026 14:32:42 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=84=E3=83=BC=E3=83=AB=E3=83=81?= =?UTF-8?q?=E3=83=83=E3=83=97=E6=96=87=E8=A8=80=E3=81=AE=E3=83=97=E3=83=AC?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=9B=E3=83=AB=E3=83=80=E3=83=BC=E7=BD=AE?= =?UTF-8?q?=E6=8F=9B=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/templateFill.js | 26 ++++++++++++++ .../test/templateFill.test.js | 35 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 NodeSrv/apps/healthcheck-survey-bot/src/lib/templateFill.js create mode 100644 NodeSrv/apps/healthcheck-survey-bot/test/templateFill.test.js diff --git a/NodeSrv/apps/healthcheck-survey-bot/src/lib/templateFill.js b/NodeSrv/apps/healthcheck-survey-bot/src/lib/templateFill.js new file mode 100644 index 00000000..c077164e --- /dev/null +++ b/NodeSrv/apps/healthcheck-survey-bot/src/lib/templateFill.js @@ -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 }; diff --git a/NodeSrv/apps/healthcheck-survey-bot/test/templateFill.test.js b/NodeSrv/apps/healthcheck-survey-bot/test/templateFill.test.js new file mode 100644 index 00000000..e5deb8b5 --- /dev/null +++ b/NodeSrv/apps/healthcheck-survey-bot/test/templateFill.test.js @@ -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, "不明: {存在しないラベル}"); +});