訂明本例驗收任務
假設出貨表的 requested 及 delivered 父表頭都各有 units 子欄。路徑與同一行數值用對齊清單提供,輸出須帶完整兩層路徑,恰好兩個非空文字名稱;完整路徑重複或兩清單長度不同便拒絕該行,零仍是已觀察值。
- 父表頭與子欄一起讀
- 每個不同完整路徑核對一值
- 輸出完整路徑與值
路徑不清楚或缺失,須覆核版面。
使用已填個案
| 個案 | 已提供輸入 | 預期結構化結果 | 判斷及原因 |
|---|---|---|---|
| 不同父欄下同名子欄 | {"paths":[["requested","units"],["delivered","units"]],"values":[8,5]} | {"fields":[{"path":["requested","units"],"value":8},{"path":["delivered","units"],"value":5}]} | 要求八與已交五保留不同父路徑。 |
| 已交零有記錄 | {"paths":[["requested","units"],["delivered","units"]],"values":[4,0]} | {"fields":[{"path":["requested","units"],"value":4},{"path":["delivered","units"],"value":0}]} | 零保留在 delivered.units,不列作欠欄。 |
| 子欄欠父表頭 | {"paths":[["units"]],"values":[8]} | {"error":"header-association"} | 只有 units,不是所需父表頭與子欄兩層關係。 |
| 完整路徑重複 | {"paths":[["requested","units"],["requested","units"]],"values":[8,5]} | {"error":"header-association"} | 兩值同指一個完整欄位,不可靜默覆寫。 |
逐步處理 duplicate-path 個案,須得到甚麼結果?原因是甚麼?
查看參考答案
{"error":"header-association"}。兩值同指一個完整欄位,不可靜默覆寫。
執行完整本機驗算
import assert from 'node:assert/strict';
function evaluate(x){
if(x.paths.some(path=>!Array.isArray(path)||path.length!==2||path.some(part=>typeof part!=='string'||!part.trim())))return {error:'header-association'};
const keys=x.paths.map(p=>JSON.stringify(p));
if(x.paths.length!==x.values.length||new Set(keys).size!==keys.length)return {error:'header-association'};
return {fields:x.paths.map((path,i)=>({path,value:x.values[i]}))};
}
const cases = [
{
"id": "same-leaf",
"input": {
"paths": [
[
"requested",
"units"
],
[
"delivered",
"units"
]
],
"values": [
8,
5
]
},
"expected": {
"fields": [
{
"path": [
"requested",
"units"
],
"value": 8
},
{
"path": [
"delivered",
"units"
],
"value": 5
}
]
}
},
{
"id": "observed-zero",
"input": {
"paths": [
[
"requested",
"units"
],
[
"delivered",
"units"
]
],
"values": [
4,
0
]
},
"expected": {
"fields": [
{
"path": [
"requested",
"units"
],
"value": 4
},
{
"path": [
"delivered",
"units"
],
"value": 0
}
]
}
},
{
"id": "leaf-without-parent",
"input": {
"paths": [
[
"units"
]
],
"values": [
8
]
},
"expected": {
"error": "header-association"
}
},
{
"id": "duplicate-path",
"input": {
"paths": [
[
"requested",
"units"
],
[
"requested",
"units"
]
],
"values": [
8,
5
]
},
"expected": {
"error": "header-association"
}
}
];
for (const c of cases) assert.deepEqual(evaluate(c.input), c.expected, c.id);
console.log(JSON.stringify({checked:cases.length,caseIds:cases.map(c=>c.id)}));
原版面關係跟讀取行一併保留。個案測合併表頭;未執行 OCR,也沒有核實實物存量。