Skip to content

Commit ea5e6b5

Browse files
authored
Merge pull request #534 from crazy-max/util-input-number
util: getInputNumber func
2 parents ca80942 + e6e545e commit ea5e6b5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

__tests__/util.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ ccccccccc`,
201201
});
202202
});
203203

204+
describe('getInputNumber', () => {
205+
it('should return a number when input is a valid number string', () => {
206+
setInput('foo', '42');
207+
const result = Util.getInputNumber('foo');
208+
expect(result).toBe(42);
209+
});
210+
211+
it('should return undefined when input is an empty string', () => {
212+
setInput('foo', '');
213+
const result = Util.getInputNumber('foo');
214+
expect(result).toBeUndefined();
215+
});
216+
217+
it('should return undefined when input is not provided', () => {
218+
const result = Util.getInputNumber('foo');
219+
expect(result).toBeUndefined();
220+
});
221+
222+
it('should return NaN when input is not a valid number', () => {
223+
setInput('foo', 'invalid');
224+
const result = Util.getInputNumber('foo');
225+
expect(result).toBeNaN();
226+
});
227+
});
228+
204229
describe('asyncForEach', () => {
205230
it('executes async tasks sequentially', async () => {
206231
const testValues = [1, 2, 3, 4, 5];

src/util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ export class Util {
6464
return res.filter(item => item).map(pat => pat.trim());
6565
}
6666

67+
public static getInputNumber(name: string): number | undefined {
68+
const value = core.getInput(name);
69+
if (!value) {
70+
return undefined;
71+
}
72+
return parseInt(value);
73+
}
74+
6775
public static async asyncForEach(array, callback) {
6876
for (let index = 0; index < array.length; index++) {
6977
await callback(array[index], index, array);

0 commit comments

Comments
 (0)