跳至內容
Astera Technology

把帶類型比較文字解析成條件記錄

分開整數及文字操作值,解析後不執行。

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

來源格式與預期記錄

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

本例自訂語法為「欄名、單一空格、運算符、單一空格、值」。欄位明列 integer 或 text;運算符有 =、>=、<=,text 只可用 =。整數文字可帶負號,不可帶加號,除 0 外不能有前置零,並須在安全範圍。文字則由必需分隔空格後起全部照存,前後及中間空格都屬值。輸出 eq/gte/lte 標記及原式。

可修改的假設轉換個案
個案輸入記錄(JSON)預期輸出(JSON)
整理具類型條件記錄{"types":{"units":"integer","label":"text"},"expressions":["units >= 3","label = blue"]}{"ok":true,"value":{"predicates":[{"field":"units","operator":"gte","value":3,"source":"units >= 3"},{"field":"label","operator":"eq","value":"blue","source":"label = blue"}]}}
拒絕整數欄出現小數{"types":{"units":"integer"},"expressions":["units = 3.5"]}{"ok":false,"error":"PREDICATE_INTEGER"}
保留文字內空格{"types":{"label":"text"},"expressions":["label = blue sky"]}{"ok":true,"value":{"predicates":[{"field":"label","operator":"eq","value":"blue sky","source":"label = blue sky"}]}}
下載可編輯 CSV ↓

在本機執行轉換

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

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

function typedPredicates(i){
  const fail=error=>({ok:false,error});if(!i||!i.types||typeof i.types!=='object'||Array.isArray(i.types)||!Array.isArray(i.expressions))return fail('PREDICATE_SHAPE');
  const operators={'=':'eq','>=':'gte','<=':'lte'},predicates=[];
  for(const source of i.expressions){
    if(typeof source!=='string')return fail('PREDICATE_TEXT');const m=/^([a-z][a-z0-9_]*) (>=|<=|=) (.+)$/.exec(source);if(!m)return fail('PREDICATE_SYNTAX');
    const [,field,op,raw]=m;if(!Object.hasOwn(i.types,field))return fail('PREDICATE_FIELD');let value;
    if(i.types[field]==='integer'){
      if(!/^-?(0|[1-9]\d*)$/.test(raw))return fail('PREDICATE_INTEGER');value=Number(raw);if(!Number.isSafeInteger(value))return fail('PREDICATE_INTEGER');if(Object.is(value,-0))value=0;
    }else if(i.types[field]==='text'){if(op!=='=')return fail('PREDICATE_TEXT_OPERATOR');value=raw;}
    else return fail('PREDICATE_TYPE');
    predicates.push({field,operator:operators[op],value,source});
  }
  return {ok:true,value:{predicates}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Parse typed constraint records",
    "input": {
      "types": {
        "units": "integer",
        "label": "text"
      },
      "expressions": [
        "units >= 3",
        "label = blue"
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "predicates": [
          {
            "field": "units",
            "operator": "gte",
            "value": 3,
            "source": "units >= 3"
          },
          {
            "field": "label",
            "operator": "eq",
            "value": "blue",
            "source": "label = blue"
          }
        ]
      }
    }
  },
  {
    "label": "Reject fraction for integer field",
    "input": {
      "types": {
        "units": "integer"
      },
      "expressions": [
        "units = 3.5"
      ]
    },
    "expected": {
      "ok": false,
      "error": "PREDICATE_INTEGER"
    }
  },
  {
    "label": "Preserve text spacing",
    "input": {
      "types": {
        "label": "text"
      },
      "expressions": [
        "label = blue sky"
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "predicates": [
          {
            "field": "label",
            "operator": "eq",
            "value": "blue sky",
            "source": "label = blue sky"
          }
        ]
      }
    }
  }
];
for (const sample of cases) {
  const actual = typedPredicates(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
下載可執行例子 ↓

判清邊界個案

units = 3.5 為何拒絕,label = blue sky 卻保留?

查看參考答案

units 已訂為整數,小數回傳 PREDICATE_INTEGER。label 是文字,完整操作值 "blue sky" 成為字串;函數未執行任何條件。

相關資源