From bb2f39337d45ba2945481b091b00cdb36a1c14af Mon Sep 17 00:00:00 2001 From: Thomas Boop <52323235+thboop@users.noreply.github.com> Date: Fri, 4 Jun 2021 09:28:49 -0400 Subject: [PATCH] Sarpik/get input list support (#829) * feat(core): Create `getInputList` utility Signed-off-by: Kipras Melnikovas * chore(core): Document usage of '\n' instead of [] @ `getInputList` Signed-off-by: Kipras Melnikovas * test(core): Create a very simple test for `getInputList` Signed-off-by: Kipras Melnikovas * run linter * update commands/readme Co-authored-by: Kipras Melnikovas --- packages/core/README.md | 1 + packages/core/__tests__/core.test.ts | 10 ++++++++++ packages/core/src/core.ts | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/packages/core/README.md b/packages/core/README.md index cbb9cfcc..56ee5f1e 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -23,6 +23,7 @@ Outputs can be set with `setOutput` which makes them available to be mapped into ```js const myInput = core.getInput('inputName', { required: true }); const myBooleanInput = core.getBooleanInput('booleanInputName', { required: true }); +const myMultilineInput = core.getMultiline('multilineInputName', { required: true }); core.setOutput('outputKey', 'outputVal'); ``` diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index dee2c78d..5be8df4e 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -29,6 +29,8 @@ const testEnvVars = { INPUT_WRONG_BOOLEAN_INPUT: 'wrong', INPUT_WITH_TRAILING_WHITESPACE: ' some val ', + INPUT_MY_INPUT_LIST: 'val1\nval2\nval3', + // Save inputs STATE_TEST_1: 'state_val', @@ -166,6 +168,14 @@ describe('@actions/core', () => { ) }) + it('getMultilineInput works', () => { + expect(core.getMultilineInput('my input list')).toEqual([ + 'val1', + 'val2', + 'val3' + ]) + }) + it('getInput trims whitespace by default', () => { expect(core.getInput('with trailing whitespace')).toBe('some val') }) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 2a8b8fd4..c5c36fa4 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -100,6 +100,25 @@ export function getInput(name: string, options?: InputOptions): string { return val.trim() } +/** + * Gets the values of an multiline input. Each value is also trimmed. + * + * @param name name of the input to get + * @param options optional. See InputOptions. + * @returns string[] + * + */ +export function getMultilineInput( + name: string, + options?: InputOptions +): string[] { + const inputs: string[] = getInput(name, options) + .split('\n') + .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` .