Skip to content

Commit

Permalink
Ensure concurrency is an integer
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
sindresorhus committed Mar 5, 2020
1 parent bf03769 commit b342717
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Expand Up @@ -3,6 +3,8 @@ declare namespace pMap {
/**
Number of concurrently pending promises returned by `mapper`.
Must be an integer from 1 and up or `Infinity`.
@default Infinity
*/
readonly concurrency?: number;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -14,8 +14,8 @@ module.exports = async (
throw new TypeError('Mapper function is required');
}

if (!(typeof concurrency === 'number' && concurrency >= 1)) {
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`);
if (!((Number.isSafeInteger(concurrency) || concurrency === Infinity) && concurrency >= 1)) {
throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
}

const result = [];
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -59,7 +59,7 @@ Type: `object`

##### concurrency

Type: `number`\
Type: `number` (Integer)\
Default: `Infinity`\
Minimum: `1`

Expand Down
1 change: 1 addition & 0 deletions test.js
Expand Up @@ -91,6 +91,7 @@ test('async with concurrency: 2 (out of order time sequence)', async t => {

test('enforce number in options.concurrency', async t => {
await t.throwsAsync(pMap([], () => {}, {concurrency: 0}), TypeError);
await t.throwsAsync(pMap([], () => {}, {concurrency: 1.5}), TypeError);
await t.notThrowsAsync(pMap([], () => {}, {concurrency: 1}));
await t.notThrowsAsync(pMap([], () => {}, {concurrency: 10}));
await t.notThrowsAsync(pMap([], () => {}, {concurrency: Infinity}));
Expand Down

0 comments on commit b342717

Please sign in to comment.