Guest User

Untitled

a guest
Jun 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. /**
  2. * Currency Converter
  3. * Used by Locale component for currency exchange rates.
  4. */
  5.  
  6. /* Modules */
  7. import fx from "money";
  8. import accounting from "accounting";
  9. import symbol from 'currency-symbol-map';
  10. import store from 'store/dist/store.modern';
  11.  
  12. export default class Money {
  13.  
  14. constructor(base){
  15. this.base = base;
  16. this.code = store.get('locale').currency;
  17. this.prices = document.querySelectorAll('span.money');
  18. this.codes = document.querySelectorAll('span.codes');
  19. this.convert();
  20. }
  21.  
  22. convert(){
  23. fx.rates = Currency.rates;
  24. this.prices.forEach(price => this.conversion(price));
  25. this.codes.forEach(code => code.innerHTML = this.code);
  26. }
  27.  
  28. conversion(price) {
  29. const value = accounting.unformat(price.dataset.base);
  30. const convert = fx(value).from(this.code).to(config.locale.currency.base);
  31. const round = accounting.toFixed(convert, 0);
  32. const sign = symbol(this.code);
  33. const amount = accounting.formatMoney(round, {
  34. symbol: this.code,
  35. format: "%v %s",
  36. precision: 2,
  37. thousand: ','
  38. });
  39.  
  40. const format = sign === this.code ? `${amount}` : `${sign} ${amount}`;
  41.  
  42. if(!this.base) {
  43. price.innerHTML = format;
  44. } else {
  45. price.innerHTML = price.dataset.base
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment