@@ -2,6 +2,20 @@ import { escapeRegExp, substringBefore } from './utils'
22import { CurrencyDisplay , NumberFormatStyle , NumberInputOptions , UnitDisplay } from './api'
33import NumberFormatOptions = Intl . NumberFormatOptions
44
5+ const getPrefix = ( parts : Intl . NumberFormatPart [ ] ) =>
6+ parts
7+ . slice ( 0 , parts . map ( ( p ) => p . type ) . indexOf ( 'integer' ) )
8+ . map ( ( p ) => p . value )
9+ . join ( '' )
10+
11+ const getSuffix = ( parts : Intl . NumberFormatPart [ ] ) => {
12+ const types = parts . map ( ( p ) => p . type )
13+ return parts
14+ . slice ( Math . max ( types . lastIndexOf ( 'integer' ) , types . indexOf ( 'fraction' ) ) + 1 )
15+ . map ( ( p ) => p . value )
16+ . join ( '' )
17+ }
18+
519export const DECIMAL_SEPARATORS = [ ',' , '.' , '٫' ]
620export const INTEGER_PATTERN = '(0|[1-9]\\d*)'
721
@@ -20,18 +34,20 @@ export class NumberFormat {
2034 maximumFractionDigits : number
2135 prefix : string
2236 negativePrefix : string
23- suffix : string
37+ suffix : string [ ]
2438
2539 constructor ( options : NumberInputOptions ) {
40+ const createNumberFormat = ( options : Intl . NumberFormatOptions ) =>
41+ new Intl . NumberFormat ( locale , {
42+ currency,
43+ currencyDisplay,
44+ unit,
45+ unitDisplay,
46+ style,
47+ ...options
48+ } )
2649 const { formatStyle : style , currency, currencyDisplay, unit, unitDisplay, locale, precision } = options
27- const numberFormat = new Intl . NumberFormat ( locale , {
28- currency,
29- currencyDisplay,
30- unit,
31- unitDisplay,
32- style,
33- minimumFractionDigits : style !== NumberFormatStyle . Currency ? 1 : undefined
34- } )
50+ const numberFormat = createNumberFormat ( { minimumFractionDigits : style !== NumberFormatStyle . Currency ? 1 : undefined } )
3551 const formatParts = numberFormat . formatToParts ( style === NumberFormatStyle . Percent ? 1234.56 : 123456 )
3652
3753 this . locale = locale
@@ -58,12 +74,9 @@ export class NumberFormat {
5874 this . maximumFractionDigits = maximumFractionDigits
5975 }
6076
61- const getPrefix = ( str : string ) => substringBefore ( str , this . digits [ 1 ] )
62- const getSuffix = ( str : string ) => str . substring ( str . lastIndexOf ( this . decimalSymbol ? this . digits [ 0 ] : this . digits [ 1 ] ) + 1 )
63-
64- this . prefix = getPrefix ( numberFormat . format ( 1 ) )
65- this . suffix = getSuffix ( numberFormat . format ( 1 ) )
66- this . negativePrefix = getPrefix ( numberFormat . format ( - 1 ) )
77+ this . prefix = getPrefix ( numberFormat . formatToParts ( 1 ) )
78+ this . suffix = [ getSuffix ( createNumberFormat ( { minimumFractionDigits : 0 } ) . formatToParts ( 1 ) ) , getSuffix ( numberFormat . formatToParts ( 2 ) ) ]
79+ this . negativePrefix = getPrefix ( numberFormat . formatToParts ( - 1 ) )
6780 }
6881
6982 parse ( str : string | null ) : number | null {
@@ -99,8 +112,22 @@ export class NumberFormat {
99112 integerNumber /= 100
100113 }
101114 return [
102- this . stripPrefixOrSuffix ( this . normalizeDigits ( integerNumber . toLocaleString ( this . locale , { ...options , useGrouping : true } ) ) ) ,
103- this . stripPrefixOrSuffix ( this . normalizeDigits ( integerNumber . toLocaleString ( this . locale , { ...options , useGrouping : false } ) ) )
115+ this . stripPrefixOrSuffix (
116+ this . normalizeDigits (
117+ integerNumber . toLocaleString ( this . locale , {
118+ ...options ,
119+ useGrouping : true
120+ } )
121+ )
122+ ) ,
123+ this . stripPrefixOrSuffix (
124+ this . normalizeDigits (
125+ integerNumber . toLocaleString ( this . locale , {
126+ ...options ,
127+ useGrouping : false
128+ } )
129+ )
130+ )
104131 ] . includes ( formattedNumber )
105132 }
106133
@@ -136,7 +163,7 @@ export class NumberFormat {
136163 }
137164
138165 insertPrefixOrSuffix ( str : string , negative : boolean ) : string {
139- return `${ negative ? this . negativePrefix : this . prefix } ${ str } ${ this . suffix } `
166+ return `${ negative ? this . negativePrefix : this . prefix } ${ str } ${ this . suffix [ 1 ] } `
140167 }
141168
142169 stripGroupingSeparator ( str : string ) : string {
@@ -148,7 +175,7 @@ export class NumberFormat {
148175 }
149176
150177 stripPrefixOrSuffix ( str : string ) : string {
151- return str . replace ( this . negativePrefix , '' ) . replace ( this . prefix , '' ) . replace ( this . suffix , '' )
178+ return str . replace ( this . negativePrefix , '' ) . replace ( this . prefix , '' ) . replace ( this . suffix [ 1 ] , '' ) . replace ( this . suffix [ 0 ] , '' )
152179 }
153180
154181 normalizeDecimalSeparator ( str : string , from : number ) : string {
0 commit comments