All files / src/support/dom class.ts

100% Statements 30/30
100% Branches 14/14
100% Functions 10/10
100% Lines 27/27

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        7x 7x           3038x 3038x   3038x 2456x 2456x 2456x 2460x     3038x               7x 6x             7x 1512x               7x 1521x               7x       8x 8x 8x   8x 11x   8x 7x 4x   3x       8x    
import { isString } from 'support/utils/types';
import { each } from 'support/utils/array';
import { keys } from 'support/utils/object';
 
const rnothtmlwhite = /[^\x20\t\r\n\f]+/g;
const classListAction = (
  elm: Element | false | null | undefined,
  className: string,
  action: (elmClassList: DOMTokenList, clazz: string) => boolean | void
): boolean => {
  let clazz: string;
  let i = 0;
  let result = false;
 
  if (elm && isString(className)) {
    const classes: Array<string> = className.match(rnothtmlwhite) || [];
    result = classes.length > 0;
    while ((clazz = classes[i++])) {
      result = !!action(elm.classList, clazz) && result;
    }
  }
  return result;
};
 
/**
 * Check whether the given element has the given class name(s).
 * @param elm The element.
 * @param className The class name(s).
 */
export const hasClass = (elm: Element | false | null | undefined, className: string): boolean =>
  classListAction(elm, className, (classList, clazz) => classList.contains(clazz));
 
/**
 * Adds the given class name(s) to the given element.
 * @param elm The element.
 * @param className The class name(s) which shall be added. (separated by spaces)
 */
export const addClass = (elm: Element | false | null | undefined, className: string): void => {
  classListAction(elm, className, (classList, clazz) => classList.add(clazz));
};
 
/**
 * Removes the given class name(s) from the given element.
 * @param elm The element.
 * @param className The class name(s) which shall be removed. (separated by spaces)
 */
export const removeClass = (elm: Element | false | null | undefined, className: string): void => {
  classListAction(elm, className, (classList, clazz) => classList.remove(clazz));
};
 
/**
 * Takes two className strings, compares them and returns the difference as array.
 * @param classNameA ClassName A.
 * @param classNameB ClassName B.
 */
export const diffClass = (
  classNameA: string | null | undefined,
  classNameB: string | null | undefined
) => {
  const classNameASplit = classNameA && classNameA.split(' ');
  const classNameBSplit = classNameB && classNameB.split(' ');
  const tempObj = {};
 
  each(classNameASplit, (className) => {
    tempObj[className] = 1;
  });
  each(classNameBSplit, (className) => {
    if (tempObj[className]) {
      delete tempObj[className];
    } else {
      tempObj[className] = 1;
    }
  });
 
  return keys(tempObj);
};