Set the decision and its input
The literal grammar uses U or S, two decimal places and kg or g. Base messages are unstable1.20kg then stable1.25kg. The stated rule selects the first stable message:1,250,000mg with source unit kg. Taking the first readable number captures an ineligible unstable reading.
| Case | Input JSON |
|---|---|
| Base test | {"messages":["U|1.20|kg","S|1.25|kg"]} |
Run the supplied comparison
The programme parses the supplied strings and scales integer hundredths. It does not connect to a real scale, establish calibration or invent a device stability tolerance.
| Path | Output JSON |
|---|---|
| Faulty path | {"stableCaptured":false,"milligrams":1200000,"sourceUnit":"kg"} |
| Required path | {"stableCaptured":true,"milligrams":1250000,"sourceUnit":"kg"} |
import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
const messages=i.messages.map(s=>{const match=/^([US])\|(\d+)\.(\d{2})\|(kg|g)$/.exec(s);if(!match)throw new Error('Outside the literal device grammar');return {stable:match[1]==='S',hundredths:Number(match[2])*100+Number(match[3]),unit:match[4]};});
const captured=corrected?messages.find(m=>m.stable):messages[0];if(!captured)return {held:true,reason:'no-stable-reading'};
return {stableCaptured:captured.stable,milligrams:captured.hundredths*(captured.unit==='kg'?10000:10),sourceUnit:captured.unit};
};
const cases=[{"input":{"messages":["U|1.20|kg","S|1.25|kg"]},"corrected":false,"expected":{"stableCaptured":false,"milligrams":1200000,"sourceUnit":"kg"}},{"input":{"messages":["U|1.20|kg","S|1.25|kg"]},"corrected":true,"expected":{"stableCaptured":true,"milligrams":1250000,"sourceUnit":"kg"}},{"input":{"messages":["S|1250.00|g"]},"corrected":true,"expected":{"stableCaptured":true,"milligrams":1250000,"sourceUnit":"g"}},{"input":{"messages":["U|1.20|kg"]},"corrected":true,"expected":{"held":true,"reason":"no-stable-reading"}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
Choose from the boundary evidence
Stable1250.00g yields the same milligrams while retaining g. An all-U sequence is held as no-stable-reading.
- Parse the stated grammar
- Select a stable message
- Retain unit and scaled value
Hold absent stability evidence rather than use the last unstable number.
| Condition | Input JSON | Expected output JSON |
|---|---|---|
| stable gram representation | {"messages":["S|1250.00|g"]} | {"stableCaptured":true,"milligrams":1250000,"sourceUnit":"g"} |
| no stable message | {"messages":["U|1.20|kg"]} | {"held":true,"reason":"no-stable-reading"} |
Which base reading is eligible?
Show the worked answer
Stable1.25kg. The earlier1.20kg remains ineligible under the supplied stable-flag rule.