chore(dev): getInputList helper func

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-04-12 16:39:34 +02:00
parent 14118c6569
commit d0d5d21abc
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
3 changed files with 119 additions and 1 deletions

79
__tests__/context.test.ts Normal file
View file

@ -0,0 +1,79 @@
import * as context from '../src/context';
describe('getInputList', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar']);
});
it('handles multiple lines correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});
it('remove empty lines correctly', async () => {
setInput('foo', 'bar\n\nbaz');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});
it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});
it('remove empty result correctly', async () => {
setInput('foo', 'bar,baz,');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines and comma correctly', async () => {
setInput('foo', 'bar\r\nbaz,bat');
const res = await context.getInputList('foo');
expect(res).toEqual(['bar', 'baz', 'bat']);
});
it('handles multiple lines and ignoring comma correctly', async () => {
setInput('driver-opts', 'foo,bar\nbaz');
const res = await context.getInputList('driver-opts', true);
expect(res).toEqual(['foo,bar', 'baz']);
});
it('handles different new lines and ignoring comma correctly', async () => {
setInput('driver-opts', 'foo,bar\r\nbaz');
const res = await context.getInputList('driver-opts', true);
expect(res).toEqual(['foo,bar', 'baz']);
});
});
describe('asyncForEach', () => {
it('executes async tasks sequentially', async () => {
const testValues = [1, 2, 3, 4, 5];
const results: number[] = [];
await context.asyncForEach(testValues, async value => {
results.push(value);
});
expect(results).toEqual(testValues);
});
});
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
}
function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}

21
dist/index.js generated vendored
View file

@ -36,7 +36,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getInputs = void 0;
exports.asyncForEach = exports.getInputList = exports.getInputs = void 0;
const core = __importStar(__webpack_require__(186));
function getInputs() {
return __awaiter(this, void 0, void 0, function* () {
@ -50,6 +50,25 @@ function getInputs() {
});
}
exports.getInputs = getInputs;
function getInputList(name, ignoreComma) {
return __awaiter(this, void 0, void 0, function* () {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []);
});
}
exports.getInputList = getInputList;
const asyncForEach = (array, callback) => __awaiter(void 0, void 0, void 0, function* () {
for (const index in array) {
yield callback(array[index], index, array);
}
});
exports.asyncForEach = asyncForEach;
//# sourceMappingURL=context.js.map
/***/ }),

View file

@ -17,3 +17,23 @@ export async function getInputs(): Promise<Inputs> {
cleanup: core.getBooleanInput('cleanup')
};
}
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.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) => {
for (const index in array) {
await callback(array[index], index, array);
}
};