-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcondition.ts
More file actions
70 lines (61 loc) · 2.29 KB
/
condition.ts
File metadata and controls
70 lines (61 loc) · 2.29 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
import { judgeVirtualFast } from "@util/pattern"
import { formatTimeYMD } from "@util/time"
import type { StatCondition } from './types'
export type ProcessedCondition = StatCondition & {
useExactDate?: boolean
exactDateStr?: string
startDateStr?: string
endDateStr?: string
timeStart?: number
timeEnd?: number
focusStart?: number
focusEnd?: number
}
export function filterHost(host: string, keys: ProcessedCondition['keys'], virtual?: boolean): boolean {
if (!virtual && judgeVirtualFast(host)) return false
if (keys === undefined) return true
return typeof keys === 'string' ? host === keys : keys.includes(host)
}
export function filterDate(
date: string,
{ useExactDate, exactDateStr, startDateStr, endDateStr }: ProcessedCondition
): boolean {
if (useExactDate) {
if (exactDateStr !== date) return false
} else {
if (startDateStr && startDateStr > date) return false
if (endDateStr && endDateStr < date) return false
}
return true
}
export function filterNumberRange(val: number, [start, end]: [start?: number, end?: number]): boolean {
if (start !== null && start !== undefined && start > val) return false
if (end !== null && end !== undefined && end < val) return false
return true
}
export function processCondition(condition?: StatCondition): ProcessedCondition {
const result: ProcessedCondition = { ...condition }
const paramDate = condition?.date
if (paramDate) {
if (paramDate instanceof Date) {
result.useExactDate = true
result.exactDateStr = formatTimeYMD(paramDate)
} else {
const [startDate, endDate] = paramDate
result.useExactDate = false
startDate && (result.startDateStr = formatTimeYMD(startDate))
endDate && (result.endDateStr = formatTimeYMD(endDate))
}
}
const paramTime = condition?.timeRange
if (paramTime) {
paramTime.length >= 2 && (result.timeEnd = paramTime[1])
paramTime.length >= 1 && (result.timeStart = paramTime[0])
}
const paramFocus = condition?.focusRange
if (paramFocus) {
paramFocus.length >= 2 && (result.focusEnd = paramFocus[1])
paramFocus.length >= 1 && (result.focusStart = paramFocus[0])
}
return result
}