1
0
Fork 0

feat: add core.getStringAsArray to parse array-like inputs (separated by either commas or new lines)

pull/1926/head
Vladimir Starkov 2025-01-05 11:54:59 +01:00
parent adb9c4a7f4
commit f8ead03bf1
3 changed files with 33 additions and 0 deletions

View File

@ -24,6 +24,7 @@ Outputs can be set with `setOutput` which makes them available to be mapped into
const myInput = core.getInput('inputName', { required: true });
const myBooleanInput = core.getBooleanInput('booleanInputName', { required: true });
const myMultilineInput = core.getMultilineInput('multilineInputName', { required: true });
const myArrayInput = core.getStringAsArray('stringAsArray', { required: true });
core.setOutput('outputKey', 'outputVal');
```

View File

@ -294,6 +294,19 @@ describe('@actions/core', () => {
).toEqual([' val1 ', ' val2 ', ' '])
})
it('getStringAsArray; separated by either comma or new line', () => {
expect(
core.getStringAsArray(`
line 1,
line 2,
comma 1, comma 2,,
`, {
trimWhitespace: false
})
).toEqual(['line 1', 'line 2', 'comma 1', 'comma 2'])
})
it('legacy setOutput produces the correct command', () => {
core.setOutput('some output', 'some value')
assertWriteCalls([

View File

@ -160,6 +160,25 @@ export function getMultilineInput(
return inputs.map(input => input.trim())
}
/**
* Gets the values of an array-like input (separated by comma or new lines). Each value is also trimmed.
*
* @param name name of the input to get
* @param options optional. See InputOptions.
* @returns string[]
*
*/
export function getStringAsArray(
name: string,
options?: InputOptions
): string[] {
const inputs: string[] = getInput(name, options)
.split(/[\n,]+/)
.map(s => s.trim())
.filter(x => x !== '');
return inputs;
}
/**
* Gets the input value of the boolean type in the YAML 1.2 "core schema" specification.
* Support boolean input list: `true | True | TRUE | false | False | FALSE` .