Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): switch from arrays to node iterators (#31718)
- Loading branch information
Showing
11 changed files
with
223 additions
and
100 deletions.
There are no files selected for viewing
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,59 @@ | ||
import { IGatsbyIterable } from "../types" | ||
|
||
export class GatsbyIterable<T> implements IGatsbyIterable<T> { | ||
constructor(private source: Iterator<T>) {} | ||
|
||
[Symbol.iterator](): Iterator<T> { | ||
return this.source | ||
} | ||
|
||
concat<U>(other: Iterable<U>): GatsbyIterable<T | U> { | ||
return new GatsbyIterable(concatSequence(this, other)) | ||
} | ||
|
||
map<U>(fn: (entry: T) => U): GatsbyIterable<U> { | ||
return new GatsbyIterable(mapSequence(this, fn)) | ||
} | ||
|
||
filter(predicate: (entry: T) => unknown): GatsbyIterable<T> { | ||
return new GatsbyIterable<T>(filterSequence(this, predicate)) | ||
} | ||
|
||
forEach(callback: (entry: T) => unknown): void { | ||
for (const value of this) { | ||
callback(value) | ||
} | ||
} | ||
} | ||
|
||
function* mapSequence<T, U>( | ||
source: Iterable<T>, | ||
fn: (arg: T) => U | ||
): Iterator<U> { | ||
for (const value of source) { | ||
yield fn(value) | ||
} | ||
} | ||
|
||
function* filterSequence<T>( | ||
source: Iterable<T>, | ||
predicate: (arg: T) => unknown | ||
): Iterator<T> { | ||
for (const value of source) { | ||
if (predicate(value)) { | ||
yield value | ||
} | ||
} | ||
} | ||
|
||
function* concatSequence<T, U = T>( | ||
first: Iterable<T>, | ||
second: Iterable<U> | ||
): Iterator<U | T> { | ||
for (const value of first) { | ||
yield value | ||
} | ||
for (const value of second) { | ||
yield value | ||
} | ||
} |
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
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
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
Oops, something went wrong.