import { Injectable , Pipe } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import 'rxjs/add/operator/map' ; import { Observable } from 'rxjs' ; import { of } from 'rxjs/observable/of' ; import { catchError , map , tap } from 'rxjs/operators' ; @ Injectable ( ) export class DataService { private result : any ; private symbolnameData : any = { 'Bitcoin' : 'BTC' , 'Ethereum' : 'ETH' , 'Ripple' : 'XRP' , 'Bitcoin Cash' : 'BCH' , 'Litecoin' : 'LTC' , 'Cardano' : 'ADA' , 'NEO' : 'NEO' , 'Stellar' : 'XLM' , 'Monero' : 'XMR' , 'EOS' : 'EOS' , 'IOTA' : 'IOT' , 'Dash' : 'DASH' , 'NEM' : 'XEM' , 'TRON' : 'TRX' , 'Eth Classic' : 'ETC' , 'Tether' : 'USDT' , 'VeChain' : 'VEN' , 'Qtum' : 'QTUM' , 'Nano' : 'XRB' , 'Lisk' : 'LSK' , 'Bitcoin Gold' : 'BTG' , 'OmiseGo' : 'OMG' , 'ICON' : 'ICX' , 'Zcash' : 'ZEC' , 'Digix DAO' : 'DGD' , 'Binance Coin' : 'BNB' , 'Steem' : 'STEEM' , 'Verge' : 'XVG' , 'Stratis' : 'STRAT' , 'Populous' : 'PPT' , 'ByteCoin' : 'BCN' , 'Waves' : 'WAVES' , 'Siacoin' : 'SC' , 'Status' : 'SNT' , 'RChain' : 'RHOC' , 'Maker' : 'MKR' , 'DogeCoin' : 'DOGE' , 'Bitshares' : 'BTS' , 'Decred' : 'DCR' , 'Aeternity' : 'AE' , 'Waltonchain' : 'WTC' , 'Augur' : 'REP' , 'Electroneum' : 'ETN' , '0x' : 'ZRX' , 'Komodo' : 'KMD' , 'Bytom' : 'BTM' , 'ARK' : 'ARK' , 'Veritaseum' : 'VERI' , 'Ardor' : 'ARDR' , 'Golem' : 'GNT' , 'Dragonchain' : 'DRGN' , 'Hshare' : 'HSR' , 'BAT' : 'BAT' , 'Cryptonex' : 'CNX' , 'SysCoin' : 'SYS' , 'Zilliqa' : 'ZIL' , 'KuCoin' : 'KCS' , 'DigiByte' : 'DGB' , 'Ethos' : 'BQX' , 'Gas' : 'GAS' } ; private defaultDataCopy : any = { ... this . symbolnameData } ; private priceMultiurl : string ; private imageurlPrefix : string = "./assets/crypto-icons/" ; private imageurlSuffix : string [ ] ; private images : any [ ] ; private timer = Observable . timer ( 0 , 15000 ) ; public _previousIndex : number = null ; public _previousData : any = null ; public _previousPageSize : number = null ; public _previousSortMapName : string = null ; public _previousSortMapSymbol : string = null ; private _filterd : boolean = false ; constructor ( private _http ) { } filter ( searchText ) { this . symbolnameData = this . defaultDataCopy ; if ( searchText !== "" ) { let data = ( JSON . stringify ( this . symbolnameData ) ) . replace ( /{|}/g , '' ) . split ( ',' ) . filter ( ( el ) => el . toLowerCase ( ) . indexOf ( searchText . toLowerCase ( ) ) > - 1 ) ; if ( data . length === 0 ) { this . _filterd = false ; return false ; } let arr = [ ] ; data . forEach ( function ( el ) { let temp = el . replace ( /"/g , '' ) . split ( ':' ) ; arr . push ( temp ) ; } ) ; this . symbolnameData = arr . reduce ( ( obj , [ key , value ] ) => { obj [ key ] = value ; return obj ; } , { } ) ; this . _filterd = true ; return true ; } else { this . _filterd = false ; this . symbolnameData = this . defaultDataCopy ; return true ; } } sortData ( sortName , sortOrder ) { switch ( sortName ) { case "name" : { if ( sortOrder === "ascend" ) { this . symbolnameData = Object . keys ( this . symbolnameData ) . sort ( ) . reduce ( ( r , k ) => ( r [ k ] = this . symbolnameData [ k ] , r ) , { } ) ; } else if ( sortOrder === "descend" ) { this . symbolnameData = Object . keys ( this . symbolnameData ) . sort ( ( a , b ) => b . localeCompare ( a ) ) . reduce ( ( r , k ) => ( r [ k ] = this . symbolnameData [ k ] , r ) , { } ) ; } else { if ( ! this . _filterd ) { this . symbolnameData = this . defaultDataCopy ; } else { this . symbolnameData = this . _previousData ; } } break ; } case "symbol" : { if ( sortOrder === "ascend" ) { this . symbolnameData = Object . keys ( this . symbolnameData ) . sort ( ( a , b ) => this . symbolnameData [ a ] . localeCompare ( this . symbolnameData [ b ] ) ) . reduce ( ( r , k ) => ( r [ k ] = this . symbolnameData [ k ] , r ) , { } ) ; } else if ( sortOrder === "descend" ) { this . symbolnameData = Object . keys ( this . symbolnameData ) . sort ( ( a , b ) => this . symbolnameData [ b ] . localeCompare ( this . symbolnameData [ a ] ) ) . reduce ( ( r , k ) => ( r [ k ] = this . symbolnameData [ k ] , r ) , { } ) ; } else { if ( ! this . _filterd ) { this . symbolnameData = this . defaultDataCopy ; } else { this . symbolnameData = this . _previousData ; } } break ; } default : { this . symbolnameData = this . defaultDataCopy ; } } } reseverState ( currentIndex , pageSize , sortMapName , sortMapSymbol ) { this . _previousIndex = currentIndex ; this . _previousPageSize = pageSize ; this . _previousSortMapName = sortMapName ; this . _previousSortMapSymbol = sortMapSymbol ; this . _previousData = { ... this . symbolnameData } ; } getPricesFull ( ) { let coinlist = Object . values ( this . symbolnameData ) ; this . priceMultiurl = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=" + coinlist . join ( ) + "&tsyms=USD&extraParams=Cryptocurrency_Market" ; return this . timer . flatMap ( result => this . result = this . _http . get ( this . priceMultiurl ) . pipe ( catchError ( this . handleError ( 'getPricesFull' , [ ] ) ) ) ) ; } getPriceSingle ( symbol ) { return this . _http . get ( "https://min-api.cryptocompare.com/data/price?fsym=" + symbol + "&tsyms=USD&extraParams=Cryptocurrency_Market" ) . map ( result => this . result = result ) . pipe ( catchError ( this . handleError ( 'getPriceSingle' , [ ] ) ) ) ; } getNamesFull ( ) { return Object . keys ( this . symbolnameData ) ; } getNameSingle ( symbol ) { return Object . keys ( this . symbolnameData ) . find ( key => this . symbolnameData [ key ] === symbol ) ; } getImagesFull ( ) { let coinlist = Object . values ( this . symbolnameData ) ; this . images = [ ] ; this . imageurlSuffix = coinlist . map ( res => res . toLowerCase ( ) ) ; for ( let symbol of this . imageurlSuffix ) { this . images . push ( this . imageurlPrefix + symbol + ".svg" ) ; } return this . images ; } getImageSingle ( symbol ) { return this . imageurlPrefix + symbol . toLowerCase ( ) + ".svg" ; } getHitoricalPrices ( symbol , prefix , timelimit , aggregate ) { return this . _http . get ( "https://min-api.cryptocompare.com/data/" + prefix + "?fsym=" + `${ symbol }` + "&tsym=USD&limit=" + timelimit + "&aggregate=" + aggregate + "&extraParams=Cryptocurrency_Market" ) . map ( result => this . result = result ) . pipe ( catchError ( this . handleError ( `getHitoricalPrices symbol=${ symbol }` ) ) ) ; } param operation param result private handleError < T > ( operation = 'operation' , result ? ) { return ( error ) : Observable < T > => { console . error ( error ) ; console . log ( `${ operation } failed: ${ error . message }` ) ; return of ( result as T ) ; } ; } /**
     * Below are functions used only in testing
     */ setCoinData ( coins ) { this . symbolnameData = coins ; } }