Exclude empty items from input list

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-09-12 23:34:53 +02:00
parent 991a380ca3
commit 67730cc1c5
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 21 additions and 2 deletions

View File

@ -15,6 +15,13 @@ describe('getInputList', () => {
expect(res).toEqual(['bar', 'baz']);
});
it('remove empty lines correctly', async () => {
setInput('foo', 'bar\n\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList('foo');
@ -22,6 +29,13 @@ describe('getInputList', () => {
expect(res).toEqual(['bar', 'baz']);
});
it('remove empty result correctly', async () => {
setInput('foo', 'bar,baz,');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList('foo');

3
dist/index.js generated vendored
View File

@ -6594,7 +6594,8 @@ function getInputList(name, ignoreComma) {
}
return items
.split(/\r?\n/)
.reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',') : line).map(pat => pat.trim()), []);
.filter(x => x)
.reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []);
});
}
exports.getInputList = getInputList;

View File

@ -35,7 +35,11 @@ export async function getInputList(name: string, ignoreComma?: boolean): Promise
}
return items
.split(/\r?\n/)
.reduce<string[]>((acc, line) => acc.concat(!ignoreComma ? line.split(',') : line).map(pat => pat.trim()), []);
.filter(x => x)
.reduce<string[]>(
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()),
[]
);
}
export const asyncForEach = async (array, callback) => {