File

projects/maplander/components/src/lib/services/currency-converter/currency-converter.service.ts

Index

Methods

Methods

Private getValueByCurrency
getValueByCurrency(currencies: CER[], c: Currency)
Parameters :
Name Type Optional
currencies CER[] No
c Currency No
Returns : number
list
list(prices: number[], currencies: CER[], from: Currency, to: Currency)
Parameters :
Name Type Optional
prices number[] No
currencies CER[] No
from Currency No
to Currency No
Returns : number[]
number
number(price: number, currencies: CER[], from: Currency, to: Currency)
Parameters :
Name Type Optional
price number No
currencies CER[] No
from Currency No
to Currency No
Returns : number
import {Injectable} from '@angular/core';
import {CER, Currency} from '@maplander/types';

@Injectable()
export class CurrencyConverterService {

  list(prices: number[], currencies: CER[], from: Currency, to: Currency): number[] {
    const valueFrom = this.getValueByCurrency(currencies, from), valueTo = this.getValueByCurrency(currencies, to);
    prices = prices.map(price => {
      if (from === to) {
        return price;
      } else {
        return valueTo * (price / valueFrom);
      }
    });
    return prices;
  }

  number(price: number, currencies: CER[], from: Currency, to: Currency): number {
    const valueFrom = this.getValueByCurrency(currencies, from), valueTo = this.getValueByCurrency(currencies, to);
    return valueTo * (price / valueFrom);
  }

  private getValueByCurrency(currencies: CER[], c: Currency): number {
    let value = 0;
    currencies.forEach(currency => {
      if (currency.type === c.toString()) {
        value = currency.value;
      }
    });
    return value;
  }
}

result-matching ""

    No results matching ""