Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
23 changes: 23 additions & 0 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,26 @@ export function versionCompare (a: Version, b: Version): CompareResult {
return 0
}
// endregion version compare

export function * iteratorMap <I, R> (iter: Iterable<I>, func: (e: I) => R): Generator<R> {
for (const item of iter) {
yield func(item)
}
}

export function * iteratorFilter <I> (iter: Iterable<I>, func: (e: I) => boolean): Generator<I> {
for (const item of iter) {
if (func(item)) {
yield item
}
}
}

/* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
-- ack */
/* @ts-expect-error TS2550 -- Set.difference() exists in node since node2024 */
export const setDifference: <I>(s1: Set<I>, s2: Set<any>) => Set<I> = typeof Set.prototype.difference === 'function'
/* @ts-expect-error TS2550 */
? (s1, s2) => s1.difference(s2)
: (s1, s2) => new Set(iteratorFilter(s1, (i) => !s2.has(i)) )
/* eslint-enable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call */
Loading