File size: 1,367 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { camelCase, titleCase } from "scule"

export function stringify(obj: any) {
  return JSON.stringify(filterEmpty(obj), null, 2)
}

export function filterEmpty(obj: any) {
  return Object.fromEntries(
    Object.entries(obj).filter(([_, value]) => {
      if (value === null) return false
      if (typeof value === "object") return Object.keys(value).length > 0
      return true
    }),
  )
}

export const toComponentCase = (part: string) => {
  return titleCase(camelCase(part, { normalize: true }))
    .split(" ")
    .join("")
}

export const isEmptyObject = (obj: any) => {
  return Object.keys(obj).length === 0
}

export function mapEntries<A, B, K extends string | number | symbol>(
  obj: { [key in K]: A },
  f: (key: K, val: A) => [K, B],
): { [key in K]: B } {
  const result: { [key in K]: B } = {} as any
  for (const key in obj) {
    const kv = f(key, obj[key])
    result[kv[0]] = kv[1]
  }
  return result
}

export const uniq = <T>(...items: T[]) => {
  const _items = items.filter(Boolean)
  return Array.from(new Set(_items))
}

export const chartComponents = [
  "area-chart",
  "bar-chart",
  "bar-list",
  "bar-segment",
  "donut-chart",
  "line-chart",
  "pie-chart",
  "radar-chart",
  "radial-chart",
  "scatter-chart",
  "sparkline",
]

export const isChartComponent = (component: string) =>
  chartComponents.includes(component)