@bitloops/bl-boilerplate-core
Version:
TypeScript boilerplate code for Bitloops Language generated projects
77 lines (76 loc) • 2.37 kB
JavaScript
import { fail, ok } from '../../../Either';
import { ValueObject } from '../../ValueObject';
import { applyRules } from '../../applyRule';
import { CurrencyCodeDoesNotExistError, CurrencyStandardDoesNotExistError } from './errors';
import { CurrencyShouldExistRule, ISOShouldBeSupportedRule } from './rules';
const ISO_4217 = 'ISO_4217';
export const ISO_4217_CURRENCY_CODES = {
EUR: {
NUMBER: '978',
CURRENCY_NAME: 'Euro',
CURRENCY_SYMBOL: '€',
NUMBER_OF_DIGITS_AFTER_DECIMAL: 2,
},
USD: {
NUMBER: '840',
CURRENCY_NAME: 'United States dollar',
CURRENCY_SYMBOL: '$',
NUMBER_OF_DIGITS_AFTER_DECIMAL: 2,
},
};
export const AVAILABLE_CURRENCY_STANDARDS = {
[ISO_4217]: {
name: ISO_4217,
},
};
export class CurrencyVO extends ValueObject {
code;
ISO4217CurrencyCodes;
availableCurrencyStandards;
standard;
static Errors = {
CurrencyStandardDoesNotExistError,
CurrencyCodeDoesNotExistError,
};
get currencyCode() {
return this.code;
}
get symbol() {
return this.ISO4217CurrencyCodes[this.code].CURRENCY_SYMBOL;
}
get name() {
return this.ISO4217CurrencyCodes[this.code].CURRENCY_NAME;
}
get availableStandards() {
return Object.keys(this.availableCurrencyStandards);
}
// TODO find a way to export the VO class using private keyword
constructor(props) {
super(props);
this.code = this.props.currencyCode.toUpperCase();
this.ISO4217CurrencyCodes = ISO_4217_CURRENCY_CODES;
this.availableCurrencyStandards = AVAILABLE_CURRENCY_STANDARDS;
if (props.standard)
this.standard = props.standard;
else
this.standard = ISO_4217;
}
static create(props) {
const rules = [new CurrencyShouldExistRule(props.currencyCode.toUpperCase())];
if (props.standard)
rules.push(new ISOShouldBeSupportedRule(props.standard));
const res = applyRules(rules);
if (res)
return fail(res);
return ok(new CurrencyVO(props));
}
toPrimitives() {
return {
currencyCode: this.currencyCode,
standard: this.standard,
};
}
static fromPrimitives(props) {
return new CurrencyVO(props);
}
}