import { bytes , ethereumAddress } from "./regEx" ; import { parseBytes32String } from "ethers/lib/utils" ; export const participantId = ( address ) : string => { if ( ! address ) return "" ; const participantId = address . substr ( 2 , 4 ) + address . substr ( - 4 , 4 ) ; return participantId . toLocaleLowerCase ( ) ; } ; export const shortAddress = ( address ) : string => { if ( typeof address !== "string" ) return "" ; if ( ! address . match ( ethereumAddress ) ) return address ; return address . substr ( 0 , 6 ) + ".." + address . substr ( - 4 , 4 ) ; } ; export const shortBytes = ( bytes , wrapLength = 66 ) : string => { if ( ! bytes ) return "" ; if ( typeof bytes !== "string" ) return bytes ; const noZeroPadding = bytes . replace ( /0{5,}/g , "0..0" ) ; const splitData = noZeroPadding . match ( new RegExp ( ".{1," + wrapLength + "}" , "g" ) ) ; const wrappedString = splitData . reduce ( ( accumulator , currentValue ) => { return accumulator + "\\n" + currentValue ; } ) ; return wrappedString ; } ; export const formatNumber = ( value ) : string => { if ( ! value ) return "" ; if ( ! value . toString ( ) . match ( /^\d+$/ ) ) return value ; const thousandsCommas = value . toString ( ) . replace ( /\B(?=(\d{3})+(?!\d))/g , "," ) ; if ( thousandsCommas . length > 24 ) { const doubleCommonPosition = thousandsCommas . length - 24 ; return ( thousandsCommas . substr ( 0 , doubleCommonPosition + 1 ) + thousandsCommas . substr ( doubleCommonPosition ) ) ; } return thousandsCommas ; } ; export const convertBytes32ToString = ( output ) => { if ( ! output || typeof output !== "string" ) return undefined ; return output . match ( bytes ) ? parseBytes32String ( output ) : output ; } ;