UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

163 lines (161 loc) 4.79 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. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.StrongData = void 0; const transforms_1 = require("../transforms"); /** * @category Core */ class StrongData { constructor(fallbackDefault, value, rules, typeId) { this.value = null; this.fallbackDefault = fallbackDefault; this.transforms = new transforms_1.Transforms(fallbackDefault); this.rules = rules; this.typeId = typeId; this.baseType = 'StrongData'; this.initial = value; this.set(value); } /** * Check if value passes this instance's rule validation. * @param value * @returns */ check(value) { if (value === undefined) { return false; } return this.rules.run(value); } get(fallback) { if (this.value === null) { if (fallback === undefined || fallback === null) { return this.fallbackDefault; } return fallback; } return this.value; } set(value) { if (value === undefined) { return false; } if (value === null) { this.value = null; return true; } const transformed = value; if (!this.check(value)) { return false; } this.value = transformed; return true; } getNull() { if (this.value === undefined || this.value === null) { return null; } return this.value; } reset() { this.value = this.initial; } /** * Divide current `value` by `divisor`. Result is zero when * `divisor` or `value` are zero. * @param divisor * @returns */ div(divisor) { const curr = this.getNull(); if (typeof divisor !== 'number' || typeof curr !== 'number') { return null; } if (divisor === 0 || curr === 0) { this.set(0); return 0; } const result = curr / divisor; if (isNaN(result)) { return null; } this.set(result); return result; } mul(value) { const curr = this.getNull(); if (typeof value !== 'number' || typeof curr !== 'number') { return null; } if (value === 0 || curr === 0) { this.set(0); return 0; } const result = value * curr; return this.set(result) ? result : null; } /** * * @param exponent * @returns */ pow(exponent) { const curr = this.getNull(); if (typeof curr !== 'number' || curr === null) { return null; } const result = Math.pow(curr, exponent); if (isNaN(result)) { return null; } if (result >= Number.MAX_SAFE_INTEGER) { return null; } this.set(result); return result; } /** * Add value to Strong Type's current value, if it is a numeric type. Operation * ignored for non-numeric types. * @param value * @returns */ add(value) { const curr = this.getNull(); if (typeof value !== 'number' || typeof curr !== 'number') { return null; } const result = value + curr; if (isNaN(result)) { return null; } if (result < Number.MIN_SAFE_INTEGER || result > Number.MAX_SAFE_INTEGER) { return null; } return this.set(result) ? result : null; } } exports.StrongData = StrongData;