Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Currency Converter
- * Used by Locale component for currency exchange rates.
- */
- /* Modules */
- import fx from "money";
- import accounting from "accounting";
- import symbol from 'currency-symbol-map';
- import store from 'store/dist/store.modern';
- export default class Money {
- constructor(base){
- this.base = base;
- this.code = store.get('locale').currency;
- this.prices = document.querySelectorAll('span.money');
- this.codes = document.querySelectorAll('span.codes');
- this.convert();
- }
- convert(){
- fx.rates = Currency.rates;
- this.prices.forEach(price => this.conversion(price));
- this.codes.forEach(code => code.innerHTML = this.code);
- }
- conversion(price) {
- const value = accounting.unformat(price.dataset.base);
- const convert = fx(value).from(this.code).to(config.locale.currency.base);
- const round = accounting.toFixed(convert, 0);
- const sign = symbol(this.code);
- const amount = accounting.formatMoney(round, {
- symbol: this.code,
- format: "%v %s",
- precision: 2,
- thousand: ','
- });
- const format = sign === this.code ? `${amount}` : `${sign} ${amount}`;
- if(!this.base) {
- price.innerHTML = format;
- } else {
- price.innerHTML = price.dataset.base
- }
- }
- }
Add Comment
Please, Sign In to add comment