Skip to content

Commit

Permalink
Merge pull request #534 from crazy-max/util-input-number
Browse files Browse the repository at this point in the history
util: getInputNumber func
  • Loading branch information
crazy-max authored Dec 13, 2024
2 parents ca80942 + e6e545e commit ea5e6b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ea5e6b5

Please sign in to comment.