import stringIncludes from '../../../../core/utils/string-includes' ; import { patternRemoveEscapedText } from '../../../utils/pattern-regexp-utils' ; import splitStringInTwoParts from '../../../../core/utils/split-string-in-two-parts' ; const countChars = ( string , char ) => { return ! string ? 0 : string . split ( '' ) . filter ( stringChar => stringChar === char ) . length ; } ; /**
 * Fraction digits (decimals) count rule (minimum and maximum fraction digits)
 * <i> Optional fraction digits would go always after the forced ones
 */ const decimalPlacesRule = ( patternMask ) => { const patternWithoutEscapedText = patternRemoveEscapedText ( patternMask ) ; const patternPrecisionPart = splitStringInTwoParts ( patternWithoutEscapedText , '.' ) [ 1 ] ; let minimumFractionDigits = 0 ; let maximumFractionDigits = 0 ; if ( ! ! patternPrecisionPart ) { const trimmedPatternPrecisionPart = patternPrecisionPart . trim ( ) ; if ( stringIncludes ( trimmedPatternPrecisionPart , '[' ) ) { const patternPrecisionPartWithoutClosingBracket = trimmedPatternPrecisionPart . replace ( ']' , '' ) ; const precisionSplitted = splitStringInTwoParts ( patternPrecisionPartWithoutClosingBracket , '[' ) ; minimumFractionDigits = countChars ( precisionSplitted [ 0 ] , '0' ) ; maximumFractionDigits = minimumFractionDigits + countChars ( precisionSplitted [ 1 ] , '0' ) ; } else if ( stringIncludes ( trimmedPatternPrecisionPart , '#' ) ) { minimumFractionDigits = countChars ( trimmedPatternPrecisionPart . split ( '#' ) [ 0 ] , '0' ) ; maximumFractionDigits = trimmedPatternPrecisionPart . length ; } else if ( stringIncludes ( trimmedPatternPrecisionPart , 'X' ) ) { minimumFractionDigits = countChars ( trimmedPatternPrecisionPart . split ( 'X' ) [ 0 ] , '0' ) ; maximumFractionDigits = 500 ; } else { const fractionDigits = countChars ( trimmedPatternPrecisionPart . split ( ' ' ) [ 0 ] , '0' ) ; minimumFractionDigits = fractionDigits ; maximumFractionDigits = fractionDigits ; } } return { minimumFractionDigits , maximumFractionDigits } as const ; } ; export default decimalPlacesRule ;