All files / src/core/utils types.ts

100% Statements 32/32
100% Branches 37/37
100% Functions 13/13
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96    11x 576x 1x 575x 48x 527x       97x       114x       28x       1605x       464x       611x       500x       2666x                 761x 761x               1337x 1123x     214x 214x 214x 214x   214x 3x     211x   211x               358x 358x 358x                   105x 42x 63x  
import { PlainObject } from 'core/typings';
 
export const type: (obj: any) => string = (obj) => {
    if (obj === undefined)
        return obj + '';
    if (obj === null)
        return obj + '';
    return Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, '$1').toLowerCase();
}
 
export function isNumber(obj: any): obj is number {
    return typeof obj === 'number';
};
 
export function isString(obj: any): obj is string {
    return typeof obj === 'string';
}
 
export function isBoolean(obj: any): obj is boolean {
    return typeof obj === 'boolean';
}
 
export function isObject(obj: any): boolean {
    return typeof obj === 'object' && !isArray(obj) && !isNull(obj);
}
 
export function isFunction(obj: any): obj is Function {
    return typeof obj === 'function';
}
 
export function isUndefined(obj: any): obj is undefined {
    return obj === undefined;
}
 
export function isNull(obj: any): obj is null {
    return obj === null;
}
 
export function isArray(obj: any): obj is Array<any> {
    return Array.isArray(obj);
}
 
 
/**
 * Returns true if the given object is array like, false otherwise.
 * @param obj The Object
 */
export function isArrayLike<T extends PlainObject = any>(obj: any): obj is ArrayLike<T> {
    const length = !!obj && obj.length;
    return isArray(obj) || (!isFunction(obj) && isNumber(length) && length > -1 && length % 1 == 0);
}
 
/**
 * Returns true if the given object is a "plain" (e.g. { key: value }) object, false otherwise. 
 * @param obj The Object.
 */
export function isPlainObject<T = any>(obj: any): obj is PlainObject<T> {
    if (!obj || !isObject(obj) || type(obj) !== 'object')
        return false;
 
    let key;
    const proto = 'prototype';
    const hasOwnProperty = Object[proto].hasOwnProperty;
    const hasOwnConstructor = hasOwnProperty.call(obj, 'constructor');
    const hasIsPrototypeOf = obj.constructor && obj.constructor[proto] && hasOwnProperty.call(obj.constructor[proto], 'isPrototypeOf');
 
    if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) {
        return false;
    }
 
    for (key in obj) { /**/ }
 
    return isUndefined(key) || hasOwnProperty.call(obj, key);
};
 
/**
 * Checks whether the given object is a HTMLElement.
 * @param obj The object which shall be checked.
 */
export function isHTMLElement(obj: any): obj is HTMLElement {
    const instaceOfRightHandSide = window.HTMLElement;
    const doInstanceOf = isObject(instaceOfRightHandSide) || isFunction(instaceOfRightHandSide);
    return !!(
        doInstanceOf ? obj instanceof instaceOfRightHandSide : (obj && isObject(obj) && obj.nodeType === 1 && isString(obj.nodeName))
    );
}
 
/**
 * Returns true if the given object is empty, false otherwise.
 * @param obj The Object.
 */
export function isEmptyObject(obj: any): boolean {
    for (let name in obj)
        return false;
    return true;
};