
A price badge uses different visual sizes for the currency symbol and amount. Splitting the final string at a fixed character position breaks across currencies. Parts preserve the order and spacing selected by the locale. A negative sign is a separate piece in the usual configuration.
Isolate the operation in one function
The example keeps the input to Intl.NumberFormat.formatToParts outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
function evaluate(value) {
return new Intl.NumberFormat('en-IN',{style:'currency',currency:'INR'}).formatToParts(value).map(p=>[p.type,p.value]);
}
const samples = [5, 100000.45, -12];
for (const sample of samples) console.log(evaluate(sample));Trace the result
| Input | Return value |
|---|---|
| 5 | [["currency", "₹"], ["integer", "5"], ["decimal", "."], ["fraction", "00"]] |
| 100000.45 | [["currency", "₹"], ["integer", "1"], ["group", ","], ["integer", "00"], ["group", ","], ["integer", "000"], ["decimal", "."], ["fraction", "45"]] |
| -12 | [["minusSign", "-"], ["currency", "₹"], ["integer", "12"], ["decimal", "."], ["fraction", "00"]] |
For 5, the observed output is [["currency", "₹"], ["integer", "5"], ["decimal", "."], ["fraction", "00"]]. With 100000.45, it becomes [["currency", "₹"], ["integer", "1"], ["group", ","], ["integer", "00"], ["group", ","], ["integer", "000"], ["decimal", "."], ["fraction", "45"]]. These are useful regression fixtures because an integration test can send both through the same entry point. A test of only the first case can stay green after a change that affects the other.
For -12, the operation returns 5 top-level items. An assertion on that count would miss a change in their contents. Capture the expected sequence or compare membership explicitly if order has no meaning in the product.
What the calling feature must decide
Keep one coherent accessible label if presentation wraps pieces into separate styled elements.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns an ordered collection of values. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
A second check against a changed input
Inspect the DOM after styling the fractional part separately. The visual result can look right while a screen reader announces the pieces in an unusual order. Test a negative price because the sign is part of the meaningful value.
The same JavaScript function was also run with 0 / -1 / 6. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| 0 | [["currency", "₹"], ["integer", "0"], ["decimal", "."], ["fraction", "00"]] |
| -1 | [["minusSign", "-"], ["currency", "₹"], ["integer", "1"], ["decimal", "."], ["fraction", "00"]] |
| 6 | [["currency", "₹"], ["integer", "6"], ["decimal", "."], ["fraction", "00"]] |
JavaScript standard library reference · MDN JavaScript language guide
Sources and further reading
Cover photograph: Pointing stick of a Lenovo ThinkPad keyboard-0899 by Raimond Spekking. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
