summaryrefslogtreecommitdiff
path: root/src/utils/index.js
blob: 48c90afe0c393280e42eeefbab9c25b2b0179509 (plain)
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
/**
 * Utility functions
 */

export const rand = (n) => Math.random() * n;
export const randint = (limit) => Math.floor(Math.random() * limit);
export const choice = (list) => list[randint(list.length)];

export const mod = (n, m) => n - m * Math.floor(n / m);

export const pad = (value) => (value < 10 ? "0" + value : value);

export const capitalizeWord = (text = "") =>
  text ? text.charAt(0).toUpperCase() + text.slice(1) : "";

export const capitalize = (text = "") =>
  String(text || "")
    .split(" ")
    .map(capitalizeWord)
    .join(" ");

/* Mobile check */

function isIpadOS() {
  return (
    navigator.userAgent.match(/iPad/i) ||
    (navigator.maxTouchPoints &&
      navigator.maxTouchPoints > 2 &&
      /MacIntel/.test(navigator.platform))
  );
}

export const isiPhone = !!(
  navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)
);
export const isiPad = isIpadOS();
export const isAndroid = !!navigator.userAgent.match(/Android/i);
export const isMobile = isiPhone || isiPad || isAndroid;
export const isDesktop = !isMobile;

const htmlClassList = document.body.parentNode.classList;
htmlClassList.add(isDesktop ? "desktop" : "mobile");