@citrineos/base
Version:
The base module for OCPP v2.0.1 including all interfaces. This module is not intended to be used directly, but rather as a dependency for other modules.
54 lines • 1.6 kB
JavaScript
import { assert } from '../assertion/assertion';
/**
* ISO-4217 currency codes.
*/
const CURRENCY_CODES = ['USD', 'EUR', 'CAD', 'GBP'];
export function isCurrencyCode(value) {
return CURRENCY_CODES.includes(value);
}
export function currencyCode(value) {
assert(isCurrencyCode(value), `Unsupported currency code: ${value}`);
return value;
}
const CURRENCY_SCALES = [2];
export function isCurrencyScale(value) {
return CURRENCY_SCALES.includes(value);
}
export function currencyScale(value) {
assert(isCurrencyScale(value), `Unsupported currency scale: ${value}`);
return value;
}
/**
* Represents a currency with decimal precision.
*
* To add support for a currency:
* 1. Add the new currency code to the {@link CURRENCY_CODES} array.
* 2. Create a corresponding mapping in the {@link SUPPORTED_CURRENCIES} map.
*/
export class Currency {
constructor(code, scale) {
this._code = currencyCode(code);
this._scale = currencyScale(scale);
}
get code() {
return this._code;
}
get scale() {
return this._scale;
}
static of(code) {
assert(isCurrencyCode(code), `Unsupported currency code: ${code}`);
const currency = Currency.SUPPORTED_CURRENCIES[code];
if (currency === undefined) {
throw Error(`${code} currency is not supported`);
}
return currency;
}
}
Currency.SUPPORTED_CURRENCIES = {
USD: new Currency('USD', 2),
EUR: new Currency('EUR', 2),
CAD: new Currency('CAD', 2),
GBP: new Currency('GBP', 2),
};
//# sourceMappingURL=Currency.js.map