import isNil from '../../core/utils/is-nil' ; import isFunction from '../../core/utils/is-function' ; import isNanNumber from '../../core/utils/is-nan-number' ; import resolveFormatOptions from '../utils/resolve-format-options' ; import formattedStringToNumber from './utils/formatted-string-to-number' ; import { NumerableFormatNumberOptions } from '../types/format-number-options' ; import { ResolvedNumerableFormatNumberOptions } from '../types/resolved-format-number-options' ; const getUnformatFunctionIfMatch = ( input , resolvedOptions ) => { for ( const formatter of resolvedOptions . formatters ) { const matcher = formatter . regexps . unformat ; if ( ! matcher ) continue ; const matcherResult = isFunction ( matcher ) ? matcher ( input , resolvedOptions ) : ! ! input . match ( matcher ) ; if ( matcherResult ) return formatter . unformat ; } } ; const parse = ( input , options ? ) : number | null => { const resolvedOptions = resolveFormatOptions ( options ) ; let value ; if ( isNil ( input ) || isNanNumber ( input ) ) { value = null ; } else if ( typeof input === 'number' ) { value = input === 0 ? 0 : input ; } else if ( typeof input === 'string' ) { if ( resolvedOptions . zeroFormat && input === resolvedOptions . zeroFormat ) { value = 0 ; } else if ( resolvedOptions . nullFormat && input === resolvedOptions . nullFormat ) { value = null ; } else { const inputStringWithNormalSpaces = input . replace ( /\u00A0/ , ' ' ) ; const unformatFunctionFromFormatters = getUnformatFunctionIfMatch ( inputStringWithNormalSpaces , resolvedOptions ) ; const unformatFunction = unformatFunctionFromFormatters || formattedStringToNumber ; value = unformatFunction ( inputStringWithNormalSpaces , resolvedOptions ) ; } } else { const result = + input ; value = result === 0 ? result : ( result || null ) ; } return value ; } ; export default parse ;