forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit.ts
More file actions
112 lines (97 loc) · 4.23 KB
/
Copy pathlimit.ts
File metadata and controls
112 lines (97 loc) · 4.23 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
import { EXCLUDING_PREFIX } from './constant/remain-host'
import { getWeekDay, MILL_PER_MINUTE, MILL_PER_SECOND } from "./time"
const GLOBSTAR_TOKEN = `__GLOBSTAR${Math.random().toString(36).slice(2, 6)}__`
const matchUrl = (cond: string, url: string): boolean => {
const pattern = cond
.replace(/\*\*/g, GLOBSTAR_TOKEN)
.split('*')
.join('.*')
.replaceAll(GLOBSTAR_TOKEN, '.+')
return new RegExp(`^.*//${pattern}`).test(url)
}
/**
* checks whether the provided URL matches the rule list (cond), following the exclusion rule priority
* @param cond
* @param url
*/
export function matches(cond: tt4b.limit.Item['cond'], url: string): boolean {
let hit = false
for (let i = cond.length - 1; i >= 0; i--) {
const rule = cond[i]
if (rule === undefined) continue
if (rule.startsWith(EXCLUDING_PREFIX)) {
if (matchUrl(rule.slice(1), url)) return false
} else {
hit = hit || matchUrl(rule, url)
}
}
return hit
}
/**
* determines which normal rules in the given list (cond) match the provided URL, strictly adhering to exclusion rule priority
* @param cond
* @param url
*/
export function matchCond(cond: tt4b.limit.Item['cond'], url: string): string[] {
const matchedNormalRules: string[] = []
for (let i = cond.length - 1; i >= 0; i--) {
const rule = cond[i]
if (rule === undefined) continue
if (rule.startsWith(EXCLUDING_PREFIX)) {
// Immediately return an empty array if an exclusion rule is hit
if (matchUrl(rule.slice(1), url)) return []
} else {
if (matchUrl(rule, url)) matchedNormalRules.push(rule)
}
}
return matchedNormalRules
}
export const meetLimit = (limit: number | undefined, value: number | undefined): boolean => {
return !!limit && !!value && value > limit
}
type LimitInfo = { wasted: number, maxLimit: number | undefined }
type DelayInfo = { count: number, duration: number, allow: boolean }
export const meetTimeLimit = (limit: LimitInfo, delay: DelayInfo) => {
const { wasted, maxLimit = 0 } = limit
const { count, duration, allow } = delay
const realLimit = allow ? maxLimit + duration * MILL_PER_MINUTE * (count ?? 0) : maxLimit
return meetLimit(realLimit, wasted)
}
export function hasDailyLimited(item: tt4b.limit.Item, delayDuration: number): boolean {
const { time, count, waste, visit, delayCount, allowDelay } = item
const delay = { count: delayCount, duration: delayDuration, allow: !!allowDelay }
const limit = { wasted: waste, maxLimit: (time ?? 0) * MILL_PER_SECOND }
return meetTimeLimit(limit, delay) || meetLimit(count, visit)
}
export function hasWeeklyLimited(item: tt4b.limit.Item, delayDuration: number): boolean {
const { weekly, weeklyCount, weeklyWaste, weeklyVisit, weeklyDelayCount, allowDelay } = item
const delay = { count: weeklyDelayCount, duration: delayDuration, allow: !!allowDelay }
const limit = { wasted: weeklyWaste, maxLimit: (weekly ?? 0) * MILL_PER_SECOND }
return meetTimeLimit(limit, delay) || meetLimit(weeklyCount, weeklyVisit)
}
export function hasLimited(item: tt4b.limit.Item, delayDuration: number): boolean {
return hasDailyLimited(item, delayDuration) || hasWeeklyLimited(item, delayDuration)
}
export function isEffective(weekdays: tt4b.limit.Rule['weekdays'], weekday?: number): boolean {
const weekdayLen = weekdays?.length
if (!weekdayLen || weekdayLen <= 0 || weekdayLen >= 7) return true
return weekdays.includes(weekday ?? getWeekDay(new Date()))
}
const idx2Str = (time: number | undefined): string => {
time = time ?? 0
const hour = Math.floor(time / 60)
const min = time - hour * 60
const hourStr = (hour < 10 ? "0" : "") + hour
const minStr = (min < 10 ? "0" : "") + min
return `${hourStr}:${minStr}`
}
export const date2Idx = (date: Date): number => date.getHours() * 60 * 60 + date.getMinutes() * 60 + date.getSeconds()
export const dateMinute2Idx = (date: Date): number => {
const hour = date.getHours()
const min = date.getMinutes()
return hour * 60 + min
}
export const period2Str = (p: tt4b.limit.Period | undefined): string => {
const [start, end] = p ?? []
return `${idx2Str(start)}-${idx2Str(end)}`
}