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 input is digits:digits, with nonnegative safe integers and a strictly positive denominator. Reduce both by their greatest common divisor and retain the source text. A zero numerator normalises to 0/1. A zero denominator is rejected. This record represents a ratio; it does not allocate quantities.
| Case | Input record (JSON) | Expected output (JSON) |
|---|---|---|
| Reduce ratio without losing source | {"ratio":"6:9"} | {"ok":true,"value":{"numerator":2,"denominator":3,"source":"6:9"}} |
| Reject zero denominator | {"ratio":"3:0"} | {"ok":false,"error":"RATIO_RANGE"} |
| Preserve zero numerator | {"ratio":"0:5"} | {"ok":true,"value":{"numerator":0,"denominator":1,"source":"0:5"}} |
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 ratioRecord(i){
const fail=error=>({ok:false,error});if(!i||typeof i.ratio!=='string'||!/^\d+:\d+$/.test(i.ratio))return fail('RATIO_TEXT');
const [n,d]=i.ratio.split(':').map(Number);if(!Number.isSafeInteger(n)||!Number.isSafeInteger(d)||d<=0)return fail('RATIO_RANGE');
let a=n,b=d;while(b){const remainder=a%b;a=b;b=remainder;}
return {ok:true,value:{numerator:n/a,denominator:d/a,source:i.ratio}};
}
// Original synthetic cases; expected outputs are literal records.
const cases = [
{
"label": "Reduce ratio without losing source",
"input": {
"ratio": "6:9"
},
"expected": {
"ok": true,
"value": {
"numerator": 2,
"denominator": 3,
"source": "6:9"
}
}
},
{
"label": "Reject zero denominator",
"input": {
"ratio": "3:0"
},
"expected": {
"ok": false,
"error": "RATIO_RANGE"
}
},
{
"label": "Preserve zero numerator",
"input": {
"ratio": "0:5"
},
"expected": {
"ok": true,
"value": {
"numerator": 0,
"denominator": 1,
"source": "0:5"
}
}
}
];
for (const sample of cases) {
const actual = ratioRecord(sample.input);
assert.deepEqual(actual, sample.expected);
console.log(sample.label, JSON.stringify(actual));
}
Work through the boundary
Why do 6:9 and 0:5 become different canonical forms?
Show the worked answer
The common divisor of 6 and 9 is 3, producing 2/3. The common divisor of 0 and 5 is 5, producing 0/1; both retain their original ratio text.