Skip to content
Astera Technology

Parse typed comparison text into predicate records

Keep integer and text operands distinct without executing the expression.

Download complete case notes and answer keys ↓

Source contract and expected records

This is an original, fully synthetic mapping example. Inputs are plain, acyclic JSON-shaped records with the fields shown; the canonical shape and rejection codes belong to this example. Adapt the contract before using a different source.

This custom syntax is field, one space, operator, one space, value. Declared types are integer or text; operators are =, >= and <=, but text accepts = only. Integer text permits an optional minus sign, no plus sign, and no leading zeros except 0; it must be in the safe range. For text, every character after the required separator belongs to the value, including leading, internal and trailing spaces. Return operator tags eq/gte/lte and retain source expressions.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Parse typed constraint records{"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"}]}}
Reject fraction for integer field{"types":{"units":"integer"},"expressions":["units = 3.5"]}{"ok":false,"error":"PREDICATE_INTEGER"}
Preserve text spacing{"types":{"label":"text"},"expressions":["label = blue sky"]}{"ok":true,"value":{"predicates":[{"field":"label","operator":"eq","value":"blue sky","source":"label = blue sky"}]}}
Download editable CSV ↓

Run the local transformation

Run the complete example as a JavaScript module in Node.js. It contains the pure mapping function, all three inputs and literal expected outputs; assertions stop on a mismatch. A rejected case returns ok=false with an error code, rather than a partial canonical record.

Complete function and executable cases
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));
}
Download runnable example ↓

Work through the boundary

Why is units = 3.5 rejected but label = blue sky retained?

Show the worked answer

units is declared integer, so a fraction returns PREDICATE_INTEGER. label is text: its full operand "blue sky" becomes a string value. Neither predicate is executed by this mapper.

Related resources