1+ import { PERCENT , SCIENTIFIC , NUMBER_PLACEHOLDER , CURRENCY_PLACEHOLDER , PERCENT_PLACEHOLDER , EMPTY , POINT } from '../common/constants' ;
12import formatCurrencySymbol from './format-currency-symbol' ;
23import groupInteger from './group-integer' ;
4+ import isCurrencyStyle from './is-currency-style' ;
35import pad from '../common/pad' ;
46import round from '../common/round' ;
57import { currencyFractionOptions } from '../cldr' ;
@@ -8,15 +10,10 @@ const DEFAULT_DECIMAL_ROUNDING = 3;
810const DEFAULT_PERCENT_ROUNDING = 0 ;
911
1012const trailingZeroRegex = / 0 + $ / ;
11- const DECIMAL_PLACEHOLDER = "n" ;
12- const CURRENCY = "currency" ;
13- const PERCENT = "percent" ;
14- const EMPTY = "" ;
15- const POINT = "." ;
1613
1714function fractionOptions ( options ) {
1815 let { minimumFractionDigits, maximumFractionDigits, style } = options ;
19- const isCurrency = style === CURRENCY ;
16+ const isCurrency = isCurrencyStyle ( style ) ;
2017 let currencyFractions ;
2118 if ( isCurrency ) {
2219 currencyFractions = currencyFractionOptions ( options . currency ) ;
@@ -47,9 +44,9 @@ function applyPattern(value, pattern, symbol) {
4744 for ( let idx = 0 , length = pattern . length ; idx < length ; idx ++ ) {
4845 let ch = pattern . charAt ( idx ) ;
4946
50- if ( ch === DECIMAL_PLACEHOLDER ) {
47+ if ( ch === NUMBER_PLACEHOLDER ) {
5148 result += value ;
52- } else if ( ch === "$" || ch === "%" ) {
49+ } else if ( ch === CURRENCY_PLACEHOLDER || ch === PERCENT_PLACEHOLDER ) {
5350 result += symbol ;
5451 } else {
5552 result += ch ;
@@ -62,7 +59,7 @@ function currencyUnitPattern(info, value) {
6259 const currencyInfo = info . numbers . currency ;
6360 let pattern = value !== 1 ? currencyInfo [ "unitPattern-count-other" ] : currencyInfo [ "unitPattern-count-one" ] ;
6461 if ( value < 0 ) {
65- pattern = pattern . replace ( "n" , "-n" ) ;
62+ pattern = pattern . replace ( NUMBER_PLACEHOLDER , `- ${ NUMBER_PLACEHOLDER } ` ) ;
6663 }
6764
6865 return pattern ;
@@ -72,17 +69,18 @@ function currencyUnitPattern(info, value) {
7269export default function standardNumberFormat ( number , options , info ) {
7370 const symbols = info . numbers . symbols ;
7471 const { style } = options ;
72+ const isCurrency = isCurrencyStyle ( style ) ;
7573
7674 //return number in exponential format
77- if ( style === "scientific" ) {
75+ if ( style === SCIENTIFIC ) {
7876 let exponential = options . minimumFractionDigits !== undefined ? number . toExponential ( options . minimumFractionDigits ) : number . toExponential ( ) ;
7977 return exponential . replace ( POINT , symbols . decimal ) ;
8078 }
8179
8280 let value = number ;
8381 let symbol ;
8482
85- if ( style === CURRENCY ) {
83+ if ( isCurrency ) {
8684 options . value = value ;
8785 symbol = formatCurrencySymbol ( info , options ) ;
8886 }
@@ -98,7 +96,7 @@ export default function standardNumberFormat(number, options, info) {
9896
9997 const negative = value < 0 ;
10098
101- const parts = value . split ( "." ) ;
99+ const parts = value . split ( POINT ) ;
102100
103101 let integer = parts [ 0 ] ;
104102 let fraction = pad ( parts [ 1 ] ? parts [ 1 ] . replace ( trailingZeroRegex , EMPTY ) : EMPTY , minimumFractionDigits , true ) ;
@@ -120,14 +118,14 @@ export default function standardNumberFormat(number, options, info) {
120118
121119 let pattern ;
122120
123- if ( style === CURRENCY && options . currencyDisplay === "name" ) {
121+ if ( isCurrency && options . currencyDisplay === "name" ) {
124122 pattern = currencyUnitPattern ( info , number ) ;
125123 } else {
126124 const patterns = options . patterns ;
127125 pattern = negative ? patterns [ 1 ] || ( "-" + patterns [ 0 ] ) : patterns [ 0 ] ;
128126 }
129127
130- if ( pattern === DECIMAL_PLACEHOLDER && ! negative ) {
128+ if ( pattern === NUMBER_PLACEHOLDER && ! negative ) {
131129 return formattedValue ;
132130 }
133131
0 commit comments