Closed
Description
TypeScript Version: 4.2.0-dev.20201221
Search Terms: NumberFormat.formatToParts
, compact
Expected behavior:
part: 'compact'
is a valid part type, as per EMCA 402, section 12.1.7 (PartitionNotationSubPattern), step 4. - c. - iv. - 2.:
iv. Else if p is equal to "compactSymbol", then
- [...]
- Append a new Record { [[Type]]: "compact", [[Value]]: compactSymbol } as the last element of result.
Note that the MDN documentation for Intl.NumberFormat.prototype.formatToParts()
doesn't mention this part type either; see mdn/content#516.
Actual behavior:
The Intl.NumberFormatPartTypes
type should include "compact"
.
Related Issues:
- Intl.formatByParts() doesn't support the "unit" part type #42004 (Intl.formatByParts() doesn't support the "unit" part type, different part type, same area).
Code
const compactFormat = Intl.NumberFormat('en', {
notation: 'compact',
compactDisplay: 'short',
style: 'decimal',
maximumFractionDigits: 3,
})
// formatValue(42174812) => {formatted: '42.175', magnitude: 'M'}
function formatValue(value: number): {formatted: string, magnitude: string} {
let magnitude = ''
return {
formatted: compactFormat
.formatToParts(value)
.reduce((formatted, part) => {
switch (part.type) {
case 'compact':
magnitude = magnitude + part.value
break
default:
formatted = formatted + part.value
}
return formatted
}, ''),
magnitude: magnitude
}
}
console.log(formatValue(42174812))
Output
"use strict";
const compactFormat = Intl.NumberFormat('en', {
notation: 'compact',
compactDisplay: 'short',
style: 'decimal',
maximumFractionDigits: 3,
});
// formatValue(42174812) => {formatted: '42.175', magnitude: 'M'}
function formatValue(value) {
let magnitude = '';
return {
formatted: compactFormat
.formatToParts(value)
.reduce((formatted, part) => {
switch (part.type) {
case 'compact':
magnitude = magnitude + part.value;
break;
default:
formatted = formatted + part.value;
}
return formatted;
}, ''),
magnitude: magnitude
};
}
console.log(formatValue(42174812));
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "Latest",
"module": "ESNext"
}
}
Playground Link: Provided
Metadata
Metadata
Assignees
Labels
No labels