import log10 from '../../../core/utils/log-10' ; import toObject from '../../../core/utils/to-object' ; import escapeRegexString from '../../../core/utils/escape-regex-string' ; import multiplyByPowerOfTen from '../../../core/utils/multiply-by-power-of-ten' ; import { ResolvedNumerableFormatNumberOptions } from '../../types/resolved-format-number-options' ; import createUnitScaleFromLocaleAbbreviations from '../../utils/create-unit-scale-from-locale-abbreviations' ; const replaceNumeralSystemWithLatinNumbers = ( numericStringWithExtraInfo , numeralSystemMap ) => { if ( ! numeralSystemMap || numeralSystemMap . length !== 10 ) return numericStringWithExtraInfo ; const numericStringLength = numericStringWithExtraInfo . length ; const numeralSystemToLatinSystemMap = toObject ( numeralSystemMap , ( digit , digitIndex ) => [ digit . replace ( /\u200e/g , '' ) , '' + digitIndex ] ) ; let output = '' ; for ( let numericStringIndex = 0 ; numericStringIndex < numericStringLength ; numericStringIndex ++ ) { const char = numericStringWithExtraInfo [ numericStringIndex ] ; output += numeralSystemToLatinSystemMap [ char ] || char ; } return output ; } ; const getScalingFactorFromAbbreviations = ( stringOriginal , options ) => { const scale = createUnitScaleFromLocaleAbbreviations ( options . locale ?. abbreviations ) ; const abbreviationsSortedByLengthDesc = Object . keys ( scale . scaleDefinition . scale ) . sort ( ( a , b ) => b . length - a . length ) ; let abbreviationScalingFactor = 1 ; for ( const abbreviation of abbreviationsSortedByLengthDesc ) { const scapedAbbreviationForRegex = escapeRegexString ( abbreviation ) ; const regexp = new RegExp ( '[^a-zA-Z]' + `(${ scapedAbbreviationForRegex })|(${ scapedAbbreviationForRegex . replace ( /\u200e/g , '' ) })` + '(?:\\)(?:\\))?)?$' ) ; if ( stringOriginal . match ( regexp ) ) { abbreviationScalingFactor = scale . toBase ( abbreviationScalingFactor , abbreviation ) ; break ; } } return abbreviationScalingFactor ; } ; const formattedStringToNumber = ( inputString , options ) => { const locale = options . locale ; const stringOriginal = inputString ; let value ; const stringWithLatinDigits = replaceNumeralSystemWithLatinNumbers ( inputString , options . locale ?. numeralSystem ) ; if ( options . zeroFormat && stringWithLatinDigits === options . zeroFormat ) { value = 0 ; } else if ( options . nullFormat && stringWithLatinDigits === options . nullFormat || ! stringWithLatinDigits . replace ( /[^0-9]+/g , '' ) . length ) { value = null ; } else { const decimalDelimiterFromLocale = locale . delimiters . decimal ; const stringWithDotDecimalDelimiter = decimalDelimiterFromLocale === '.' ? stringWithLatinDigits : stringWithLatinDigits . replace ( /\./g , '' ) . replace ( decimalDelimiterFromLocale , '.' ) ; const abbreviationScalingFactor = getScalingFactorFromAbbreviations ( stringOriginal , options ) ; const negativeFactor = ( stringWithDotDecimalDelimiter . split ( '-' ) . length + Math . min ( stringWithDotDecimalDelimiter . split ( '(' ) . length - 1 , stringWithDotDecimalDelimiter . split ( ')' ) . length - 1 ) ) % 2 ? 1 : - 1 ; const numberAsString = stringWithDotDecimalDelimiter . replace ( /[^0-9.]+/g , '' ) ; value = negativeFactor * multiplyByPowerOfTen ( + numberAsString , log10 ( abbreviationScalingFactor ) ) ; } return value ; } ; export default formattedStringToNumber ;