import { logger } from "../logger" ; import { ICoin } from "./coins" ; function isMatch ( coin , str ) { return coin . names . indexOf ( str ) > - 1 || coin . algorithm . names . indexOf ( str ) > - 1 || str === coin . id . toString ( ) ; } function disableCoin ( coin , trigger ) { if ( coin . enabled === false ) { logger . warn ( `Couldn't disable coin ${ coin . displayName } because it's already disabled (triggered by '${ trigger }')` ) ; return ; } logger . debug ( `filter(): Disabled coin ${ coin . displayName } because of argument '${ trigger }'` ) ; coin . enabled = false ; } function enableCoin ( coin , trigger ) { if ( coin . enabled === true ) { logger . warn ( `Couldn't enable coin ${ coin . displayName } because it's already enabled (triggered by '${ trigger }')` ) ; return ; } logger . debug ( `filter(): Enabled coin ${ coin . displayName } because of argument '${ trigger }'` ) ; coin . enabled = true ; } export function filter ( coins , targets ) { coins . forEach ( ( coin ) => coin . enabled = null ) ; let hasEnabledCoins = false ; for ( const str of targets ) { let foundMatch = false ; for ( const coin of coins ) { const isDisablingCoin = str . startsWith ( "-" ) ; let name = str ; if ( isDisablingCoin ) { name = name . substr ( 1 ) ; } if ( isMatch ( coin , name ) ) { if ( isDisablingCoin ) { disableCoin ( coin , name ) ; } else { enableCoin ( coin , name ) ; if ( ! hasEnabledCoins ) { coins . filter ( ( c ) => c . enabled === null ) . forEach ( ( c ) => c . enabled = false ) ; } hasEnabledCoins = true ; } foundMatch = true ; } } if ( ! foundMatch ) { logger . warn ( `Couldn't find coins that match argument '${ str }'` ) ; } } coins . forEach ( ( coin ) => coin . enabled = coin . enabled || coin . enabled === null ) ; return coins . filter ( ( coin ) => coin . enabled === true ) ; }