All files / ts utils.ts

100% Statements 33/33
100% Branches 24/24
100% Functions 10/10
100% Lines 31/31
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 9921x 21x                     5x       5x     5x         21x 9x 2x     7x 3x     4x             21x     7x       300x       20x       18x     3825x     3x       3x 1x     2x       4x       4x   4x 3x     1x   1x     21x 9x 9x 9x              
import * as randomSeed from 'random-seed';
import {
  MATCHES_AMPERSAND,
  MATCHES_BLANK_FIRST_LINE,
  MATCHES_BLANK_LAST_LINE,
  MATCHES_INITIAL_INDENTATION,
  MATCHES_LEADING_AND_TRAILING_HYPHENS,
  MATCHES_NON_WORD_CHARACTERS,
  MATCHES_WHITESPACE,
} from './constants';
 
export const formatCode = (code: string) => {
  const codeWithoutLeadingOrTrailingEmptyLines = code
    .replace(MATCHES_BLANK_FIRST_LINE, '')
    .replace(MATCHES_BLANK_LAST_LINE, '');
 
  const initialIndentation: RegExpExecArray | null =
    MATCHES_INITIAL_INDENTATION.exec(codeWithoutLeadingOrTrailingEmptyLines);
 
  return initialIndentation ?
    codeWithoutLeadingOrTrailingEmptyLines.replace(new RegExp(`^${initialIndentation[1]}`, 'gm'), '') :
    codeWithoutLeadingOrTrailingEmptyLines;
}
 
export const getHref = (children?: React.ReactNode, href?: string): string | undefined => {
  if (href) {
    return href;
  }
 
  if (typeof children !== 'string') {
    return undefined;
  }
 
  return children
    .replace(MATCHES_AMPERSAND, '-and-')
    .replace(MATCHES_NON_WORD_CHARACTERS, '-')
    .replace(MATCHES_LEADING_AND_TRAILING_HYPHENS, '')
    .toLowerCase();
}
 
let rand = randomSeed.create('dabapps');
 
export const resetRandomSeed = () => {
  rand = randomSeed.create('dabapps');
}
 
export const generateIpsum = (words: string[]) => {
  const ipsum = Array.apply(null, new Array(15)).map(() => (
    words[Math.floor(rand.range(words.length))]
  )).join(' ');
 
  return ipsum.charAt(0).toUpperCase() + ipsum.substring(1) + '.';
}
 
export const shouldNotBeRendered = (children: any) => {
  return children === false || children === null || children === undefined || children === '';
}
 
export const isValidColumnNumber = (value?: number) => typeof value === 'number' && value === +value;
 
export const addClassName = (element: HTMLElement, className: string) => {
  const myClassNames = element.className
    .trim()
    .split(MATCHES_WHITESPACE);
 
  if (myClassNames.indexOf(className) >= 0) {
    return;
  }
 
  element.className = [...myClassNames, className].join(' ');
};
 
export const removeClassName = (element: HTMLElement, className: string) => {
  const myClassNames = element.className
    .trim()
    .split(MATCHES_WHITESPACE);
 
  const index = myClassNames.indexOf(className);
 
  if (index < 0) {
    return;
  }
 
  myClassNames.splice(index, 1);
 
  element.className = myClassNames.join(' ');
};
 
export const getScrollOffset = () => {
  const doc = document.documentElement;
  const left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
  const top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
 
  return {
    x: left,
    y: top,
  };
}