Bin
2025-12-17 21f0498f62ada55651f4d232327e15fc47f498b1
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
declare global {
  interface Window {
    APP_SETTINGS: any;
  }
}
 
export const formDataToJPO = (formData: FormData) => {
  if (formData instanceof FormData) {
    const entries = formData.entries();
 
    return Array.from(entries).reduce((res, [key, value]) => {
      return { ...res, [key]: value };
    }, {});
  }
 
  return formData;
};
 
type Uniqueness<T> = (a: T, b: T) => boolean;
 
export const unique = <T>(list: T[] | undefined, expression: Uniqueness<T>): T[] => {
  const comparator = expression ?? ((a, b) => a === b);
 
  return (list ?? []).reduce<T[]>((res, item) => {
    const index = res.findIndex((elem) => comparator(elem, item));
 
    if (index < 0) res.push(item);
 
    return res;
  }, []);
};
 
export const isDefined = <T>(value: T | undefined | null): value is T => {
  return value !== null && value !== undefined;
};
 
export const isEmptyString = (value: any) => {
  return typeof value === "string" && value.trim() === "";
};
 
export const objectClean = <T extends AnyObject>(source: T) => {
  const cleanObject: [keyof T, unknown][] = Object.entries(source).reduce<[keyof T, unknown][]>((res, [key, value]) => {
    const valueIsDefined = isDefined(value) && !isEmptyString(value);
 
    if (!valueIsDefined) {
      return res;
    }
 
    if (Object.prototype.toString.call(value) === "[object Object]") {
      return [...res, [key, objectClean(value as AnyObject)]];
    }
    return [...res, [key, value]];
  }, []);
 
  return Object.fromEntries(cleanObject) as T;
};
 
export const numberWithPrecision = (n: number, precision = 1, removeTrailinZero = false) => {
  if (typeof n !== "number" || isNaN(n)) return "";
 
  let finalNum = n.toFixed(precision);
 
  if (removeTrailinZero) {
    finalNum = finalNum.replace(/.(0+)$/, "");
  }
 
  return finalNum;
};
 
export const humanReadableNumber = (n: number) => {
  const abs = Math.abs(n);
 
  if (isNaN(abs) || n === null) return "—";
  const normalizeNumber = (n: number) => numberWithPrecision(n, 1, true);
 
  let result;
 
  if (abs < 1e3) {
    result = normalizeNumber(n);
  } else if (abs >= 1e3 && abs < 1e6) {
    result = `${normalizeNumber(n / 1e3)}K`;
  } else if (abs >= 1e6 && abs < 1e9) {
    result = `${normalizeNumber(n / 1e6)}M`;
  } else {
    result = `${normalizeNumber(n / 1e9)}B`;
  }
 
  return result || null;
};
 
export const absoluteURL = (path = "") => {
  if (path.match(/^https?/) || path.match(/^\/\//)) {
    return path;
  }
  return [APP_SETTINGS.hostname.replace(/([/]+)$/, ""), path.replace(/^([/]+)/, "")].join("/");
};
 
export const removePrefix = (path: string) => {
  if (APP_SETTINGS.hostname) {
    const hostname = APP_SETTINGS.hostname;
    const prefix = new URL(hostname).pathname.replace(/([/]+)$/, "");
 
    return path.replace(new RegExp(`^${prefix}`), "");
  }
 
  return path || "/";
};
 
export const copyText = (text: string) => {
  const input = document.createElement("textarea");
 
  input.style.position = "fixed"; // don't mess up with scroll
  document.body.appendChild(input);
 
  input.value = text;
  input.focus();
  input.select();
 
  document.execCommand("copy");
  input.remove();
};
 
export const delay = (time = 0) => {
  return new Promise((resolve) => setTimeout(resolve, time));
};
 
export const clamp = (value: number, min: number, max: number) => {
  return Math.max(min, Math.min(value, max));
};
 
export const getLastTraceback = (traceback: string): string => {
  const lines = traceback.split("\n");
  let lastTraceIndex = -1;
 
  for (let i = lines.length - 1; i >= 0; i--) {
    if (lines[i].startsWith("  File")) {
      lastTraceIndex = i;
      break;
    }
  }
 
  return lastTraceIndex >= 0 ? lines.slice(lastTraceIndex).join("\n") : traceback;
};