forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.ts
More file actions
89 lines (72 loc) · 2.77 KB
/
Copy pathstat.ts
File metadata and controls
89 lines (72 loc) · 2.77 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
import { toMap } from './array'
import { identifySiteKey } from "./site"
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
export function isNotZeroResult(target: tt4b.core.Result): boolean {
return !!target.focus || !!target.time
}
export function resultOf(focus: number, time: number): tt4b.core.Result {
return { focus, time }
}
export const ALL_DIMENSIONS: tt4b.core.Dimension[] = ['focus', 'time']
export function identifyTargetKey(targetKey: tt4b.stat.TargetKey): string {
if ('cateKey' in targetKey) {
return `cate_${targetKey.cateKey}`
} else if ('siteKey' in targetKey) {
return identifySiteKey(targetKey.siteKey)
} else {
return `group_${targetKey.groupKey}`
}
}
export function identifyStatKey(rowKey: tt4b.stat.StatKey) {
const { date } = rowKey || {}
return [identifyTargetKey(rowKey), date ?? ''].join('_')
}
export const isNormalSite = (row: tt4b.stat.Row): row is tt4b.stat.SiteRow => {
return 'siteKey' in row && row.siteKey.type === 'normal'
}
export const isGroup = (row: tt4b.stat.StatKey): row is tt4b.stat.GroupRow => {
return 'groupKey' in row
}
export const isSite = (row: tt4b.stat.StatKey): row is tt4b.stat.SiteRow => {
return 'siteKey' in row
}
export const isCate = (row: tt4b.stat.StatKey): row is tt4b.stat.CateRow => {
return 'cateKey' in row
}
export const getHost = (row: tt4b.stat.Row): string | undefined => {
return 'siteKey' in row ? row.siteKey.host : undefined
}
export const getAlias = (row: tt4b.stat.Row): string | undefined => {
return 'alias' in row ? row.alias : undefined
}
export const getIconUrl = (row: tt4b.stat.Row): string | undefined => {
return 'iconUrl' in row ? row.iconUrl : undefined
}
export const getRelatedCateId = (row: tt4b.stat.Row): number | undefined => {
if ('cateId' in row) return row.cateId
if ('cateKey' in row) return row.cateKey
return undefined
}
export const getComposition = (row: tt4b.stat.Row, dimension: tt4b.core.Dimension): tt4b.stat.RemoteCompositionVal[] => {
return 'composition' in row ? row.composition?.[dimension] ?? [] : []
}
export const getGroupName = (groupMap: Record<string, chrome.tabGroups.TabGroup>, row: tt4b.stat.GroupRow): string => {
const { groupKey } = row
const title = groupMap[groupKey]?.title
return title ?? `ID: ${groupKey}`
}
export const mergeWith = async <T extends tt4b.core.RowKey,>(
original: T[], exist: tt4b.core.Row[],
mergeFunc: (o: T, e: tt4b.core.Row | undefined) => Awaitable<void>,
) => {
const keyOf = ({ date, host }: tt4b.core.RowKey) => `${date}${host}`
const existMap = toMap(exist, keyOf)
for (const o of original) {
await mergeFunc(o, existMap[keyOf(o)])
}
}