Skip to content

Commit

Permalink
feat: add new options currencyDisplay and unitDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
dm4t2 committed Oct 30, 2021
1 parent 73904c2 commit ce38d4e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/api.ts
Expand Up @@ -23,11 +23,26 @@ export enum NumberFormatStyle {
Percent = 'percent'
}

export enum CurrencyDisplay {
Symbol = 'symbol',
NarrowSymbol = 'narrowSymbol',
Code = 'code',
Name = 'name'
}

export enum UnitDisplay {
Short = 'short',
Narrow = 'narrow',
Long = 'long'
}

export interface NumberInputOptions {
locale?: string
formatStyle: NumberFormatStyle
currency?: string
currencyDisplay?: CurrencyDisplay
unit?: string
unitDisplay?: UnitDisplay
exportValueAsInteger?: boolean
hidePrefixOrSuffixOnFocus?: boolean
hideGroupingSeparatorOnFocus?: boolean
Expand Down
28 changes: 24 additions & 4 deletions src/numberFormat.ts
@@ -1,5 +1,5 @@
import { count, escapeRegExp, substringBefore } from './utils'
import { NumberFormatStyle, NumberInputOptions } from './api'
import { CurrencyDisplay, NumberFormatStyle, NumberInputOptions, UnitDisplay } from './api'
import NumberFormatOptions = Intl.NumberFormatOptions

export const DECIMAL_SEPARATORS = [',', '.', '٫']
Expand All @@ -9,7 +9,9 @@ export class NumberFormat {
locale?: string
style: NumberFormatStyle
currency?: string
currencyDisplay?: CurrencyDisplay
unit?: string
unitDisplay?: UnitDisplay
digits: string[]
decimalSymbol: string | undefined
groupingSymbol: string
Expand All @@ -21,14 +23,23 @@ export class NumberFormat {
suffix: string

constructor(options: NumberInputOptions) {
const { formatStyle: style, currency, unit, locale, precision } = options
const numberFormat = new Intl.NumberFormat(locale, { currency, unit, style, minimumFractionDigits: style !== NumberFormatStyle.Currency ? 1 : undefined })
const { formatStyle: style, currency, currencyDisplay, unit, unitDisplay, locale, precision } = options
const numberFormat = new Intl.NumberFormat(locale, {
currency,
currencyDisplay,
unit,
unitDisplay,
style,
minimumFractionDigits: style !== NumberFormatStyle.Currency ? 1 : undefined
})
const ps = numberFormat.format(style === NumberFormatStyle.Percent ? 1234.56 : 123456)

this.locale = locale
this.style = style
this.currency = currency
this.currencyDisplay = currencyDisplay
this.unit = unit
this.unitDisplay = unitDisplay
this.digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((i) => i.toLocaleString(locale))
this.decimalSymbol = count(ps, this.digits[0]) ? ps.substr(ps.indexOf(this.digits[6]) + 1, 1) : undefined
this.groupingSymbol = ps.substr(ps.indexOf(this.digits[3]) + 1, 1)
Expand Down Expand Up @@ -69,7 +80,14 @@ export class NumberFormat {
}

isValidIntegerFormat(formattedNumber: string, integerNumber: number): boolean {
const options = { style: this.style, currency: this.currency, unit: this.unit, minimumFractionDigits: 0 }
const options = {
style: this.style,
currency: this.currency,
currencyDisplay: this.currencyDisplay,
unit: this.unit,
unitDisplay: this.unitDisplay,
minimumFractionDigits: 0
}
if (this.style === NumberFormatStyle.Percent) {
integerNumber /= 100
}
Expand All @@ -90,7 +108,9 @@ export class NumberFormat {
? new Intl.NumberFormat(this.locale, {
style: this.style,
currency: this.currency,
currencyDisplay: this.currencyDisplay,
unit: this.unit,
unitDisplay: this.unitDisplay,
...options
}).format(this.style === NumberFormatStyle.Percent ? value / 100 : value)
: ''
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/__snapshots__/numberFormat.spec.ts.snap
Expand Up @@ -3,6 +3,7 @@
exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: ar-SA_SAR 1`] = `
NumberFormat {
"currency": "SAR",
"currencyDisplay": undefined,
"decimalSymbol": "٫",
"digits": Array [
"٠",
Expand All @@ -26,12 +27,14 @@ NumberFormat {
"style": "currency",
"suffix": " ر.س.‏",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: de-CH_EUR 1`] = `
NumberFormat {
"currency": "EUR",
"currencyDisplay": undefined,
"decimalSymbol": ".",
"digits": Array [
"0",
Expand All @@ -55,12 +58,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: de-DE_EUR 1`] = `
NumberFormat {
"currency": "EUR",
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -84,12 +89,14 @@ NumberFormat {
"style": "currency",
"suffix": " €",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: en-GB_GBP 1`] = `
NumberFormat {
"currency": "GBP",
"currencyDisplay": undefined,
"decimalSymbol": ".",
"digits": Array [
"0",
Expand All @@ -113,12 +120,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: en-IN_INR 1`] = `
NumberFormat {
"currency": "INR",
"currencyDisplay": undefined,
"decimalSymbol": ".",
"digits": Array [
"0",
Expand All @@ -142,12 +151,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: en-US_USD 1`] = `
NumberFormat {
"currency": "USD",
"currencyDisplay": undefined,
"decimalSymbol": ".",
"digits": Array [
"0",
Expand All @@ -171,12 +182,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: es-ES_EUR 1`] = `
NumberFormat {
"currency": "EUR",
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -200,12 +213,14 @@ NumberFormat {
"style": "currency",
"suffix": " €",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: es-ES_EUR 2`] = `
NumberFormat {
"currency": "EUR",
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -229,12 +244,14 @@ NumberFormat {
"style": "currency",
"suffix": " €",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: fa-IR_IRR 1`] = `
NumberFormat {
"currency": "IRR",
"currencyDisplay": undefined,
"decimalSymbol": undefined,
"digits": Array [
"۰",
Expand All @@ -258,12 +275,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: fr-CH_CHF 1`] = `
NumberFormat {
"currency": "CHF",
"currencyDisplay": undefined,
"decimalSymbol": ".",
"digits": Array [
"0",
Expand All @@ -287,12 +306,14 @@ NumberFormat {
"style": "currency",
"suffix": " CHF",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: ja_JPY 1`] = `
NumberFormat {
"currency": "JPY",
"currencyDisplay": undefined,
"decimalSymbol": undefined,
"digits": Array [
"0",
Expand All @@ -316,12 +337,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: nl-NL_EUR 1`] = `
NumberFormat {
"currency": "EUR",
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -345,12 +368,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: pt_BRL 1`] = `
NumberFormat {
"currency": "BRL",
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -374,12 +399,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats currency should work for the respective locale and currency: zh_CNY 1`] = `
NumberFormat {
"currency": "CNY",
"currencyDisplay": undefined,
"decimalSymbol": ".",
"digits": Array [
"0",
Expand All @@ -403,12 +430,14 @@ NumberFormat {
"style": "currency",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats decimal de-DE 1`] = `
NumberFormat {
"currency": undefined,
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -432,12 +461,14 @@ NumberFormat {
"style": "decimal",
"suffix": "",
"unit": undefined,
"unitDisplay": undefined,
}
`;

exports[`NumberFormat constructing number formats unit de-DE 1`] = `
NumberFormat {
"currency": undefined,
"currencyDisplay": undefined,
"decimalSymbol": ",",
"digits": Array [
"0",
Expand All @@ -461,5 +492,6 @@ NumberFormat {
"style": "unit",
"suffix": " Bytes",
"unit": "byte",
"unitDisplay": undefined,
}
`;

0 comments on commit ce38d4e

Please sign in to comment.