import { camelCase } from 'lodash' ; import { parseUrl } from 'query-string' ; import { TaroTabBarConfig , CallbackResult , OptionsFunc } from './types' ; const globalAny = global ; export function isUrl ( str ) { const protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/ ; const localhostDomainRE = /^localhost[:?\d]*(?:[^:?\d]\S*)?$/ ; const nonLocalhostDomainRE = /^[^\s.]+\.\S{2,}$/ ; if ( typeof str !== 'string' ) { return false ; } const match = str . match ( protocolAndDomainRE ) ; if ( ! match ) { return false ; } const everythingAfterProtocol = match [ 1 ] ; if ( ! everythingAfterProtocol ) { return false ; } if ( localhostDomainRE . test ( everythingAfterProtocol ) || nonLocalhostDomainRE . test ( everythingAfterProtocol ) ) { return true ; } return false ; } export function isFunction ( obj ) { return typeof obj === 'function' ; } export function isEmptyObject ( obj ) { if ( obj == null ) { return true ; } for ( const key in obj ) { if ( obj . hasOwnProperty ( key ) ) { return false ; } } return true ; } export function successHandler ( success , complete ) { return function ( res ) { success && isFunction ( success ) && success ( res ) ; complete && isFunction ( complete ) && complete ( res ) ; return Promise . resolve ( res ) ; } ; } export function errorHandler ( fail , complete ) { return function ( res ) { fail && isFunction ( fail ) && fail ( res ) ; complete && isFunction ( complete ) && complete ( res ) ; return Promise . reject ( res ) ; } ; } export function getTabItemConfig ( index , key ) { const _taroTabBarIconConfig = globalAny . __taroTabBarIconConfig as TaroTabBarConfig ; const _taroTabItems = _taroTabBarIconConfig . tabItems ; return _taroTabItems [ index ] && _taroTabItems [ index ] [ key ] ; } export function getTabConfig ( key ) { const _taroTabBarIconConfig = globalAny . __taroTabBarIconConfig as TaroTabBarConfig ; return _taroTabBarIconConfig [ key ] ; } export function setTabConfig ( key , value ) { const tabBarConfig = globalAny . __taroTabBarIconConfig ; tabBarConfig [ key ] = value ; globalAny . __taroTabBarIconConfig = tabBarConfig ; } export function setTabInitRoute ( routeName ) { globalAny . __taroTabInitRoute = routeName ; } export function getTabInitRoute ( ) { return globalAny . __taroTabInitRoute || '' ; } export function getTabVisible ( ) { return getTabConfig ( 'tabBarVisible' ) ; } export function getDefalutTabItem ( index ) { const _taroAppConfig = globalAny . __taroAppConfig || { } ; const tabBar = _taroAppConfig ?. appConfig ?. tabBar || [ ] ; return tabBar ?. list [ index ] || { } ; } export function getTabBarPages ( ) { const tabBar = globalAny . __taroAppConfig ?. appConfig ?. tabBar || { } ; if ( isEmptyObject ( tabBar ) ) return [ ] ; const pages = [ ] ; tabBar ?. list . forEach ( ( item ) => { const path = item . pagePath . startsWith ( '/' ) ? item . pagePath : `/${ item . pagePath }` ; pages . push ( camelCase ( path ) ) ; } ) ; return pages ; } export function handleUrl ( url ) { const path = url . split ( '?' ) [ 0 ] ; const pageName = camelCase ( path . startsWith ( '/' ) ? path : `/${ path }` ) ; const params = parseUrl ( url . startsWith ( '/' ) ? url . substr ( 1 ) : url ) . query || { } ; return { pageName , params } ; }