types/is.js

'use strict';

/**
 * @author schukai GmbH
 */

import {Monster} from '../namespace.js';

/**
 * with this function you can check if a value is iterable
 *
 * you can call the method via the monster namespace `Monster.Types.isPrimitive()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(Monster.Types.isIterable(null)) // ↦ false
 * console.log(Monster.Types.isIterable('hello')) // ↦ true
 * console.log(Monster.Types.isIterable([])) // ↦ true
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {isIterable} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(isIterable(null))  // ↦ false
 * console.log(isIterable('hello'))  // ↦ true
 * console.log(isIterable([]))  // ↦ true
 * </script>
 * ```
 *
 * @param {*} value
 * @returns {boolean}
 * @since 1.2.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
function isIterable(value) {
    if (value === undefined) return false;
    if (value === null) return false;
    return typeof value?.[Symbol.iterator] === 'function';
}


/**
 * checks whether the value passed is a primitive (string, number, boolean, NaN, undefined, null or symbol)
 *
 * you can call the method via the monster namespace `Monster.Types.isPrimitive()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(Monster.Types.isPrimitive('2')) // ↦ false
 * console.log(Monster.Types.isPrimitive([])) // ↦ true
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {isPrimitive} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(isPrimitive('2'))  // ↦ true
 * console.log(isPrimitive([]))  // ↦ false
 * </script>
 * ```
 *
 * @param {*} value
 * @returns {boolean}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
function isPrimitive(value) {
    var type;

    if (value === undefined || value === null || value === NaN) {
        return true;
    }

    type = typeof value;

    if (type === 'string' || type === 'number' || type === 'boolean' || type === 'symbol') {
        return true;
    }

    return false;
}

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

    if (value === true || value === false) {
        return true;
    }

    return false;
}

/**
 * checks whether the value passed is a string
 *
 * you can call the method via the monster namespace `Monster.Types.isString()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(Monster.Types.isString('2')) // ↦ true
 * console.log(Monster.Types.isString([])) // ↦ false
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {isString} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(isString('2'))  // ↦ true
 * console.log(isString([]))  // ↦ false
 * </script>
 * ```
 *
 * @param {*} value
 * @returns {boolean}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
function isString(value) {
    if (value === undefined || typeof value !== 'string') {
        return false;
    }
    return true;
}

/**
 * checks whether the value passed is a object
 *
 * you can call the method via the monster namespace `Monster.Types.isObject()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(Monster.Types.isObject('2')) // ↦ false
 * console.log(Monster.Types.isObject([])) // ↦ false
 * console.log(Monster.Types.isObject({})) // ↦ true
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {isObject} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(isObject('2'))  // ↦ false
 * console.log(isObject([]))  // ↦ false
 * </script>
 * ```
 *
 * @param {*} value
 * @returns {boolean}
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
function isObject(value) {

    if (isArray(value)) return false;
    if (isPrimitive(value)) return false;

    if (typeof value === 'object') {
        return true;
    }

    return false;
}

/**
 * checks whether the value passed is a object and instance of instance
 *
 * you can call the method via the monster namespace `Monster.Types.isInstance()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(Monster.Types.isInstance('2')) // ↦ false
 * console.log(Monster.Types.isInstance([])) // ↦ false
 * console.log(Monster.Types.isInstance({})) // ↦ true
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {isInstance} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(isInstance('2'))  // ↦ false
 * console.log(isInstance([]))  // ↦ false
 * </script>
 * ```
 *
 * @param {*} value
 * @param {*} instance
 * @returns {boolean}
 * @since 1.5.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
function isInstance(value, instance) {

    if (!isObject(value)) return false;
    if (!isFunction(instance)) return false;
    if (!instance.hasOwnProperty('prototype')) return false;
    return (value instanceof instance)?true:false; 

}

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

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

    if (typeof value === 'function') {
        return true;
    }

    return false;

}

/**
 * checks whether the value passed is an integer
 *
 * you can call the method via the monster namespace `Monster.Types.isFunction()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(Monster.Types.isInteger(()=>{})) // ↦ true
 * console.log(Monster.Types.isInteger('2')) // ↦ false
 * console.log(Monster.Types.isInteger(2)) // ↦ true
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {isInteger} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/is.js';
 * console.log(isInteger(()=>{})) // ↦ true
 * console.log(isInteger('2'))  // ↦ false
 * console.log(isInteger(2))  // ↦ true
 * </script>
 * ```
 *
 * @param {*} value
 * @returns {boolean}
 * @since 1.4.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
function isInteger(value) {
    return Number.isInteger(value);
}


Monster.assignToNamespace('Monster.Types', isPrimitive, isBoolean, isString, isObject, isArray, isFunction, isIterable, isInteger);
export {Monster, isPrimitive, isBoolean, isString, isObject, isInstance, isArray, isFunction, isIterable, isInteger}