Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```

Expand Down
11 changes: 11 additions & 0 deletions packages/core/__tests__/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ 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,,
`)
).toEqual(['line 1', 'line 2', 'comma 1', 'comma 2'])
})

it('legacy setOutput produces the correct command', () => {
core.setOutput('some output', 'some value')
assertWriteCalls([
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` .
Expand Down