import parsePattern from '../parse-pattern/parse-pattern' ; import { NumberFormatRules } from '../../../core/types/rules' ; import removeSignIfExists from './utils/remove-sign-if-exists' ; import rescaleRoundedValue from './utils/rescale-rounded-value' ; import isFiniteNumber from '../../../core/utils/is-finite-number' ; import scaleValueWithAbbreviation from './utils/scale-value-with-abbreviation' ; import roundValueAndAddTrailingZeros from './utils/round-value-and-add-trailing-zeros' ; import { patternStripAndNormalizeEscapedText } from '../../utils/pattern-regexp-utils' ; import replaceDigitsWithNumeralSystem from './utils/replace-digits-with-numeral-system' ; import addOrRemoveLeadingZerosToValue from './utils/add-or-remove-leading-zeros-to-value' ; import addSignInfoToFullFormattedNumber from './utils/add-sign-info-to-full-formatted-number' ; import splitNumberIntegerAndDecimalParts from './utils/split-number-integer-and-decimal-parts' ; import addThousandsSeparatorToValueIntegerPart from './utils/add-thousands-separator-to-value-integer-part' ; import { ResolvedNumerableFormatNumberOptions } from '../../../formatter/types/resolved-format-number-options' ; import applyAbbreviationLocalizedUnitToPatternMask from './utils/apply-abbreviation-localized-unit-to-pattern-mask' ; const scaleAndRoundValue = ( number , patternRules , options ) : [ string , string | null ] => { if ( ! patternRules . compact ) { const roundedValueAsString = roundValueAndAddTrailingZeros ( number , patternRules , options ) ; return [ roundedValueAsString , null ] ; } const [ scaledValue , scaledValueLocalizedUnit ] = scaleValueWithAbbreviation ( number , patternRules , options ) ; const roundedScaledValue = + roundValueAndAddTrailingZeros ( scaledValue , patternRules , options ) ; const [ rescaledValue , rescaledValueLocalizedUnit ] = rescaleRoundedValue ( + roundedScaledValue , scaledValueLocalizedUnit , patternRules , options ) ; const roundedRescaledValueAsStringWithTrailingZeros = roundValueAndAddTrailingZeros ( rescaledValue , patternRules , options ) ; return [ roundedRescaledValueAsStringWithTrailingZeros , rescaledValueLocalizedUnit ] ; } ; const numberToFormattedNumber = ( number , pattern , options ) : string => { const patternRules = parsePattern ( pattern ) ; const resolvedValue = isFiniteNumber ( number ) ? number : 0 ; const [ valueAsString , localizedAbbreviationUnit ] = scaleAndRoundValue ( resolvedValue , patternRules , options ) ; if ( valueAsString === 'NaN' ) return '' ; const isValueNegative = options . signedZero ? number < 0 : + valueAsString < 0 ; const isValueZero = options . signedZero ? number === 0 : + valueAsString === 0 ; const valueAsStringWithoutSign = removeSignIfExists ( valueAsString ) ; const [ integerPart , decimalPart ] = splitNumberIntegerAndDecimalParts ( valueAsStringWithoutSign , patternRules ) ; const valueIntegerPartWithLeadingZeros = addOrRemoveLeadingZerosToValue ( integerPart , patternRules ) ; const valueIntegerPartWithThousandsSeparator = addThousandsSeparatorToValueIntegerPart ( valueIntegerPartWithLeadingZeros , patternRules , options ) ; const numeralSystemFromLocale = options . locale . numeralSystem ; const integerPartWithNumeralSystem = replaceDigitsWithNumeralSystem ( valueIntegerPartWithThousandsSeparator , numeralSystemFromLocale ) ; const decimalPartWithNumeralSystem = replaceDigitsWithNumeralSystem ( decimalPart , numeralSystemFromLocale ) ; const fullNumberWithNumeralSystem = ( integerPartWithNumeralSystem + ( ! ! decimalPartWithNumeralSystem ? ( options . locale . delimiters ?. decimal || '.' ) + decimalPartWithNumeralSystem : '' ) ) ; const patternMaskWithAbbreviation = applyAbbreviationLocalizedUnitToPatternMask ( patternRules . patternMask , localizedAbbreviationUnit , patternRules . compact ) ; const patternMaskWithNumber = patternMaskWithAbbreviation . replace ( `'ɵn'` , _ => `'${ fullNumberWithNumeralSystem . replace ( /'/g , "\\'" ) }'` ) ; const patternMaskWithSignInfo = addSignInfoToFullFormattedNumber ( patternMaskWithNumber , isValueNegative , isValueZero , patternRules ) ; const cleanPatternMask = patternMaskWithSignInfo . replace ( /'ɵ(nps|npe|s|a|n)'/g , '' ) ; const fullFormattedValueWithNormalizedText = patternStripAndNormalizeEscapedText ( cleanPatternMask ) ; return fullFormattedValueWithNormalizedText ; } ; export default numberToFormattedNumber ;