Skip to content

Commit

Permalink
fix: Analyze commits in batches of 2000 (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemelipa authored Nov 8, 2023
1 parent 4226521 commit a028765
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/nx-semantic-release/src/utils/promise-filter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export const promiseFilter = <T>(
export const promiseFilter = async <T>(
array: T[],
predicate: (item: T) => Promise<boolean>
): Promise<T[]> =>
Promise.all(
array.map((item) =>
predicate(item).then((result) => (result ? item : null))
)
).then((results) => results.filter(Boolean)) as Promise<T[]>;
): Promise<T[]> => {
const chunkSize = 2000;
const chunks = Array(Math.ceil(array.length / chunkSize))
.fill(null)
.map((_, index) => array.slice(index * chunkSize, (index + 1) * chunkSize));

const results = [];
for (const chunk of chunks) {
const chunkResults = await Promise.all(
chunk.map((item) => predicate(item).then((result) => ({ item, result })))
);
results.push(
...chunkResults.filter(({ result }) => result).map(({ item }) => item)
);
}

return results;
};

0 comments on commit a028765

Please sign in to comment.