src/plugins/easycurrency/formatters/DefaultFormatter.js
import * as currencies from 'js-money/lib/currency.js';
import Formatter from './Formatter';
/**
* A default formatting implementation.
* Formats most currencies as $00.00, except JPY which is 00 $
*/
export default class DefaultFormatter extends Formatter {
/**
* Format a currency.
* @param {type} amount - Amount in cents.
* @param {type} currency - Currency code to convert to.
* @return {String}
*/
format(amount, currency) {
var currencyInfo = currencies[currency];
if (currencyInfo.code == 'JPY') {
return this._suffixSymbolNoDecimal(amount, currencyInfo, true);
}
return this._formatPrefixedSymbolTwoDecimal(amount, currencyInfo, false);
}
/**
* Format as 00 $
* @param {Number} amount
* @param {Object} currencyInfo
* @param {Boolean} space
* @return {String}
*/
_suffixSymbolNoDecimal(amount, currencyInfo, space) {
// Amount is always in cents, even for JPY
return (amount / 100) + (space ? ' ' : '') + currencyInfo.symbol_native;
}
/**
* Format as $00.00
* @param {Number} amount
* @param {Object} currencyInfo
* @param {Boolean} space
* @return {String}
*/
_formatPrefixedSymbolTwoDecimal(amount, currencyInfo, space) {
return currencyInfo.symbol_native + (space ? ' ' : '') + (amount / 100).toFixed(2);
}
}