跳至內容
Astera Technology

按軸標籤填入長、闊、高

按軸的標籤配尺寸,不靠輸入先後。

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

來源格式與預期記錄

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

本例 L、W、H 各須有一項,以正安全整數毫米表示。欠軸、重複或未知標籤均拒絕。輸出固定為 lengthMm、widthMm、heightMm,來源排列先後不影響配對。

可修改的假設轉換個案
個案輸入記錄(JSON)預期輸出(JSON)
按軸標籤分配{"axes":[{"axis":"W","mm":20},{"axis":"H","mm":10},{"axis":"L","mm":30}]}{"ok":true,"value":{"lengthMm":30,"widthMm":20,"heightMm":10}}
拒絕重複軸{"axes":[{"axis":"L","mm":30},{"axis":"L","mm":20}]}{"ok":false,"error":"AXIS_VALUE"}
拒絕欠高度{"axes":[{"axis":"L","mm":30},{"axis":"W","mm":20}]}{"ok":false,"error":"AXIS_MISSING"}
下載可編輯 CSV ↓

在本機執行轉換

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

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

function axisDimensions(i){
  const fail=error=>({ok:false,error});if(!i||!Array.isArray(i.axes))return fail('AXIS_LIST');
  const values={};
  for(const a of i.axes){if(!a||!['L','W','H'].includes(a.axis)||Object.hasOwn(values,a.axis)||!Number.isSafeInteger(a.mm)||a.mm<=0)return fail('AXIS_VALUE');values[a.axis]=a.mm;}
  if(!['L','W','H'].every(x=>Object.hasOwn(values,x)))return fail('AXIS_MISSING');
  return {ok:true,value:{lengthMm:values.L,widthMm:values.W,heightMm:values.H}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Map axes by label",
    "input": {
      "axes": [
        {
          "axis": "W",
          "mm": 20
        },
        {
          "axis": "H",
          "mm": 10
        },
        {
          "axis": "L",
          "mm": 30
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "lengthMm": 30,
        "widthMm": 20,
        "heightMm": 10
      }
    }
  },
  {
    "label": "Reject repeated axis",
    "input": {
      "axes": [
        {
          "axis": "L",
          "mm": 30
        },
        {
          "axis": "L",
          "mm": 20
        }
      ]
    },
    "expected": {
      "ok": false,
      "error": "AXIS_VALUE"
    }
  },
  {
    "label": "Reject missing height",
    "input": {
      "axes": [
        {
          "axis": "L",
          "mm": 30
        },
        {
          "axis": "W",
          "mm": 20
        }
      ]
    },
    "expected": {
      "ok": false,
      "error": "AXIS_MISSING"
    }
  }
];
for (const sample of cases) {
  const actual = axisDimensions(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
下載可執行例子 ↓

判清邊界個案

陣列先 W=20、H=10,最後 L=30,哪個是長?

查看參考答案

長 30 mm、闊 20 mm、高 10 mm。長由 L 標籤決定,重複 L 不能代替欠缺的 H。

相關資源