-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.ts
More file actions
187 lines (166 loc) Β· 4.67 KB
/
Copy pathindex.ts
File metadata and controls
187 lines (166 loc) Β· 4.67 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
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
import { Locales } from '@/models/ThemeConfig.class'
export interface RecentComment {
id: number
body: string
node_id?: number
html_url: string
issue_url: string
created_at: string
updated_at: string
author_association: string
filtered?: boolean
user: {
id: number
login: string
avatar_url: string
html_url: string
}
is_admin: boolean
cache_flag?: boolean
}
/**
* Formatting ISO time into readable times.
*/
export function formatTime(
time: number | string,
options?: { template?: string; lang?: Locales }
): string {
const configs: { template: string; lang: Locales } = {
template: '[TIME]',
lang: 'en'
}
const languages: Record<Locales, { [type: string]: string }> = {
es: {
seconds: 'hace un momento',
minutes: ' minutos',
hours: ' horas',
days: ' dΓas',
months: ' meses',
years: ' aΓ±os'
},
en: {
seconds: 'just seconds ago',
minutes: ' minutes ago',
hours: ' hours ago',
days: ' days ago',
months: ' months ago',
years: ' years ago'
},
'zh-CN': {
seconds: 'εε',
minutes: ' ειε',
hours: ' ε°ζΆε',
days: ' 倩ε',
months: ' δΈͺζε',
years: ' εΉ΄ε'
},
'zh-TW': {
seconds: 'εε',
minutes: ' ειε',
hours: ' ε°ζε',
days: ' 倩ε',
months: ' εζε',
years: ' εΉ΄ε'
}
}
if (options !== undefined) {
if (options.template) configs.template = options.template
if (options.lang) configs.lang = options.lang
}
if (typeof time === 'string') {
if (/[a-zA-Z]+/g.test(time)) time = new Date(time).getTime()
else time = parseInt(time)
}
if (String('' + time).length === 10) {
time = parseInt(String(time)) * 1000
} else {
time = +time
}
const d = new Date(time).getTime()
const now = Date.now()
const diff = Math.floor((now - d) / 1000)
let formattedTime = ''
if (diff < 60) {
// Within 1 minute
formattedTime = languages[configs.lang].seconds
} else if (diff < 3600) {
// Within 1 hour
formattedTime =
String(Math.floor(diff / 60)) + languages[configs.lang].minutes
} else if (diff < 3600 * 24) {
// Within 1 day
formattedTime =
String(Math.floor(diff / 3600)) + languages[configs.lang].hours
} else if (diff < 3600 * 24 * 30) {
// Within 1 month
formattedTime =
String(Math.floor(diff / 3600 / 24)) + languages[configs.lang].days
} else if (diff < 3600 * 24 * 365) {
// Within 1 year
formattedTime =
String(Math.floor(diff / 3600 / 24 / 30)) + languages[configs.lang].months
} else {
formattedTime =
String(Math.floor(diff / 3600 / 24 / 365)) + languages[configs.lang].years
}
return configs.template.replace('[TIME]', formattedTime)
}
export function filterHTMLContent(content: string, length?: number): string {
if (!length) length = 28
content = content
// Replace all images
.replace(
/?://)+[\w.-]+(?:.[\w.-]+)+[\w\-._~:/?#[\]@!$&'*+,;=.]+\)/g,
'[img]'
)
// Replacing all links.
.replace(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)+/g,
'[link]'
)
// Removing all html tags
.replace(/( |<([^>]+)>)/gi, '')
if (content.length > length) {
// TODO: replace deprecated `.substr` function
content = content.substr(0, length)
content += '...'
}
return content
}
export function getDaysTillNow(from: string) {
const today = new Date()
const fromDate = new Date(from)
// To calculate the time difference of two dates
const timeDiff = today.getTime() - fromDate.getTime()
// To calculate the no. of days between two dates
return Math.floor(timeDiff / (1000 * 3600 * 24))
}
export function cleanPath(path: string) {
if (path !== '/' && path.at(-1) === '/') {
return path.slice(0, -1)
}
return path
}
export function shuffleArray<T = any>(array: T[]): T[] {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
return array
}
export function throttle(func: () => void, timeFrame: number) {
let time = Number(new Date())
return function () {
if (time + timeFrame - Date.now() < 0) {
func()
time = Date.now()
}
}
}
export function paginator<T>(data: T[], page: number, pageSize: number) {
const skip = pageSize * (page - 1)
// slice function's endIndex will not be included
// therefore the ending index should be pageSize not pageSize - 1
const endIndex = skip > data.length - 1 ? undefined : pageSize * page
return data.slice(skip, endIndex)
}