/**
 * utils.ts - Various utility functions
 */ import Blockweave from 'blockweave' ; import fs from 'fs' ; import path from 'path' ; import minimist from 'minimist' ; export function setArweaveInstance ( argv , debug ) { const timeout = argv . timeout || 20000 ; const gateway = argv . gateway || 'https://arweave.net' ; return new Blockweave ( { url : gateway , timeout , logging : debug , } , [ gateway ] ) ; } export function isValidWalletAddress ( address ) { return /[a-z0-9_-]{43}/i . test ( address ) ; } export function bytesForHumans ( bytes ) { const sizes = [ 'Bytes' , 'kB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' ] ; let output ; sizes . forEach ( ( unit , id ) => { const s = Math . pow ( 1024 , id ) ; let fixed = '' ; if ( bytes >= s ) { fixed = String ( ( bytes / s ) . toFixed ( 2 ) ) ; if ( fixed . indexOf ( '.0' ) === fixed . length - 2 ) { fixed = fixed . slice ( 0 , - 2 ) ; } output = `${ fixed } ${ unit }` ; } } ) ; if ( ! output ) { return `0 Bytes` ; } return output ; } export function numbersForHumans ( x ) { return x . toString ( ) . replace ( /\B(?=(\d{3})+(?!\d))/g , ',' ) ; } export function dirExists ( dir ) { return fs . existsSync ( dir ) ; } export function getUserDirectory ( ) { return process . cwd ( ) ; } export function getPackageVersion ( ) { return require ( path . join ( __dirname , '..' , '..' , 'package.json' ) ) . version ; } export async function pause ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } export function snakeCaseToTitleCase ( snake_case ) { const sentence = snake_case . toLowerCase ( ) . split ( '_' ) ; for ( let i = 0 ; i < sentence . length ; i ++ ) { sentence [ i ] = sentence [ i ] [ 0 ] . toUpperCase ( ) + sentence [ i ] . slice ( 1 ) ; } return sentence . join ( ' ' ) ; }