UNPKG

@bitloops/bl-boilerplate-core

Version:

TypeScript boilerplate code for Bitloops Language generated projects

77 lines (76 loc) 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CurrencyVO = exports.AVAILABLE_CURRENCY_STANDARDS = exports.ISO_4217_CURRENCY_CODES = void 0; const Either_1 = require("../../../Either"); const ValueObject_1 = require("../../ValueObject"); const applyRule_1 = require("../../applyRule"); const errors_1 = require("./errors"); const rules_1 = require("./rules"); const ISO_4217 = 'ISO_4217'; exports.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, }, }; exports.AVAILABLE_CURRENCY_STANDARDS = { [ISO_4217]: { name: ISO_4217, }, }; class CurrencyVO extends ValueObject_1.ValueObject { 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 = exports.ISO_4217_CURRENCY_CODES; this.availableCurrencyStandards = exports.AVAILABLE_CURRENCY_STANDARDS; if (props.standard) this.standard = props.standard; else this.standard = ISO_4217; } static create(props) { const rules = [new rules_1.CurrencyShouldExistRule(props.currencyCode.toUpperCase())]; if (props.standard) rules.push(new rules_1.ISOShouldBeSupportedRule(props.standard)); const res = (0, applyRule_1.applyRules)(rules); if (res) return (0, Either_1.fail)(res); return (0, Either_1.ok)(new CurrencyVO(props)); } toPrimitives() { return { currencyCode: this.currencyCode, standard: this.standard, }; } static fromPrimitives(props) { return new CurrencyVO(props); } } exports.CurrencyVO = CurrencyVO; CurrencyVO.Errors = { CurrencyStandardDoesNotExistError: errors_1.CurrencyStandardDoesNotExistError, CurrencyCodeDoesNotExistError: errors_1.CurrencyCodeDoesNotExistError, };