(core) Generic number parsing functionality.

Summary:
Added NumberParse.ts, counterpart of NumberFormat.ts.

Contains generic functionality for parsing numbers formatted by Intl.NumberFormat, not tied to documents or anything.

This doesn't change any actual behaviour, applying this parsing when pasting/typing in numeric columns will be a separate diff.

Test Plan: New file with extensive unit tests.

Reviewers: dsagal

Reviewed By: dsagal

Subscribers: jarek

Differential Revision: https://phab.getgrist.com/D3078
This commit is contained in:
Alex Hall
2021-10-19 20:59:13 +02:00
parent dd0f1be117
commit 4894631ba4
2 changed files with 207 additions and 2 deletions

View File

@@ -35,8 +35,12 @@ export interface NumberFormatOptions extends FormatOptions {
currency?: string;
}
export function getCurrency(options: NumberFormatOptions, docSettings: DocumentSettings): string {
return options.currency || docSettings.currency || LocaleCurrency.getCurrency(docSettings.locale);
}
export function buildNumberFormat(options: NumberFormatOptions, docSettings: DocumentSettings): Intl.NumberFormat {
const currency = options.currency || docSettings.currency || LocaleCurrency.getCurrency(docSettings.locale);
const currency = getCurrency(options, docSettings);
const nfOptions: Intl.NumberFormatOptions = parseNumMode(options.numMode, currency);
// numSign is implemented outside of Intl.NumberFormat since the latter's similar 'currencySign'
@@ -62,7 +66,7 @@ export function buildNumberFormat(options: NumberFormatOptions, docSettings: Doc
return new Intl.NumberFormat(docSettings.locale, nfOptions);
}
function parseNumMode(numMode?: NumMode, currency?: string): Intl.NumberFormatOptions {
export function parseNumMode(numMode?: NumMode, currency?: string): Intl.NumberFormatOptions {
switch (numMode) {
case 'currency': return {style: 'currency', currency, currencyDisplay: 'narrowSymbol' };
case 'decimal': return {useGrouping: true};