Skip to content
Astera Technology

Convert explicit service flags into selected option keys

Keep false flags and require the full declared option set.

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.

The original flags object must contain exactly setup, training and export, each a Boolean. Return true keys in that fixed order and retain all three flags. Missing, unknown or non-Boolean flags reject the mapping. All false is a complete selection record with an empty selected list.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Decode selected flag fields{"flags":{"setup":true,"training":false,"export":true}}{"ok":true,"value":{"selected":["setup","export"],"sourceFlags":{"setup":true,"training":false,"export":true}}}
Reject missing flag{"flags":{"setup":true,"training":false}}{"ok":false,"error":"FLAG_MISSING"}
Preserve all-false selection{"flags":{"setup":false,"training":false,"export":false}}{"ok":true,"value":{"selected":[],"sourceFlags":{"setup":false,"training":false,"export":false}}}
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 serviceFlags(i){
  const fail=error=>({ok:false,error});if(!i||!i.flags||typeof i.flags!=='object'||Array.isArray(i.flags))return fail('FLAG_OBJECT');
  const keys=['setup','training','export'];
  if(Object.keys(i.flags).some(k=>!keys.includes(k)))return fail('FLAG_UNKNOWN');
  if(keys.some(k=>!Object.hasOwn(i.flags,k)))return fail('FLAG_MISSING');
  if(keys.some(k=>typeof i.flags[k]!=='boolean'))return fail('FLAG_VALUE');
  return {ok:true,value:{selected:keys.filter(k=>i.flags[k]),sourceFlags:{setup:i.flags.setup,training:i.flags.training,export:i.flags.export}}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Decode selected flag fields",
    "input": {
      "flags": {
        "setup": true,
        "training": false,
        "export": true
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "selected": [
          "setup",
          "export"
        ],
        "sourceFlags": {
          "setup": true,
          "training": false,
          "export": true
        }
      }
    }
  },
  {
    "label": "Reject missing flag",
    "input": {
      "flags": {
        "setup": true,
        "training": false
      }
    },
    "expected": {
      "ok": false,
      "error": "FLAG_MISSING"
    }
  },
  {
    "label": "Preserve all-false selection",
    "input": {
      "flags": {
        "setup": false,
        "training": false,
        "export": false
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "selected": [],
        "sourceFlags": {
          "setup": false,
          "training": false,
          "export": false
        }
      }
    }
  }
];
for (const sample of cases) {
  const actual = serviceFlags(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

Why must a missing export flag fail while export=false is valid?

Show the worked answer

Missing supplies no answer; false explicitly supplies an unselected answer. The first case selects setup and export while retaining training=false; it does not place a service order.

Related resources