-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstring-to-array.js
More file actions
35 lines (31 loc) · 1.06 KB
/
string-to-array.js
File metadata and controls
35 lines (31 loc) · 1.06 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
import * as core from '@actions/core'
// Helper function to convert a String to an Array specifically in Actions
// :param string: A comma seperated string to convert to an array
// :return Array: The function returns an Array - can be empty
export async function stringToArray(string) {
try {
// If the String is empty, return an empty Array
if (string.trim() === '') {
core.debug(
'in stringToArray(), an empty String was found so an empty Array was returned'
)
return []
}
// Split up the String on commas, trim each element, and return the Array
const stringArray = string.split(',').map(target => target.trim())
var results = []
// filter out empty items
for (const item of stringArray) {
if (item === '') {
continue
}
results.push(item)
}
return results
} catch (error) {
/* istanbul ignore next */
core.error(`failed string for debugging purposes: ${string}`)
/* istanbul ignore next */
throw new Error(`could not convert String to Array - error: ${error}`)
}
}