Skip to content

compact missing from Int.NumberFormat.formatToParts  #42063

Closed
@mjpieters

Description

@mjpieters

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

  1. [...]
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions