UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

118 lines (116 loc) 3.98 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.Transforms = void 0; /** * Container holding the transform function chain used to transform * values before saving. Transforms are applied in the order they are * stored in the transforms or transformsNB array. * * @category Transforms */ class Transforms { constructor(fallbackDefault) { this.transforms = []; this.transformsNB = []; this.fallbackDefault = fallbackDefault; } /** * Add nullable transform function to chain. * @param transform Nullable transform fn to be added to chain. * @returns Boolean indicating add success or failure. * true - Transform fn added to chain successfully. * false - Transform fn not added to chain. */ addNB(transform) { if (!transform) { return false; } this.transformsNB.push(transform); return true; } /** * Add transform function to chain. * @param transform Transform fn to be added to chain. * @returns Boolean indicating add success or failure. * true - Transform fn added to chain successfully. * false - Transform fn not added to chain. */ add(transform) { if (!transform) { return false; } // todo: add sorted insert here based on filter.sortOrder this.transforms.push(transform); return true; } /** * Run transform chain, applying each transforms once the order added to * the chain. * @param value * @returns */ run(value) { if (typeof value === 'undefined' || value === null) { return this.fallbackDefault; } let transformed = value; const transforms = this.transforms; for (const transform of transforms) { const input = transformed; const output = transform.run(input); transformed = output; } return transformed; } /** * Run transform chain, applying each transforms once the order added to * the chain. * @param value * @returns */ runNB(value) { if (typeof value === 'undefined' || value === null) { return null; } let transformed = value; const transformsNB = this.transformsNB; for (const transform of transformsNB) { const input = transformed; const output = transform.run(input); transformed = output; } return transformed; } /** * Remove all transform functions from chain. Fallback value * remains the same. */ reset() { this.transforms.length = 0; this.transformsNB.length = 0; } } exports.Transforms = Transforms;