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 record declares a currency label and mixed entries of kind item or charge. Item entries supply sku and nonnegative safe whole units; charge entries supply description and nonnegative safe whole amountUnits. amountUnits uses the source’s declared accounting unit, with no implicit minor-unit conversion. Split into items and charges, rejecting incomplete entries.
| Case | Input record (JSON) | Expected output (JSON) |
|---|---|---|
| Partition goods and charge entries | {"currency":"HKD","entries":[{"kind":"item","sku":"A","units":2},{"kind":"charge","description":"delivery","amountUnits":10}]} | {"ok":true,"value":{"currency":"HKD","items":[{"sku":"A","units":2}],"charges":[{"description":"delivery","amountUnits":10}]}} |
| Reject incomplete charge | {"currency":"HKD","entries":[{"kind":"charge","description":"delivery"}]} | {"ok":false,"error":"CHARGE_LINE"} |
| Preserve zero goods line | {"currency":"HKD","entries":[{"kind":"item","sku":"A","units":0}]} | {"ok":true,"value":{"currency":"HKD","items":[{"sku":"A","units":0}],"charges":[]}} |
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.
import assert from 'node:assert/strict';
function itemChargeSplit(i){
const fail=error=>({ok:false,error});if(!i||typeof i.currency!=='string'||!i.currency||!Array.isArray(i.entries))return fail('ENTRY_SHAPE');
const items=[],charges=[];
for(const e of i.entries){
if(!e)return fail('ENTRY_KIND');
if(e.kind==='item'){
if(typeof e.sku!=='string'||!e.sku||!Number.isSafeInteger(e.units)||e.units<0)return fail('ITEM_LINE');items.push({sku:e.sku,units:e.units});
}else if(e.kind==='charge'){
if(typeof e.description!=='string'||!e.description||!Number.isSafeInteger(e.amountUnits)||e.amountUnits<0)return fail('CHARGE_LINE');charges.push({description:e.description,amountUnits:e.amountUnits});
}else return fail('ENTRY_KIND');
}
return {ok:true,value:{currency:i.currency,items,charges}};
}
// Original synthetic cases; expected outputs are literal records.
const cases = [
{
"label": "Partition goods and charge entries",
"input": {
"currency": "HKD",
"entries": [
{
"kind": "item",
"sku": "A",
"units": 2
},
{
"kind": "charge",
"description": "delivery",
"amountUnits": 10
}
]
},
"expected": {
"ok": true,
"value": {
"currency": "HKD",
"items": [
{
"sku": "A",
"units": 2
}
],
"charges": [
{
"description": "delivery",
"amountUnits": 10
}
]
}
}
},
{
"label": "Reject incomplete charge",
"input": {
"currency": "HKD",
"entries": [
{
"kind": "charge",
"description": "delivery"
}
]
},
"expected": {
"ok": false,
"error": "CHARGE_LINE"
}
},
{
"label": "Preserve zero goods line",
"input": {
"currency": "HKD",
"entries": [
{
"kind": "item",
"sku": "A",
"units": 0
}
]
},
"expected": {
"ok": true,
"value": {
"currency": "HKD",
"items": [
{
"sku": "A",
"units": 0
}
],
"charges": []
}
}
}
];
for (const sample of cases) {
const actual = itemChargeSplit(sample.input);
assert.deepEqual(actual, sample.expected);
console.log(sample.label, JSON.stringify(actual));
}
Work through the boundary
Does the delivery charge of 10 add ten units to item A?
Show the worked answer
No. A remains units=2. The delivery row becomes a separate charge with amountUnits=10 and the currency label is retained. No payment or stock change is performed.