forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
57 lines (50 loc) · 1.47 KB
/
util.ts
File metadata and controls
57 lines (50 loc) · 1.47 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
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { t } from "@app/locale"
/**
* Transfer host info to label
*/
export function labelOfHostInfo(site: timer.site.SiteKey | undefined): string {
if (!site) return ''
const { host, type } = site
if (!host) return ''
let label = ''
type === 'merged' && (label = `[${t(msg => msg.analysis.common.merged)}]`)
type === 'virtual' && (label = `[${t(msg => msg.analysis.common.virtual)}]`)
return `${host}${label}`
}
export type RingValue = [
current?: number,
last?: number,
]
/**
* Compute ring text
*
* @param ring ring value
* @param formatter formatter
* @returns text or '-'
*/
export function computeRingText(ring: RingValue, formatter?: ValueFormatter): string {
if (!ring) {
return ''
}
const [current, last] = ring
if (current === undefined && last === undefined) {
// return undefined if both are undefined
return ''
}
const delta = (current || 0) - (last || 0)
let result = formatter ? formatter(delta) : delta?.toString()
delta >= 0 && (result = '+' + result)
return result
}
export type ValueFormatter = (val: number | undefined) => string
export const formatValue = (val: number | undefined, formatter?: ValueFormatter) => formatter ? formatter(val) : val?.toString() || '-'
export type DimensionEntry = {
date: string
value: number
}