import * as path from 'path' ; import { Keccak } from 'sha3' ; module const cwd = path . resolve ( process . cwd ( ) ) ; see https example method param { String } address return { Boolean } function isAddress ( address ) { if ( ! /^(0x)?[0-9a-f]{40}$/i . test ( address ) ) { return false ; } if ( /^(0x)?[0-9a-f]{40}$/ . test ( address ) || /^(0x)?[0-9A-F]{40}$/ . test ( address ) ) { return true ; } return isChecksumAddress ( address ) ; } function isHash ( hash ) { return /([A-Fa-f0-9]{64})$/ . test ( hash ) ; } example method param { String } address return { Boolean } function isChecksumAddress ( _address ) { const address = _address . replace ( '0x' , '' ) ; const addressHash = new Keccak ( 256 ) ; addressHash . update ( address . toLowerCase ( ) ) ; addressHash . digest ( 'hex' ) ; for ( let i = 0 ; i < 40 ; i ++ ) { if ( ( parseInt ( addressHash [ i ] , 16 ) > 7 && address [ i ] . toUpperCase ( ) !== address [ i ] ) || ( parseInt ( addressHash [ i ] , 16 ) <= 7 && address [ i ] . toLowerCase ( ) !== address [ i ] ) ) { return false ; } } return true ; } example method return { Int } function iuid ( ) { return this . uid ( undefined , 12 ) ; } example method param prefix param digits return { Int } function uid ( prefix ? , digits = 6 ) { if ( digits < 1 ) digits = 1 ; if ( digits > 16 ) digits = 16 ; const random = Math . floor ( Math . random ( ) * ( 10 ** digits ) ) ; return ( prefix === undefined ) ? `${ random }` : `${ prefix }_${ random }` ; } example method param { * } Object return { Object } function usc ( args ) { return Object . keys ( args ) . reduce ( ( acc , key ) => { acc [ `_${ key }` ] = args [ key ] ; return acc ; } , { } ) ; } example method param { String } argName param { Int } defaultValue return { Int } function getArgInt ( argName , defaultValue ) { const index = process . argv . indexOf ( argName ) ; if ( index <= 0 ) { if ( defaultValue !== undefined ) return defaultValue ; throw new Error ( `Argument not found ${ argName }` ) ; } const stringValue = process . argv [ index + 1 ] ; const intValue = parseInt ( stringValue ) ; if ( isNaN ( intValue ) ) throw new Error ( `Invalid int value for ${ argName } : ${ stringValue }` ) ; return intValue ; } function sleep ( ms ) { return new Promise ( resolve => setTimeout ( resolve , ms ) ) ; } example method param { * } predicate param { * } action param { * } options param { * } timeout return { Object } async function until ( predicate , action , options , timeout = 60000 ) { const phi = 10 ; let dt = 500 ; let totalSleep = 0 ; while ( totalSleep < timeout ) { const result = await action ( options ) ; if ( predicate ( result ) ) { return result ; } await sleep ( dt ) ; totalSleep += dt ; dt += phi ; } throw new Error ( `until: timeout ${ timeout } ms exceeded` ) ; } function timeout ( ms ) { return new Promise ( resolve => setTimeout ( resolve , ms ) ) ; } example method param { Array } setA param { Array } setB param { * } comparator param { * } isDebug return { Array } function filter_isContained ( setA , setB , comparator , isDebug ) { if ( isDebug ) { console . log ( 'setA' , setA ) ; console . log ( 'setB' , setB ) ; } return setA . filter ( function ( memberA ) { return ! ( setB . filter ( function ( memberB ) { return comparator ( memberA , memberB ) ; } ) . length > 0 ) ; } ) ; } example method param { * } array return { String } function toCsv ( array ) { return array . reduce ( function ( prevVal , elem ) { return prevVal + ( prevVal . length > 0 ? ',' : '' ) + elem ; } , '' ) ; } export default { cwd , filter_isContained , getArgInt , isAddress , isHash , iuid , uid , usc , sleep , until , timeout , toCsv } ;