@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.
59 lines • 1.76 kB
JavaScript
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
import { assert } from '../assertion/assertion.js';
/**
* 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 {
static SUPPORTED_CURRENCIES = {
USD: new Currency('USD', 2),
EUR: new Currency('EUR', 2),
CAD: new Currency('CAD', 2),
GBP: new Currency('GBP', 2),
};
_code;
_scale;
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;
}
}
//# sourceMappingURL=Currency.js.map