generated from YashTotale/boilerplate-react-with-redux-and-firebase
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfuncs.ts
122 lines (94 loc) · 2.73 KB
/
funcs.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
// External Imports
import moment from "moment";
// Internal Imports
import { EnqueueSnackbar } from "../Hooks/useClosableSnackbar";
export const chunk = <T extends unknown>(arr: T[], size: number): T[][] => {
const newArr = [];
for (let i = 0; i < arr.length; i += size) {
newArr.push(arr.slice(i, i + size));
}
return newArr;
};
interface DateObject {
start: string;
end?: string;
}
export const sortByDate = (
a: DateObject,
b: DateObject,
multiplier: 1 | -1 = 1
): number => {
const format = "MMMM YYYY";
if (!a.end && b.end) {
return -1 * multiplier;
}
if (a.end && !b.end) {
return 1 * multiplier;
}
if (a.end && b.end) {
const compare = compareDates(a.end, b.end, format, multiplier);
if (compare !== 0) return compare;
}
return compareDates(a.start, b.start, format, multiplier);
};
export const compareDates = (
a: string,
b: string,
format: string | string[],
multiplier: 1 | -1 = 1
): number => {
const first = moment(a, format);
const second = moment(b, format);
const areEqual = first.isSame(second);
if (areEqual) return 0;
const isBefore = first.isBefore(second);
return (isBefore ? 1 : -1) * multiplier;
};
type Sorter<T> = (a: T, b: T) => number;
export const createSorter = <K extends string, T>(
sortFuncs: Record<K, Sorter<T>>,
all: T[]
): ((sort: K) => T[]) => {
// Initialize cache with null values
const sortCache = Object.keys(sortFuncs).reduce(
(clone, key) => ({ [key]: null, ...clone }),
{} as Record<K, null | T[]>
);
return (sort: K): T[] => {
if (sortCache[sort]) return sortCache[sort] as T[];
const sorted = sortFuncs[sort] ? [...all].sort(sortFuncs[sort]) : all;
sortCache[sort] = sorted;
return sorted;
};
};
type Resolver<T> = (id: string, isSlug?: boolean) => T | null;
export const createResolver = <T>(resolveFunc: Resolver<T>): Resolver<T> => {
const resolveCache: Record<string, T | null> = {};
return (id: string, isSlug = false) => {
if (resolveCache[id]) return resolveCache[id];
const resolved = resolveFunc(id, isSlug);
resolveCache[id] = resolved;
return resolved;
};
};
const toAppend = " — Yash Totale";
export const generatePageTitle = (t: string): string => `${t}${toAppend}`;
export const getPageTitle = (rawTitle: string): string => {
return rawTitle.replace(toAppend, "");
};
export const scrollToTop = (behavior: ScrollBehavior = "smooth"): void => {
window.scrollTo({
top: 0,
left: 0,
behavior,
});
};
export const enqueueError = (
e: Error | string,
enqueueSnackbar: EnqueueSnackbar
): void => {
const message = typeof e === "string" ? e : e.message;
enqueueSnackbar(message || "An error occurred. Please try again.", {
variant: "error",
});
};