Skip to content

compact missing from Int.NumberFormat.formatToParts #42063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjpieters opened this issue Dec 21, 2020 · 4 comments
Closed

compact missing from Int.NumberFormat.formatToParts #42063

mjpieters opened this issue Dec 21, 2020 · 4 comments

Comments

@mjpieters
Copy link
Contributor

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

@MartinJohns
Copy link
Contributor

You are targetting ES3, but the 'compact' notation is an ES2020 feature.

@mjpieters
Copy link
Contributor Author

mjpieters commented Dec 21, 2020

You are targetting ES3, but the 'compact' notation is an ES2020 feature.

I'm not. I don't know why the playground link, when opened, indeed shows 'ES3', but the config as included in the issue uses "target": "Latest" and locally I have "target": "esnext". Looks like this is a playground bug as the playground URL includes target=99; I reported it to the TypeScript-Website repo.

I've also linked to the source code for esnext.intl.d.ts here, where you can see that the NumberFormatPartTypes type definition is clearly missing "compact" as an option.

@mjpieters
Copy link
Contributor Author

I've found a few more missing types:

  • 'unknown'
  • 'exponentSeparator'
  • 'exponentMinusSign'
  • 'exponentInteger'

(also reported to MDN).

@andrewbranch
Copy link
Member

Fixed by #42066

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants