-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathUtil.ts
More file actions
95 lines (84 loc) · 2.45 KB
/
Util.ts
File metadata and controls
95 lines (84 loc) · 2.45 KB
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
import customColors from '@/customColors'
export function uuidv4(): string {
// Use Web Crypto API if available
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
return crypto.randomUUID()
}
// Fallback for older browsers
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (crypto.getRandomValues(new Uint8Array(1))[0] * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
// Predefined color palette for consistent colors
const colorPalette: string[] = [
customColors.logo,
customColors.white,
customColors.black,
customColors.darkGray,
customColors.accentRed,
customColors.logo,
customColors.orange,
customColors.lightBlue,
]
export function getNewColor(index = 0): string {
// Use modulo to cycle through the palette
return colorPalette[index % colorPalette.length]
}
export const getClientScopeByInum = (str: string): string => {
const inum = str.split(',')[0]
const value = inum.split('=')[1]
return value
}
export function getYearMonth(date: Date): string {
return date.getFullYear() + getMonth(date)
}
export function getMonth(aDate: Date): string {
const value = String(aDate.getMonth() + 1)
if (value.length > 1) {
return value
} else {
return '0' + value
}
}
export function formatDate(date?: string): string {
if (!date) {
return '-'
}
if (date.length > 10) {
return date.substring(0, 10)
}
if (date.length == 10) {
return date
}
return '-'
}
export const trimObjectStrings = <T extends object>(obj: T): T => {
const source = obj as unknown as Record<string, unknown>
const trimmed: Record<string, unknown> = {}
for (const key in source) {
const value = source[key]
if (typeof value === 'string') {
trimmed[key] = value.trim()
} else if (value && typeof value === 'object' && !Array.isArray(value)) {
trimmed[key] = trimObjectStrings(value as Record<string, unknown>)
} else {
trimmed[key] = value
}
}
return trimmed as T
}
export const filterEmptyObjects = <T extends object>(items?: T[]): T[] => {
return (items || []).filter((item) => item && Object.keys(item).length !== 0)
}
export const mapPropertyToKeyValue = (prop: {
key?: string
value?: string
value1?: string
value2?: string
}): { key: string; value: string } => {
const key = (prop.key ?? prop.value1 ?? '').trim()
const value = (prop.value ?? prop.value2 ?? '').trim()
return { key, value }
}