Skip to content
Snippets Groups Projects
Select Git revision
  • 8ede41bd9268ae4c726e0f3973b860ec96bbf5f3
  • master default protected
  • 1.31
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
23 results

overlay.mjs

Blame
  • oroperator.js 2.73 KiB
    'use strict';
    
    /**
     * @author schukai GmbH
     */
    import {assignToNamespace, Monster} from '../namespace.js';
    import {AbstractOperator} from "./abstractoperator.js";
    
    /**
     * Constraints are used to define conditions that must be met by the value of a variable.
     *
     * The uniform API of the constraints allows chains to be formed.
     *
     * The OrOperator is used to link several constraints. The constraint is fulfilled if one of the constraints is fulfilled.
     *
     * ```
     * <script type="module">
     * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.29.0/dist/monster.js';
     * new Monster.Constraint.OrOperator();
     * </script>
     * ```
     *
     * Alternatively, you can also integrate this function individually.
     *
     * ```
     * <script type="module">
     * import {OrOperator} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.29.0/dist/modules/constraint/oroperator.js';
     * new OrOperator();
     * </script>
     * ```
     *
     * @example
     *
     * import {Valid} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.29.0/dist/modules/constraints/valid.js';
     * import {Invalid} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.29.0/dist/modules/constraints/invalid.js';
     * import {OrOperator} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.29.0/dist/modules/constraints/oroperator.js';
     *
     * new OrOperator(
     * new Valid(), new Invalid()).isValid()
     * .then(()=>console.log(true))
     * .catch(()=>console.log(false));
     * // ↦ true
     *
     * new OrOperator(
     * new Invalid(), new Invalid()).isValid()
     * .then(()=>console.log(true))
     * .catch(()=>console.log(false));
     * // ↦ false
     *
     * @since 1.3.0
     * @copyright schukai GmbH
     * @memberOf Monster.Constraints
     * @summary A or operator 
     */
    class OrOperator extends AbstractOperator {
    
        /**
         * this method return a promise containing the result of the check.
         *
         * @param {*} value
         * @returns {Promise}
         */
        isValid(value) {
            var self = this;
    
            return new Promise(function (resolve, reject) {
                let a, b;
    
                self.operantA.isValid(value)
                    .then(function () {
                        resolve();
                    }).catch(function () {
                    a = false;
                    /** b has already been evaluated and was not true */
                    if (b === false) {
                        reject();
                    }
                });
    
                self.operantB.isValid(value)
                    .then(function () {
                        resolve();
                    }).catch(function () {
                    b = false;
                    /** b has already been evaluated and was not true */
                    if (a === false) {
                        reject();
                    }
                });
            });
        }
    
    
    }
    
    assignToNamespace('Monster.Constraints', OrOperator);
    export {Monster, OrOperator}