Skip to content
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

Allow providing precision range #5

Merged
merged 1 commit into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface NumberInputOptions {
hidePrefixOrSuffixOnFocus?: boolean
hideGroupingSeparatorOnFocus?: boolean
hideNegligibleDecimalDigitsOnFocus?: boolean
precision?: number
precision?: number | NumberRange
autoDecimalDigits?: boolean
autoSign?: boolean
valueRange?: NumberRange
Expand Down
5 changes: 4 additions & 1 deletion src/numberFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export class NumberFormat {

if (this.decimalSymbol === undefined) {
this.minimumFractionDigits = this.maximumFractionDigits = 0
} else if (precision !== undefined) {
} else if (typeof precision === 'number') {
this.minimumFractionDigits = this.maximumFractionDigits = precision
} else if (typeof precision === 'object') {
dm4t2 marked this conversation as resolved.
Show resolved Hide resolved
this.minimumFractionDigits = precision.min || 0
this.maximumFractionDigits = precision.max !== undefined ? precision.max : 15
} else {
const { maximumFractionDigits, minimumFractionDigits } = new Intl.NumberFormat(locale, { currency, unit, style }).resolvedOptions()
this.minimumFractionDigits = minimumFractionDigits
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/numberFormat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ describe('NumberFormat', () => {
expect(new NumberFormat({ formatStyle: NumberFormatStyle.Currency, currency: 'EUR', precision: 0 })).toEqual(
expect.objectContaining({ minimumFractionDigits: 0, maximumFractionDigits: 0 })
)

expect(new NumberFormat({ formatStyle: NumberFormatStyle.Currency, currency: 'EUR', precision: { min: 2, max: 5 } })).toEqual(
expect.objectContaining({ minimumFractionDigits: 2, maximumFractionDigits: 5 })
)
})

it('should ignore the custom precision if the locale does not support decimal digits', () => {
Expand Down Expand Up @@ -524,6 +528,33 @@ describe('NumberFormat', () => {
currency: 'EUR'
}).format(1234.5789, { minimumFractionDigits: 4 })
).toBe('€1,234.5789')

expect(
new NumberFormat({
formatStyle: NumberFormatStyle.Currency,
locale: 'en',
currency: 'EUR',
precision: 3
}).format(1234.5789)
).toBe('€1,234.579')

expect(
new NumberFormat({
formatStyle: NumberFormatStyle.Currency,
locale: 'en',
currency: 'EUR',
precision: { min: 2, max: 5 }
}).format(1234.56789)
).toBe('€1,234.56789')

expect(
new NumberFormat({
formatStyle: NumberFormatStyle.Currency,
locale: 'en',
currency: 'EUR',
precision: { min: 2, max: 5 }
}).format(1234.5)
).toBe('€1,234.50')
})
})
})
Expand Down