generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessThreshold.ts
29 lines (26 loc) · 913 Bytes
/
processThreshold.ts
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
export function processThreshold(thresholdAsString: string): {
thresholdAsAbsolute: number | undefined
thresholdAsFraction: number | undefined
} {
let thresholdAsAbsolute = undefined
let thresholdAsFraction = undefined
const valueError = `The threshold must be a positive number, but ${thresholdAsString} was provided.`
if (thresholdAsString.endsWith('%')) {
thresholdAsFraction = parseFloat(thresholdAsString.slice(0, -1)) / 100
if (thresholdAsFraction <= 0) {
throw new Error(valueError)
}
} else {
const threshold: number = parseFloat(thresholdAsString) || 0
// if threshold is smaller than 1, interpret it as a fraction
if (threshold < 1) {
thresholdAsFraction = threshold
} else {
thresholdAsAbsolute = threshold
}
if (threshold <= 0) {
throw new Error(valueError)
}
}
return { thresholdAsAbsolute, thresholdAsFraction }
}