constraints/isarray.js

'use strict';

/**
 * @author schukai GmbH
 */

import {Monster, AbstractConstraint} from "./abstract.js";
import {isArray} from "../types/is.js";

/**
 * You can call the method via the monster namespace `new Monster.Constraint.IsObject()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/constraints/isarray.js';
 * console.log(new Monster.Constraint.IsArray())
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {IsArray} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/constraints/isarray.js';
 * console.log(new IsArray())
 * </script>
 * ```
 * 
 * @example
 *
 * import {IsArray} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/constraints/isarray.js';
 *
 * new IsArray()
 * .isValid([])
 * .then(()=>console.log(true));
 * // ↦ true
 *
 * new IsArray()
 * .isValid(99)
 * .catch(e=>console.log(e));
 * // ↦ 99
 *
 * @since 1.3.0
 * @copyright schukai GmbH
 * @memberOf Monster/Constraints
 */
class IsArray extends AbstractConstraint {

    /**
     * this method return a promise containing the result of the check.
     *
     * @param {*} value
     * @returns {Promise}
     */
    isValid(value) {
        if (isArray(value)) {
            return Promise.resolve(value);
        }

        return Promise.reject(value);
    }

}

Monster.assignToNamespace('Monster.Constraints', IsArray);
export {Monster, IsArray}