Skip to content
Astera Technology

Rebuild repeating lines from sparse numbered slots

Sort supplied positions while preserving gaps and zero lines.

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 local source stores line objects under positive integer slot keys written without leading zeros. Each line has an item label and a nonnegative safe integer quantity. Sort by numeric slot and retain the original slot on the output. Missing positions create no record. Empty slots produces an empty list.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Keep source slots{"slots":{"1":{"item":"A","units":0},"3":{"item":"C","units":2}}}{"ok":true,"value":{"lines":[{"slot":1,"item":"A","units":0},{"slot":3,"item":"C","units":2}]}}
Reject negative quantity{"slots":{"1":{"item":"A","units":-1}}}{"ok":false,"error":"SLOT_LINE"}
Preserve empty slots{"slots":{}}{"ok":true,"value":{"lines":[]}}
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 repeatingSlots(i){
  const fail=error=>({ok:false,error});if(!i||!i.slots||typeof i.slots!=='object'||Array.isArray(i.slots))return fail('SLOT_OBJECT');
  const lines=[];
  for(const [key,r]of Object.entries(i.slots)){
    const slot=Number(key);if(!/^[1-9]\d*$/.test(key)||!Number.isSafeInteger(slot))return fail('SLOT_POSITION');
    if(!r||typeof r.item!=='string'||!r.item||!Number.isSafeInteger(r.units)||r.units<0)return fail('SLOT_LINE');
    lines.push({slot,item:r.item,units:r.units});
  }
  lines.sort((a,b)=>a.slot-b.slot);return {ok:true,value:{lines}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Keep source slots",
    "input": {
      "slots": {
        "1": {
          "item": "A",
          "units": 0
        },
        "3": {
          "item": "C",
          "units": 2
        }
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "lines": [
          {
            "slot": 1,
            "item": "A",
            "units": 0
          },
          {
            "slot": 3,
            "item": "C",
            "units": 2
          }
        ]
      }
    }
  },
  {
    "label": "Reject negative quantity",
    "input": {
      "slots": {
        "1": {
          "item": "A",
          "units": -1
        }
      }
    },
    "expected": {
      "ok": false,
      "error": "SLOT_LINE"
    }
  },
  {
    "label": "Preserve empty slots",
    "input": {
      "slots": {}
    },
    "expected": {
      "ok": true,
      "value": {
        "lines": []
      }
    }
  }
];
for (const sample of cases) {
  const actual = repeatingSlots(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

Slots 1 and 3 exist. Should a blank line be inserted for slot 2?

Show the worked answer

No. Output is slot 1 with A=0, then slot 3 with C=2. The gap is preserved, while the explicitly supplied zero line remains.

Related resources