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:
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
TypeScript Version: 4.2.0-dev.20201221
Search Terms:
NumberFormat.formatToParts,compactExpected behavior:
part: 'compact'is a valid part type, as per EMCA 402, section 12.1.7 (PartitionNotationSubPattern), step 4. - c. - iv. - 2.: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.NumberFormatPartTypestype should include"compact".Related Issues:
Code
Output
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