This is not GitHub.com

Like what you see? Install OctoLinker now!

Skip to content
Permalink
Browse files
Add another test
Fixes #57
  • Loading branch information
sindresorhus committed Mar 7, 2022
1 parent 3b62341 commit 735d80e928f083c9399e5f741eb1b3cabd0ac1ba
Showing with 32 additions and 28 deletions.
  1. +4 −4 index.js
  2. +2 −2 index.test-d.ts
  3. +3 −3 package.json
  4. +1 −1 test-multiple-pmapskips-performance.js
  5. +22 −18 test.js
@@ -5,10 +5,10 @@ export default async function pMap(
mapper,
{
concurrency = Number.POSITIVE_INFINITY,
stopOnError = true
} = {}
stopOnError = true,
} = {},
) {
return new Promise((resolve, reject_) => { // eslint-disable-line promise/param-names
return new Promise((resolve, reject_) => {
if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
}
@@ -64,7 +64,7 @@ export default async function pMap(

isResolved = true;

if (!skippedIndexesMap.size) {
if (skippedIndexesMap.size === 0) {
resolve(result);
return;
}
@@ -4,13 +4,13 @@ import pMap, {Options, Mapper, pMapSkip} from './index.js';
const sites = [
'https://sindresorhus.com',
'https://avajs.dev',
'https://github.com'
'https://github.com',
];

const numbers = [
0,
1,
2
2,
];

const asyncMapper = async (site: string): Promise<string> => site;
@@ -44,12 +44,12 @@
"aggregate-error": "^4.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"ava": "^4.1.0",
"delay": "^5.0.0",
"in-range": "^3.0.0",
"random-int": "^3.0.0",
"time-span": "^5.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"tsd": "^0.19.1",
"xo": "^0.48.0"
}
}
@@ -13,7 +13,7 @@ function generateSkipPerformanceData(length) {
}

test('multiple pMapSkips - algorithmic complexity', async t => {
const testData = [generateSkipPerformanceData(1000), generateSkipPerformanceData(10000), generateSkipPerformanceData(100000)];
const testData = [generateSkipPerformanceData(1000), generateSkipPerformanceData(10_000), generateSkipPerformanceData(100_000)];
const testDurationsMS = [];

for (const data of testData) {
40 test.js
@@ -9,7 +9,7 @@ import pMap, {pMapSkip} from './index.js';
const sharedInput = [
[async () => 10, 300],
[20, 200],
[30, 100]
[30, 100],
];

const errorInput1 = [
@@ -20,7 +20,7 @@ const errorInput1 = [
}, 10],
[() => {
throw new Error('bar');
}, 10]
}, 10],
];

const errorInput2 = [
@@ -31,7 +31,7 @@ const errorInput2 = [
[30, 100],
[() => {
throw new Error('foo');
}, 10]
}, 10],
];

const mapper = async ([value, ms]) => {
@@ -72,7 +72,7 @@ class ThrowingIterator {
// eslint is wrong - bind is needed else the next() call cannot update
// this.index, which we need to track how many times the iterator was called
// eslint-disable-next-line no-extra-bind
}).bind(this)
}).bind(this),
};
}
}
@@ -151,7 +151,7 @@ test('pMapSkip', async t => {
t.deepEqual(await pMap([
1,
pMapSkip,
2
2,
], async value => value), [1, 2]);
});

@@ -164,7 +164,7 @@ test('multiple pMapSkips', async t => {
3,
pMapSkip,
pMapSkip,
4
4,
], async value => value), [1, 2, 3, 4]);
});

@@ -173,7 +173,7 @@ test('all pMapSkips', async t => {
pMapSkip,
pMapSkip,
pMapSkip,
pMapSkip
pMapSkip,
], async value => value), []);
});

@@ -188,7 +188,7 @@ test('all mappers should run when concurrency is infinite, even after stop-on-er
await delay(100);
throw new Error('Oops!');
}
})
}),
);
await delay(500);
t.deepEqual(mappedValues, [1, 3, 2]);
@@ -287,7 +287,7 @@ test('asyncIterator - pMapSkip', async t => {
t.deepEqual(await pMap(new AsyncTestData([
1,
pMapSkip,
2
2,
]), async value => value), [1, 2]);
});

@@ -300,7 +300,7 @@ test('asyncIterator - multiple pMapSkips', async t => {
3,
pMapSkip,
pMapSkip,
4
4,
]), async value => value), [1, 2, 3, 4]);
});

@@ -309,7 +309,7 @@ test('asyncIterator - all pMapSkips', async t => {
pMapSkip,
pMapSkip,
pMapSkip,
pMapSkip
pMapSkip,
]), async value => value), []);
});

@@ -328,7 +328,7 @@ test('asyncIterator - all mappers should run when concurrency is infinite, even
throw new Error(`Oops! ${value}`);
}
}),
{message: 'Oops! 1'}
{message: 'Oops! 1'},
);
await delay(500);
t.deepEqual(mappedValues, [1, 3, 2]);
@@ -344,7 +344,7 @@ test('catches exception from source iterator - 1st item', async t => {
await delay(100);
return value;
},
{concurrency: 1, stopOnError: true}
{concurrency: 1, stopOnError: true},
));
t.is(error.message, 'throwing on index 0');
t.is(input.index, 1);
@@ -365,7 +365,7 @@ test('catches exception from source iterator - 2nd item', async t => {
await delay(100);
return value;
},
{concurrency: 1, stopOnError: true}
{concurrency: 1, stopOnError: true},
));
await delay(300);
t.is(input.index, 2);
@@ -384,7 +384,7 @@ test('catches exception from source iterator - 2nd item after 1st item mapper th
await delay(100);
throw new Error('mapper threw error');
},
{concurrency: 1, stopOnError: false}
{concurrency: 1, stopOnError: false},
));
await delay(300);
t.is(error.message, 'throwing on index 1');
@@ -414,7 +414,7 @@ test('asyncIterator - get the correct exception after stop-on-error', async t =>
test('incorrect input type', async t => {
let mapperCalled = false;

const task = pMap(123456, async () => {
const task = pMap(123_456, async () => {
mapperCalled = true;
await delay(100);
});
@@ -432,7 +432,7 @@ test('no unhandled rejected promises from mapper throws - infinite concurrency',
await delay(100);
throw new Error(`Oops! ${value}`);
}),
{message: 'Oops! 1'}
{message: 'Oops! 1'},
);
// Note: All 3 mappers get invoked, all 3 throw, even with `{stopOnError: true}` this
// should raise an AggregateError with all 3 exceptions instead of throwing 1
@@ -450,7 +450,11 @@ test('no unhandled rejected promises from mapper throws - concurrency 1', async
throw new Error(`Oops! ${value}`);
},
{concurrency: 1}),
{message: 'Oops! 1'}
{message: 'Oops! 1'},
);
t.deepEqual(mappedValues, [1]);
});

test('invalid mapper', async t => {
await t.throwsAsync(pMap([], 'invalid mapper', {concurrency: 2}), {instanceOf: TypeError});
});

0 comments on commit 735d80e

Please sign in to comment.