Bin
2025-12-17 2b99d77d73ba568beff0a549534017caaad8a6de
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
/**
 * @todo use [].every()
 * Returns true if all checks return true
 * @param {boolean[]} boolArray
 * @param {(any) => boolean} check
 */
export const all = <T>(boolArray: T[], check: (item: T) => boolean) => {
  return boolArray.reduce((res, value) => {
    return res && !!check(value);
  }, true);
};
 
/**
 * Returns true if any of the checks return true
 * @param {boolean[]} boolArray
 * @param {(any) => boolean} check
 */
export const any = <T>(boolArray: T[], check: (item: T) => boolean) => {
  return boolArray.find((value) => !!check(value)) || false;
};
 
export const randomDate = (start: Date, end: Date) => {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
};
 
export const groupBy = <T>(list: T[], group: (item: T) => string) => {
  return list.reduce<Record<string, T[]>>((res, item) => {
    const property = group(item);
 
    if (res[property]) {
      res[property].push(item);
    } else {
      res[property] = [item];
    }
 
    return res;
  }, {});
};
 
export const unique = <T>(list: T[]): T[] => {
  return Array.from(new Set<T>(list));
};
 
export const cleanArray = <T>(array: T[]): T[] => {
  return array.filter((el) => !!el);
};
 
export const isDefined = <T>(value?: T): value is T => {
  return value !== null && value !== undefined;
};
 
export const isBlank = (value?: string) => {
  if (!isDefined(value)) return true;
 
  if (typeof value === "string") {
    return value.trim().length === 0;
  }
 
  return false;
};