跳至內容
Astera Technology

按前面的文件標頭歸組明細

把標頭與明細混排的資料,轉成巢狀文件。

下載完整個案資料及參考答案 ↓

來源格式與預期記錄

這是原創假設轉換例子。輸入限下列欄位的普通、無循環 JSON 形記錄,統一記錄格式及拒絕代碼由本例訂明。若換來源,先修改格式約定。

本例每個 header 開始一份文件,其後 line 歸該文件,直至下一個 header。文件參考不可空白或重複;明細要有貨品及非負安全整數數量。標頭出現前已有明細便拒絕;只有標頭也保留空文件。

可修改的假設轉換個案
個案輸入記錄(JSON)預期輸出(JSON)
按前面的標頭歸組{"records":[{"kind":"header","ref":"D1"},{"kind":"line","item":"A","units":2}]}{"ok":true,"value":{"documents":[{"ref":"D1","lines":[{"item":"A","units":2}]}]}}
拒絕無標頭明細{"records":[{"kind":"line","item":"A","units":2}]}{"ok":false,"error":"HEADER_REQUIRED"}
保留無明細文件{"records":[{"kind":"header","ref":"D2"}]}{"ok":true,"value":{"documents":[{"ref":"D2","lines":[]}]}}
下載可編輯 CSV ↓

在本機執行轉換

把完整例子以 JavaScript 模組在 Node.js 執行,內含純轉換函數、三個輸入及寫明的預期輸出,對不上便由斷言停止。拒絕個案回傳 ok=false 及代碼,不回傳半份統一記錄。

完整函數及可執行個案
import assert from 'node:assert/strict';

function headerDocuments(i){
  const fail=error=>({ok:false,error});if(!i||!Array.isArray(i.records))return fail('DOCUMENT_ROWS');
  const documents=[],refs=new Set();let current;
  for(const r of i.records){
    if(!r||typeof r.kind!=='string')return fail('DOCUMENT_KIND');
    if(r.kind==='header'){
      if(typeof r.ref!=='string'||!r.ref||refs.has(r.ref))return fail('DOCUMENT_HEADER');
      refs.add(r.ref);current={ref:r.ref,lines:[]};documents.push(current);
    }else if(r.kind==='line'){
      if(!current)return fail('HEADER_REQUIRED');
      if(typeof r.item!=='string'||!r.item||!Number.isSafeInteger(r.units)||r.units<0)return fail('DOCUMENT_LINE');
      current.lines.push({item:r.item,units:r.units});
    }else return fail('DOCUMENT_KIND');
  }
  return {ok:true,value:{documents}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Group under preceding header",
    "input": {
      "records": [
        {
          "kind": "header",
          "ref": "D1"
        },
        {
          "kind": "line",
          "item": "A",
          "units": 2
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "documents": [
          {
            "ref": "D1",
            "lines": [
              {
                "item": "A",
                "units": 2
              }
            ]
          }
        ]
      }
    }
  },
  {
    "label": "Reject orphan line",
    "input": {
      "records": [
        {
          "kind": "line",
          "item": "A",
          "units": 2
        }
      ]
    },
    "expected": {
      "ok": false,
      "error": "HEADER_REQUIRED"
    }
  },
  {
    "label": "Preserve empty document",
    "input": {
      "records": [
        {
          "kind": "header",
          "ref": "D2"
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "documents": [
          {
            "ref": "D2",
            "lines": []
          }
        ]
      }
    }
  }
];
for (const sample of cases) {
  const actual = headerDocuments(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
下載可執行例子 ↓

判清邊界個案

明細在標頭之前,可否配給後面的標頭?

查看參考答案

不可。本例按前面的標頭歸組,所以該行回傳 HEADER_REQUIRED。D2 只有標頭則有效,保留 lines=[]。

相關資源

系統整合服務 →