aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/lib/utils.ts
blob: 0fe5bdfdc61679d80558db60c6690f99b391a9f0 (plain) (blame)
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
"use client";
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

// removes http(s?):// and / from the url
export function cleanUrl(url: string) {
  if (url.endsWith("/")) {
    url = url.slice(0, -1);
  }
  return url.startsWith("https://")
    ? url.slice(8)
    : url.startsWith("http://")
      ? url.slice(7)
      : url;
}

export function getIdsFromSource(sourceIds: string[]) {
  console.log(sourceIds);
  return sourceIds.map((id) => {
    const parts = id.split("-");
    if (parts.length > 1) {
      return parts.slice(0, -1).join("-");
    } else {
      return id;
    }
  });
}

export function generateId() {
  return Math.random().toString(36).slice(2, 9);
}

export function svgId(prefix: string, id: string) {
  return `${prefix}-${id}`;
}

export function countLines(textarea: HTMLTextAreaElement): number {
  let _buffer: HTMLTextAreaElement | null = null;

  if (_buffer == null) {
    _buffer = document.createElement("textarea");
    _buffer.style.border = "none";
    _buffer.style.height = "0";
    _buffer.style.overflow = "hidden";
    _buffer.style.padding = "0";
    _buffer.style.position = "absolute";
    _buffer.style.left = "0";
    _buffer.style.top = "0";
    _buffer.style.zIndex = "-1";
    document.body.appendChild(_buffer);
  }

  const cs = window.getComputedStyle(textarea);
  const pl = parseInt(cs.paddingLeft as string);
  const pr = parseInt(cs.paddingRight as string);
  let lh = parseInt(cs.lineHeight as string);

  // [cs.lineHeight] may return 'normal', which means line height = font size.
  if (isNaN(lh)) lh = parseInt(cs.fontSize as string);

  // Copy content width.
  if (_buffer) {
    _buffer.style.width = textarea.clientWidth - pl - pr + "px";

    // Copy text properties.
    _buffer.style.font = cs.font as string;
    _buffer.style.letterSpacing = cs.letterSpacing as string;
    _buffer.style.whiteSpace = cs.whiteSpace as string;
    _buffer.style.wordBreak = cs.wordBreak as string;
    _buffer.style.wordSpacing = cs.wordSpacing as string;
    _buffer.style.wordWrap = cs.wordWrap as string;

    // Copy value.
    _buffer.value = textarea.value;

    const result = Math.floor(_buffer.scrollHeight / lh);
    return result > 0 ? result : 1;
  }

  return 0;
}

export function convertRemToPixels(rem: number) {
  return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
}

export function isArraysEqual(a: any[], b: any[]) {
	if (a === b) return true;
  if (a == null || b == null) return false;
  if (a.length !== b.length) return false;

	let isEqual = true;
	
	a.forEach(i => {
		if (!isEqual) return
		isEqual = b.includes(i)
	})

	if (!isEqual)
		return isEqual

	b.forEach(i => {
		if (!isEqual) return
		isEqual = a.includes(i)
	})

	return isEqual

}