import * as domain from 'domain' ; import { BigNumber } from './number' ; export const NULL_ADDRESS = '0x0000000000000000000000000000000000000000' ; param value returns export const stringify = ( value ) : string => { if ( ! ( typeof value === 'string' ) ) { value = JSON . stringify ( value ) ; } return value as string ; } ; param value returns export const jsonify = ( value ) : { } => { return isJson ( value ) ? JSON . parse ( value ) : value ; } ; param value returns export const isJson = ( value ) : boolean => { try { JSON . parse ( value ) ; } catch ( err ) { return false ; } return true ; } ; export interface PrettyPrintable { [ key ] : string | number | BigNumber | boolean | any ; } param obj returns export const prettify = ( obj ) : string => { const parsed = { } ; for ( const key of Object . keys ( obj ) ) { const value = obj [ key ] ; parsed [ key ] = BigNumber . isBigNumber ( value ) ? `${ value . toString ( 16 ) } (${ value . toString ( 10 ) })` : value ; } return JSON . stringify ( parsed , null , 2 ) ; } ; param ms returns export const sleep = ( ms ) : Promise < void > => { return new Promise ( ( resolve ) => { setTimeout ( resolve , ms ) ; } ) ; } ; param str returns export const remove0x = ( str ) : string => { return str . startsWith ( '0x' ) ? str . slice ( 2 ) : str ; } ; param str returns export const add0x = ( str ) : string => { return str . startsWith ( '0x' ) ? str : '0x' + str ; } ; param obj returns export const isObject = ( obj ) : boolean => { return typeof obj === 'object' && obj !== null ; } ; param n returns export const getNullString = ( n ) : string => { return '0x' + '0' . repeat ( n ) ; } ; param str returns export const reverse = ( str ) : string => { return Array . from ( str ) . reverse ( ) . join ( '' ) ; } ; param buf returns export const bufToHexString = ( buf ) : string => { return '0x' + buf . toString ( 'hex' ) ; } ; param bn returns export const bnToHexString = ( bn ) : string => { return '0x' + bn . toString ( 'hex' ) ; } ; param value returns export const hexStringify = ( value ) : string => { if ( value instanceof BigNumber ) { return bnToHexString ( value ) ; } else if ( value instanceof Buffer ) { return bufToHexString ( value ) ; } else { throw new Error ( "Can't hexStringify--invalid type passed" ) ; } } ; param hexString returns export const hexStrToBuf = ( hexString ) : Buffer => { return Buffer . from ( remove0x ( hexString ) , 'hex' ) ; } ; param buff returns export const hexBufToStr = ( buff ) : string => { return add0x ( buff . toString ( 'hex' ) ) ; } ; param list param element returns export const except = < T > ( list , element ) : T [ ] => { return list . filter ( ( item ) => { return item !== element ; } ) ; } ; param d param func returns export const runInDomain = async ( d , func ) : Promise < any > => { const domainToUse = ! ! d ? d : domain . create ( ) ; return domainToUse . run ( func ) ; } ;