UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

124 lines (122 loc) 4.73 kB
"use strict"; /** * MIT License * * Copyright (c) 2019 - 2021 Toreda, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.safeMoneyMake = void 0; const big_js_1 = __importDefault(require("big.js")); const rules_1 = require("../../rules"); const type_1 = require("../../create/type"); const big_1 = require("../../to/safe/money/big"); const BIG_ZERO = (0, big_js_1.default)(0); const BIG_ONE = (0, big_js_1.default)(1); /** * * @param fallback * @param initial * @returns * * @category Maths */ function safeMoneyMake(fallback, initial) { const rules = new rules_1.Rules(); rules.add().must.match.type.big(); const bigFallback = (0, big_1.toSafeMoneyBig)(fallback); const bigInitial = (0, big_1.toSafeMoneyBig)(initial); const strong = (0, type_1.createType)(bigFallback !== null && bigFallback !== void 0 ? bigFallback : BIG_ZERO, bigInitial, rules, 'SafeMoney'); return Object.assign(strong, { increment: () => { const value = strong._data.getNull(); if (value === null) { return null; } const result = value.add(BIG_ONE); return strong._data.set(result) ? result : null; }, decrement: () => { const value = strong._data.getNull(); if (value === null) { return null; } const result = value.minus(BIG_ONE); return strong._data.set(result) ? result : null; }, mul: (input) => { const curr = strong.get(BIG_ZERO); const value = (0, big_1.toSafeMoneyBig)(input); if (value === null || curr === null) { return null; } const result = curr.mul(value); return strong._data.set(result) ? result : null; }, pow: (exponent) => { const curr = strong._data.getNull(); const value = (0, big_1.toSafeMoneyBig)(exponent); if (curr === null || value === null) { return null; } const result = curr.pow(value.toNumber()); return strong._data.set(result) ? result : null; }, div: (input) => { const curr = strong.get(BIG_ZERO); const value = (0, big_1.toSafeMoneyBig)(input); if (curr === null || value === null) { return null; } if (value === BIG_ZERO || curr === BIG_ZERO) { return null; } const result = curr.div(value); return strong._data.set(result) ? result : null; }, add: (input) => { const value = (0, big_1.toSafeMoneyBig)(input); const curr = strong.getNull(); if (value === null || curr === null) { return null; } const result = curr.add(value); return strong._data.set(result) ? result : null; }, sub: (input) => { const value = (0, big_1.toSafeMoneyBig)(input); const curr = strong.getNull(); if (value === null || curr === null) { return null; } const result = curr.minus(value); return strong._data.set(result) ? result : null; }, round: (_input) => { return null; }, typeId: 'SafeMoney', baseType: 'StrongType' }); } exports.safeMoneyMake = safeMoneyMake;