Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple pMapSkip's #52

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export default async function pMap(
} else {
isResolved = true;

// Delete skipped index from back to front, to support multiple pMapSkips
// so use the unshift method when putting index into skippedIndexes.
// see line 93
for (const skippedIndex of skippedIndexes) {
result.splice(skippedIndex, 1);
}
Expand All @@ -87,7 +90,7 @@ export default async function pMap(
const value = await mapper(element, index);

if (value === pMapSkip) {
skippedIndexes.push(index);
skippedIndexes.unshift(index);
ferrinweb marked this conversation as resolved.
Show resolved Hide resolved
} else {
result[index] = value;
}
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ test('pMapSkip', async t => {
], async value => value), [1, 2]);
});

test('multiple pMapSkips', async t => {
ferrinweb marked this conversation as resolved.
Show resolved Hide resolved
t.deepEqual(await pMap([
1,
pMapSkip,
2,
pMapSkip,
3,
pMapSkip,
pMapSkip,
4
], async value => value), [1, 2, 3, 4]);
});

test('all mappers should run when concurrency is infinite, even after stop-on-error happened', async t => {
const input = [1, async () => delay(300, {value: 2}), 3];
const mappedValues = [];
Expand Down Expand Up @@ -269,6 +282,19 @@ test('asyncIterator - pMapSkip', async t => {
]), async value => value), [1, 2]);
});

test('asyncIterator - multiple pMapSkips', async t => {
t.deepEqual(await pMap(new AsyncTestData([
1,
pMapSkip,
2,
pMapSkip,
3,
pMapSkip,
pMapSkip,
4
]), async value => value), [1, 2, 3, 4]);
});

test('asyncIterator - all mappers should run when concurrency is infinite, even after stop-on-error happened', async t => {
const input = [1, async () => delay(300, {value: 2}), 3];
const mappedValues = [];
Expand Down