All files / src/support/utils types.ts

100% Statements 30/30
100% Branches 45/45
100% Functions 13/13
100% Lines 29/29

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113    24x 24x     1908x       911x     24x 463x               9626x       6052x       775x       6576x       13936x       3940x               9470x 9470x   9470x                       898x     186x 186x 186x 186x 186x   186x 3x       183x         183x               1106x 1106x                       3087x 3087x            
import { PlainObject } from 'typings';
 
const ElementNodeType = Node.ELEMENT_NODE;
const { toString, hasOwnProperty } = Object.prototype;
 
export function isUndefined(obj: any): obj is undefined {
  return obj === undefined;
}
 
export function isNull(obj: any): obj is null {
  return obj === null;
}
 
export const type: (obj: any) => string = (obj) =>
  isUndefined(obj) || isNull(obj)
    ? `${obj}`
    : 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 isFunction(obj: any): obj is (...args: any[]) => any {
  return typeof obj === 'function';
}
 
export function isArray(obj: any): obj is Array<any> {
  return Array.isArray(obj);
}
 
export function isObject(obj: any): boolean {
  return typeof obj === 'object' && !isArray(obj) && !isNull(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;
  const lengthCorrectFormat = isNumber(length) && length > -1 && length % 1 == 0; // eslint-disable-line eqeqeq
 
  return isArray(obj) || (!isFunction(obj) && lengthCorrectFormat)
    ? length > 0 && isObject(obj)
      ? length - 1 in obj
      : true
    : false;
}
 
/**
 * 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 cstr = 'constructor';
  const ctor = obj[cstr];
  const ctorProto = ctor && ctor.prototype;
  const hasOwnConstructor = hasOwnProperty.call(obj, cstr);
  const hasIsPrototypeOf = ctorProto && hasOwnProperty.call(ctorProto, 'isPrototypeOf');
 
  if (ctor && !hasOwnConstructor && !hasIsPrototypeOf) {
    return false;
  }
 
  /* eslint-disable no-restricted-syntax */
  for (key in obj) {
    /**/
  }
  /* eslint-enable */
 
  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 instanceofObj = window.HTMLElement;
  return obj
    ? instanceofObj
      ? obj instanceof instanceofObj
      : obj.nodeType === ElementNodeType
    : false;
}
 
/**
 * Checks whether the given object is a Element.
 * @param obj The object which shall be checked.
 */
export function isElement(obj: any): obj is Element {
  const instanceofObj = window.Element;
  return obj
    ? instanceofObj
      ? obj instanceof instanceofObj
      : obj.nodeType === ElementNodeType
    : false;
}