diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index ccd083f5..76d6beda 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -201,6 +201,31 @@ ccccccccc`, }); }); +describe('getInputNumber', () => { + it('should return a number when input is a valid number string', () => { + setInput('foo', '42'); + const result = Util.getInputNumber('foo'); + expect(result).toBe(42); + }); + + it('should return undefined when input is an empty string', () => { + setInput('foo', ''); + const result = Util.getInputNumber('foo'); + expect(result).toBeUndefined(); + }); + + it('should return undefined when input is not provided', () => { + const result = Util.getInputNumber('foo'); + expect(result).toBeUndefined(); + }); + + it('should return NaN when input is not a valid number', () => { + setInput('foo', 'invalid'); + const result = Util.getInputNumber('foo'); + expect(result).toBeNaN(); + }); +}); + describe('asyncForEach', () => { it('executes async tasks sequentially', async () => { const testValues = [1, 2, 3, 4, 5]; diff --git a/src/util.ts b/src/util.ts index 2587acac..0b785754 100644 --- a/src/util.ts +++ b/src/util.ts @@ -64,6 +64,14 @@ export class Util { return res.filter(item => item).map(pat => pat.trim()); } + public static getInputNumber(name: string): number | undefined { + const value = core.getInput(name); + if (!value) { + return undefined; + } + return parseInt(value); + } + public static async asyncForEach(array, callback) { for (let index = 0; index < array.length; index++) { await callback(array[index], index, array);