Skip to content
Astera Technology

Retain parcel membership while ordering parcel records

Keep empty parcels and each parcel’s own 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.

Every parcel has a unique positive safe integer number and a contents array. Contents require sku text and nonnegative safe whole units. Map number to parcelNo, copy its lines, and sort parcels numerically. Empty parcels remain; an empty source yields an empty consignment.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Retain nested parcel contents{"parcels":[{"number":2,"contents":[{"sku":"A","units":1}]},{"number":1,"contents":[]}]}{"ok":true,"value":{"consignment":[{"parcelNo":1,"lines":[]},{"parcelNo":2,"lines":[{"sku":"A","units":1}]}]}}
Reject repeated parcel number{"parcels":[{"number":1,"contents":[]},{"number":1,"contents":[]}]}{"ok":false,"error":"PARCEL_NUMBER"}
Preserve empty consignment{"parcels":[]}{"ok":true,"value":{"consignment":[]}}
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 parcelContents(i){
  const fail=error=>({ok:false,error});if(!i||!Array.isArray(i.parcels))return fail('PARCEL_LIST');
  const seen=new Set(),consignment=[];
  for(const p of i.parcels){
    if(!p||!Number.isSafeInteger(p.number)||p.number<1||seen.has(p.number)||!Array.isArray(p.contents))return fail('PARCEL_NUMBER');seen.add(p.number);
    if(p.contents.some(c=>!c||typeof c.sku!=='string'||!c.sku||!Number.isSafeInteger(c.units)||c.units<0))return fail('PARCEL_CONTENT');
    consignment.push({parcelNo:p.number,lines:p.contents.map(c=>({sku:c.sku,units:c.units}))});
  }
  consignment.sort((a,b)=>a.parcelNo-b.parcelNo);return {ok:true,value:{consignment}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Retain nested parcel contents",
    "input": {
      "parcels": [
        {
          "number": 2,
          "contents": [
            {
              "sku": "A",
              "units": 1
            }
          ]
        },
        {
          "number": 1,
          "contents": []
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "consignment": [
          {
            "parcelNo": 1,
            "lines": []
          },
          {
            "parcelNo": 2,
            "lines": [
              {
                "sku": "A",
                "units": 1
              }
            ]
          }
        ]
      }
    }
  },
  {
    "label": "Reject repeated parcel number",
    "input": {
      "parcels": [
        {
          "number": 1,
          "contents": []
        },
        {
          "number": 1,
          "contents": []
        }
      ]
    },
    "expected": {
      "ok": false,
      "error": "PARCEL_NUMBER"
    }
  },
  {
    "label": "Preserve empty consignment",
    "input": {
      "parcels": []
    },
    "expected": {
      "ok": true,
      "value": {
        "consignment": []
      }
    }
  }
];
for (const sample of cases) {
  const actual = parcelContents(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

Does sorting parcel 1 before parcel 2 move A into parcel 1?

Show the worked answer

No. Parcel 1 retains lines=[] and parcel 2 retains A=1. Sorting changes record order, not membership; the mapped record does not establish dispatch.

Related resources