-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(without): add without support to fromfrom
- Loading branch information
Showing
5 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ComparePredicate } from "../types"; | ||
import { createFilterIterable } from "./filter"; | ||
import { IterableCreatorIterable } from "../IterableCreatorIterable"; | ||
|
||
export const createWithoutIterable = <TItem>( | ||
iterator: Iterable<TItem>, | ||
withoutItems: Iterable<TItem>, | ||
comparePredicate?: ComparePredicate<TItem> | ||
): Iterable<TItem> => { | ||
if (!comparePredicate) { | ||
const withoutSet = new Set(withoutItems); | ||
// fast path, create a filter iterable with the `Set.prototype.has` function call | ||
return createFilterIterable(iterator, item => !withoutSet.has(item)); | ||
} else { | ||
// Must compare each item for equality for each item | ||
return new IterableCreatorIterable(function* filter(): IterableIterator< | ||
TItem | ||
> { | ||
// cache already found results | ||
const cache = new Set(); | ||
|
||
outer: for (const item of iterator) { | ||
// fast path, this item was already found, don't loop | ||
if (cache.has(item)) continue; | ||
// slow path, loop over each item in the set, determine if it matches the ComparePredicate | ||
for (const withoutItem of withoutItems) { | ||
// if the item is found, add it to the cache and skip the item | ||
if (comparePredicate(item, withoutItem)) { | ||
cache.add(item); | ||
continue outer; | ||
} | ||
} | ||
|
||
// we can safely yield the item | ||
yield item; | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters