forked from adrianhajdin/banking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
211 lines (175 loc) · 5.82 KB
/
utils.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* eslint-disable no-prototype-builtins */
import { type ClassValue, clsx } from "clsx";
import qs from "query-string";
import { twMerge } from "tailwind-merge";
import { z } from "zod";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
// FORMAT DATE TIME
export const formatDateTime = (dateString: Date) => {
const dateTimeOptions: Intl.DateTimeFormatOptions = {
weekday: "short", // abbreviated weekday name (e.g., 'Mon')
month: "short", // abbreviated month name (e.g., 'Oct')
day: "numeric", // numeric day of the month (e.g., '25')
hour: "numeric", // numeric hour (e.g., '8')
minute: "numeric", // numeric minute (e.g., '30')
hour12: true, // use 12-hour clock (true) or 24-hour clock (false)
};
const dateDayOptions: Intl.DateTimeFormatOptions = {
weekday: "short", // abbreviated weekday name (e.g., 'Mon')
year: "numeric", // numeric year (e.g., '2023')
month: "2-digit", // abbreviated month name (e.g., 'Oct')
day: "2-digit", // numeric day of the month (e.g., '25')
};
const dateOptions: Intl.DateTimeFormatOptions = {
month: "short", // abbreviated month name (e.g., 'Oct')
year: "numeric", // numeric year (e.g., '2023')
day: "numeric", // numeric day of the month (e.g., '25')
};
const timeOptions: Intl.DateTimeFormatOptions = {
hour: "numeric", // numeric hour (e.g., '8')
minute: "numeric", // numeric minute (e.g., '30')
hour12: true, // use 12-hour clock (true) or 24-hour clock (false)
};
const formattedDateTime: string = new Date(dateString).toLocaleString(
"en-US",
dateTimeOptions
);
const formattedDateDay: string = new Date(dateString).toLocaleString(
"en-US",
dateDayOptions
);
const formattedDate: string = new Date(dateString).toLocaleString(
"en-US",
dateOptions
);
const formattedTime: string = new Date(dateString).toLocaleString(
"en-US",
timeOptions
);
return {
dateTime: formattedDateTime,
dateDay: formattedDateDay,
dateOnly: formattedDate,
timeOnly: formattedTime,
};
};
export function formatAmount(amount: number): string {
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
});
return formatter.format(amount);
}
export const parseStringify = (value: any) => JSON.parse(JSON.stringify(value));
export const removeSpecialCharacters = (value: string) => {
return value.replace(/[^\w\s]/gi, "");
};
interface UrlQueryParams {
params: string;
key: string;
value: string;
}
export function formUrlQuery({ params, key, value }: UrlQueryParams) {
const currentUrl = qs.parse(params);
currentUrl[key] = value;
return qs.stringifyUrl(
{
url: window.location.pathname,
query: currentUrl,
},
{ skipNull: true }
);
}
export function getAccountTypeColors(type: AccountTypes) {
switch (type) {
case "depository":
return {
bg: "bg-blue-25",
lightBg: "bg-blue-100",
title: "text-blue-900",
subText: "text-blue-700",
};
case "credit":
return {
bg: "bg-success-25",
lightBg: "bg-success-100",
title: "text-success-900",
subText: "text-success-700",
};
default:
return {
bg: "bg-green-25",
lightBg: "bg-green-100",
title: "text-green-900",
subText: "text-green-700",
};
}
}
export function countTransactionCategories(
transactions: Transaction[]
): CategoryCount[] {
const categoryCounts: { [category: string]: number } = {};
let totalCount = 0;
// Iterate over each transaction
transactions &&
transactions.forEach((transaction) => {
// Extract the category from the transaction
const category = transaction.category;
// If the category exists in the categoryCounts object, increment its count
if (categoryCounts.hasOwnProperty(category)) {
categoryCounts[category]++;
} else {
// Otherwise, initialize the count to 1
categoryCounts[category] = 1;
}
// Increment total count
totalCount++;
});
// Convert the categoryCounts object to an array of objects
const aggregatedCategories: CategoryCount[] = Object.keys(categoryCounts).map(
(category) => ({
name: category,
count: categoryCounts[category],
totalCount,
})
);
// Sort the aggregatedCategories array by count in descending order
aggregatedCategories.sort((a, b) => b.count - a.count);
return aggregatedCategories;
}
export function extractCustomerIdFromUrl(url: string) {
// Split the URL string by '/'
const parts = url.split("/");
// Extract the last part, which represents the customer ID
const customerId = parts[parts.length - 1];
return customerId;
}
export function encryptId(id: string) {
return btoa(id);
}
export function decryptId(id: string) {
return atob(id);
}
export const getTransactionStatus = (date: Date) => {
const today = new Date();
const twoDaysAgo = new Date(today);
twoDaysAgo.setDate(today.getDate() - 2);
return date > twoDaysAgo ? "Processing" : "Success";
};
export const authFormSchema = (type: string) => z.object({
// sign up
firstName: type === 'sign-in' ? z.string().optional() : z.string().min(3),
lastName: type === 'sign-in' ? z.string().optional() : z.string().min(3),
address1: type === 'sign-in' ? z.string().optional() : z.string().max(50),
city: type === 'sign-in' ? z.string().optional() : z.string().max(50),
state: type === 'sign-in' ? z.string().optional() : z.string().min(2).max(2),
postalCode: type === 'sign-in' ? z.string().optional() : z.string().min(3).max(6),
dateOfBirth: type === 'sign-in' ? z.string().optional() : z.string().min(3),
ssn: type === 'sign-in' ? z.string().optional() : z.string().min(3),
// both
email: z.string().email(),
password: z.string().min(8),
})