Skip to content

Commit b33912b

Browse files
Core: Add trimWhitespace to getInput (#802)
* Add option to not trim whitespace from inputs * Fix typos * Add doc clarification * Rename options
1 parent cac7db2 commit b33912b

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

packages/core/__tests__/core.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const testEnvVars = {
2727
INPUT_BOOLEAN_INPUT_FALSE2: 'False',
2828
INPUT_BOOLEAN_INPUT_FALSE3: 'FALSE',
2929
INPUT_WRONG_BOOLEAN_INPUT: 'wrong',
30+
INPUT_WITH_TRAILING_WHITESPACE: ' some val ',
3031

3132
// Save inputs
3233
STATE_TEST_1: 'state_val',
@@ -165,6 +166,22 @@ describe('@actions/core', () => {
165166
)
166167
})
167168

169+
it('getInput trims whitespace by default', () => {
170+
expect(core.getInput('with trailing whitespace')).toBe('some val')
171+
})
172+
173+
it('getInput trims whitespace when option is explicitly true', () => {
174+
expect(
175+
core.getInput('with trailing whitespace', {trimWhitespace: true})
176+
).toBe('some val')
177+
})
178+
179+
it('getInput does not trim whitespace when option is false', () => {
180+
expect(
181+
core.getInput('with trailing whitespace', {trimWhitespace: false})
182+
).toBe(' some val ')
183+
})
184+
168185
it('getInput gets non-required boolean input', () => {
169186
expect(core.getBooleanInput('boolean input')).toBe(true)
170187
})

packages/core/src/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import * as path from 'path'
1111
export interface InputOptions {
1212
/** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */
1313
required?: boolean
14+
15+
/** Optional. Whether leading/trailing whitespace will be trimmed for the input. Defaults to true */
16+
trimWhitespace?: boolean
1417
}
1518

1619
/**
@@ -88,6 +91,10 @@ export function getInput(name: string, options?: InputOptions): string {
8891
throw new Error(`Input required and not supplied: ${name}`)
8992
}
9093

94+
if (options && options.trimWhitespace === false) {
95+
return val
96+
}
97+
9198
return val.trim()
9299
}
93100

0 commit comments

Comments
 (0)