mirror of https://github.com/actions/toolkit
feat: add 'core.getCommaSeparatedInput' method
This commit adds a `core.getCommaSeparatedInput` method that can be used to parse comma-separated lists (i.e. `[val1,val2,val3]` and `val1,val2,val3`).pull/1566/head
parent
fe3e7ce9a7
commit
195d0ef692
|
@ -35,6 +35,11 @@ const testEnvVars = {
|
||||||
INPUT_WITH_TRAILING_WHITESPACE: ' some val ',
|
INPUT_WITH_TRAILING_WHITESPACE: ' some val ',
|
||||||
INPUT_MY_INPUT_LIST: 'val1\nval2\nval3',
|
INPUT_MY_INPUT_LIST: 'val1\nval2\nval3',
|
||||||
INPUT_LIST_WITH_TRAILING_WHITESPACE: ' val1 \n val2 \n ',
|
INPUT_LIST_WITH_TRAILING_WHITESPACE: ' val1 \n val2 \n ',
|
||||||
|
INPUT_COMMA_SEPARATED_LIST: 'val1,val2,val3',
|
||||||
|
INPUT_COMMA_SEPARATED_LIST2: '[val1,val2,val3]',
|
||||||
|
INPUT_COMMA_SEPARATED_LIST_WITH_TRAILING_WHITESPACE: ' val1 , val2 , ',
|
||||||
|
INPUT_COMMA_SEPARATED_LIST_WITH_TRAILING_WHITESPACE2:
|
||||||
|
'[ val1 , val2 , ]',
|
||||||
|
|
||||||
// Save inputs
|
// Save inputs
|
||||||
STATE_TEST_1: 'state_val',
|
STATE_TEST_1: 'state_val',
|
||||||
|
@ -289,6 +294,66 @@ describe('@actions/core', () => {
|
||||||
).toEqual([' val1 ', ' val2 ', ' '])
|
).toEqual([' val1 ', ' val2 ', ' '])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('getCommaSeparatedInput works', () => {
|
||||||
|
expect(core.getCommaSeparatedInput('comma separated list')).toEqual([
|
||||||
|
'val1',
|
||||||
|
'val2',
|
||||||
|
'val3'
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(core.getCommaSeparatedInput('comma separated list2')).toEqual([
|
||||||
|
'val1',
|
||||||
|
'val2',
|
||||||
|
'val3'
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getCommaSeparatedInput trims whitespace by default', () => {
|
||||||
|
expect(
|
||||||
|
core.getCommaSeparatedInput(
|
||||||
|
'comma separated list with trailing whitespace'
|
||||||
|
)
|
||||||
|
).toEqual(['val1', 'val2'])
|
||||||
|
|
||||||
|
expect(
|
||||||
|
core.getCommaSeparatedInput(
|
||||||
|
'comma separated list with trailing whitespace2'
|
||||||
|
)
|
||||||
|
).toEqual(['val1', 'val2', ''])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getCommaSeparatedInput trims whitespace when option is explicitly true', () => {
|
||||||
|
expect(
|
||||||
|
core.getCommaSeparatedInput(
|
||||||
|
'comma separated list with trailing whitespace',
|
||||||
|
{trimWhitespace: true}
|
||||||
|
)
|
||||||
|
).toEqual(['val1', 'val2'])
|
||||||
|
|
||||||
|
expect(
|
||||||
|
core.getCommaSeparatedInput(
|
||||||
|
'comma separated list with trailing whitespace2',
|
||||||
|
{trimWhitespace: true}
|
||||||
|
)
|
||||||
|
).toEqual(['val1', 'val2', ''])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getCommaSeparatedInput does not trim whitespace when option is false', () => {
|
||||||
|
expect(
|
||||||
|
core.getCommaSeparatedInput(
|
||||||
|
'comma separated list with trailing whitespace',
|
||||||
|
{trimWhitespace: false}
|
||||||
|
)
|
||||||
|
).toEqual([' val1 ', ' val2 ', ' '])
|
||||||
|
|
||||||
|
expect(
|
||||||
|
core.getCommaSeparatedInput(
|
||||||
|
'comma separated list with trailing whitespace2',
|
||||||
|
{trimWhitespace: false}
|
||||||
|
)
|
||||||
|
).toEqual([' val1 ', ' val2 ', ' '])
|
||||||
|
})
|
||||||
|
|
||||||
it('legacy setOutput produces the correct command', () => {
|
it('legacy setOutput produces the correct command', () => {
|
||||||
core.setOutput('some output', 'some value')
|
core.setOutput('some output', 'some value')
|
||||||
assertWriteCalls([
|
assertWriteCalls([
|
||||||
|
|
|
@ -160,6 +160,29 @@ export function getMultilineInput(
|
||||||
return inputs.map(input => input.trim())
|
return inputs.map(input => input.trim())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the values of a comma separated input. Each value is also trimmed.
|
||||||
|
*
|
||||||
|
* @param name name of the input to get
|
||||||
|
* @param options optional. See InputOptions.
|
||||||
|
* @returns string[]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export function getCommaSeparatedInput(
|
||||||
|
name: string,
|
||||||
|
options?: InputOptions
|
||||||
|
): string[] {
|
||||||
|
const inputs: string[] = getInput(name, options)
|
||||||
|
.split(/[[\]\n,]+/)
|
||||||
|
.filter(x => x !== '')
|
||||||
|
|
||||||
|
if (options && options.trimWhitespace === false) {
|
||||||
|
return inputs
|
||||||
|
}
|
||||||
|
|
||||||
|
return inputs.map(input => input.trim())
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the input value of the boolean type in the YAML 1.2 "core schema" specification.
|
* 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` .
|
* Support boolean input list: `true | True | TRUE | false | False | FALSE` .
|
||||||
|
|
Loading…
Reference in New Issue