types/validate.js

'use strict';

/**
 * @author schukai GmbH
 */

import {Monster} from '../namespace.js';
import {
    isPrimitive,
    isBoolean,
    isString,
    isObject,
    isArray,
    isFunction,
    isIterable,
    isInstance,
    isInteger
} from './is.js';

/**
 * this method checks if the type matches the primitive type. this function is identical to isPrimitive() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validatePrimitive()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateIterable('2')) // ↦ TypeError
 * console.log(Monster.Types.validateIterable([])) // ↦ undefined
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateIterable} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateIterable('2'))  // ↦ TypeError
 * console.log(validateIterable([]))  // ↦ undefined
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.2.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not a primitive
 * @see {@link isPrimitive}
 * @see {@link Monster/Types/isPrimitive}
 * @see {@link Monster/Types#isPrimitive}
 */
function validateIterable(value) {
    if (!isIterable(value)) {
        throw new TypeError('value is not iterable')
    }
}

/**
 * this method checks if the type matches the primitive type. this function is identical to isPrimitive() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validatePrimitive()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validatePrimitive('2')) // ↦ undefined
 * console.log(Monster.Types.validatePrimitive([])) // ↦ TypeError
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validatePrimitive} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validatePrimitive('2'))  // ↦ undefined
 * console.log(validatePrimitive([]))  // ↦ TypeError
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not a primitive
 * @see {@link isPrimitive}
 * @see {@link Monster/Types/isPrimitive}
 * @see {@link Monster/Types#isPrimitive}
 */
function validatePrimitive(value) {
    if (!isPrimitive(value)) {
        throw new TypeError('value is not a primitive')
    }
}

/**
 * this method checks if the type matches the boolean type. this function is identical to isBoolean() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validateBoolean()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateBoolean(true)) // ↦ undefined
 * console.log(Monster.Types.validateBoolean('2')) // ↦ TypeError
 * console.log(Monster.Types.validateBoolean([])) // ↦ TypeError
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateBoolean} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateBoolean(false))  // ↦ undefined
 * console.log(validateBoolean('2'))  // ↦ TypeError
 * console.log(validateBoolean([]))  // ↦ TypeError
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types

 * @throws {TypeError}  value is not primitive
 */
function validateBoolean(value) {
    if (!isBoolean(value)) {
        throw new TypeError('value is not a boolean')
    }
}

/**
 * this method checks if the type matches the string type. this function is identical to isString() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validateString()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateString('2')) // ↦ undefined
 * console.log(Monster.Types.validateString([])) // ↦ TypeError
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateString} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateString('2'))  // ↦ undefined
 * console.log(validateString([]))  // ↦ TypeError
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not a string
 */
function validateString(value) {
    if (!isString(value)) {
        throw new TypeError('value is not a string')
    }
}


/**
 * this method checks if the type matches the object type. this function is identical to isObject() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validateObject()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateObject({})) // ↦ undefined
 * console.log(Monster.Types.validateObject('2')) // ↦ TypeError
 * console.log(Monster.Types.validateObject([])) // ↦ TypeError
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateObject} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateObject({}))  // ↦ undefined
 * console.log(validateObject('2'))  // ↦ TypeError
 * console.log(validateObject([]))  // ↦ TypeError
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not a object
 */
function validateObject(value) {
    if (!isObject(value)) {
        throw new TypeError('value is not a object')
    }
}

/**
 * this method checks if the type matches the object instance.
 *
 * you can call the method via the monster namespace `Monster.Types.validateInstance()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateInstance({}, Object)) // ↦ undefined
 * console.log(Monster.Types.validateInstance('2', Object)) // ↦ TypeError
 * console.log(Monster.Types.validateInstance([], Object)) // ↦ TypeError
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateInstance} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateInstance({}, Object)) // ↦ undefined
 * console.log(validateInstance('2', Object)) // ↦ TypeError
 * console.log(validateInstance([], Object)) // ↦ TypeError
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.5.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not an instance of
 */
function validateInstance(value, instance) {
    if (!isInstance(value, instance)) {
        let n = "";
        if (isObject(instance) || isFunction(instance)) {
            n = instance?.['name']
        }

        if (n) {
            n = " " + n;
        }

        throw new TypeError('value is not an instance of' + n)
    }
}

/**
 * this method checks if the type matches the array type. this function is identical to isArray() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validateArray()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateArray('2')) // ↦ TypeError
 * console.log(Monster.Types.validateArray([])) // ↦ undefined
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateArray} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateArray('2'))  // ↦ TypeError
 * console.log(validateArray([]))  // ↦ undefined
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not an array
 */
function validateArray(value) {
    if (!isArray(value)) {
        throw new TypeError('value is not an array')
    }
}

/**
 * this method checks if the type matches the function type. this function is identical to isFunction() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validateFunction()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateFunction(()=>{})) // ↦ undefined
 * console.log(Monster.Types.validateFunction('2')) // ↦ TypeError
 * console.log(Monster.Types.validateFunction([])) // ↦ TypeError
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateFunction} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateFunction(()=>{})) // ↦ undefined
 * console.log(validateFunction('2'))  // ↦ TypeError
 * console.log(validateFunction([]))  // ↦ TypeError
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not a function
 */
function validateFunction(value) {
    if (!isFunction(value)) {
        throw new TypeError('value is not a function')
    }
}

/**
 * this method checks if the type is an integer. this function is identical to isInteger() except that a TypeError is thrown.
 *
 * you can call the method via the monster namespace `Monster.Types.validateInteger()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(Monster.Types.validateInteger(true)) // ↦ TypeError
 * console.log(Monster.Types.validateInteger('2')) // ↦ TypeError
 * console.log(Monster.Types.validateInteger(2)) // ↦ undefined
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {validateFunction} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/validate.js';
 * console.log(validateInteger(true)) // ↦ TypeError
 * console.log(validateInteger('2'))  // ↦ TypeError
 * console.log(validateInteger(2))  // ↦ undefined
 * </script>
 * ```
 *
 * @param {*} value
 * @return {undefined}
 * @since 1.4.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 * @throws {TypeError} value is not an integer
 */
function validateInteger(value) {
    if (!isInteger(value)) {
        throw new TypeError('value is not an integer')
    }
}

Monster.assignToNamespace('Monster.Types', validatePrimitive, validateBoolean, validateString, validateObject, validateArray, validateFunction, validateIterable, validateInteger);
export {
    Monster,
    validatePrimitive,
    validateBoolean,
    validateString,
    validateObject,
    validateInstance,
    validateArray,
    validateFunction,
    validateIterable,
    validateInteger
}