Repository navigation
Expand file tree
/
Copy pathActionUtils.ts
More file actions
47 lines (43 loc) · 1.37 KB
/
Copy pathActionUtils.ts
File metadata and controls
47 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
1
6AEE
0
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
import * as core from '@actions/core';
/**
* Safely parse a JSON input to a GitHub Action.
*
* @param name - The name of the input.
* @param options - Options to pass to core.getInput
* @param [defaultValue] - A default value to provide for the input.
* Not required if the {required: true} option is given in the second arg to this function.
*/
function getJSONInput(name: string, options: core.InputOptions, defaultValue?: unknown): unknown {
const input = core.getInput(name, options);
if (input) {
return JSON.parse(input);
}
return defaultValue;
}
/**
* Safely access a string input to a GitHub Action, or fall back on a default if the string is empty.
*/
function getStringInput(name: string, options: core.InputOptions, defaultValue?: string): string | undefined {
const input = core.getInput(name, options);
if (!input) {
return defaultValue;
}
return input;
}
/**
* Converts a value to a number, returning 0 for non-numeric values.
*/
function convertToNumber(value: unknown): number {
switch (typeof value) {
case 'number':
return value;
case 'string':
if (!Number.isNaN(Number(value))) {
return Number(value);
}
return 0;
default:
return 0;
}
}
export {getJSONInput, getStringInput, convertToNumber};