-
Notifications
You must be signed in to change notification settings - Fork 13
Add an Iterator.from TypeScript goody
#417
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fa05672
wip(iterator): added Iterator.from
elimanzo 8916c29
feat(iterator from): Adding returns for Iterator and object with rand…
elimanzo 9ee9caa
wip(iterator from test): Added jest test to Iterator.from (snapshots …
elimanzo 14496cb
chore(pr): Applying 1st round of PR feedback
elimanzo 8be1dab
wip: PR feedback 2
elimanzo 4de0c46
chore: added chained iterator method test
elimanzo 3c39749
fix: fixing snapshots
elimanzo d38f8c0
wip(iterator.from): Attempting to fix Interator.from to work
elimanzo 7686304
chore(snapshots): fixing snapshots
elimanzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
This file contains hidden or 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,98 @@ | ||
| import { describe, expect, it } from "@jest/globals"; | ||
|
|
||
| import "../Iterator.prototype.map"; | ||
| import "../Iterator.prototype.filter"; | ||
|
|
||
| (globalThis as Record<string, unknown>).Iterator && | ||
| delete ( | ||
| (globalThis as Record<string, unknown>).Iterator as Record<string, unknown> | ||
| ).from; | ||
|
|
||
| // eslint-disable-next-line import-x/first | ||
| import "./index"; | ||
|
|
||
| describe("Iterator.from", () => { | ||
| it("can convert an Array to an Iterator", () => { | ||
| const array = [-3, 1, -4, 1, -5]; | ||
| const iterator = Iterator.from(array); | ||
|
|
||
| expect([...iterator]).toStrictEqual(array); | ||
| }); | ||
|
|
||
| it("can convert a Set to an Iterator", () => { | ||
| const set = new Set([-3, 1, 1, -4, -4, 1, -5]); | ||
| const iterator = Iterator.from(set); | ||
|
|
||
| expect([...iterator]).toStrictEqual([...set]); | ||
| }); | ||
|
|
||
| it("can convert a Map to an Iterator", () => { | ||
| const map = new Map([ | ||
| ["z", 3], | ||
| ["e", 1], | ||
| ["d", 4], | ||
| ]); | ||
| const iterator = Iterator.from(map); | ||
| expect([...iterator]).toStrictEqual([...map.entries()]); | ||
| }); | ||
|
|
||
| it("can convert a Generator Object to an Iterator", () => { | ||
| const factory = function* (): Generator<number, void, void> { | ||
| yield 3; | ||
| yield 1; | ||
| yield 4; | ||
| yield 1; | ||
| yield 5; | ||
| }; | ||
| const iterator = Iterator.from(factory()); | ||
|
|
||
| expect([...iterator]).toStrictEqual([3, 1, 4, 1, 5]); | ||
| }); | ||
|
|
||
| it("can convert a String to an Iterator", () => { | ||
| const string = "🚀💯🔥"; | ||
| const iterator = Iterator.from(string); | ||
|
|
||
| expect([...iterator]).toStrictEqual([...string]); | ||
| }); | ||
|
|
||
| it("can convert an Iterator to an IterableIterator and returns the same reference", () => { | ||
| const array = [-3, 1, -4, 1, -5]; | ||
| const iterator = array.values(); | ||
| const convertedIterator = Iterator.from(iterator); | ||
|
|
||
| expect(convertedIterator).toBe(iterator); | ||
| }); | ||
|
|
||
| it("can convert an IterableIterator to an Iterator", () => { | ||
| const array = [-3, 1, -4, 1, -5]; | ||
| const iterator = Iterator.from(array.values()); | ||
| const iterator2 = Iterator.from(iterator[Symbol.iterator]()); | ||
|
|
||
| expect([...iterator2]).toStrictEqual(array); | ||
| }); | ||
|
|
||
| it("can convert an object with a random next() method to an Iterator", () => { | ||
| const object = { | ||
| values: [-3, 1, -4, 1, -5, 9], | ||
| index: 0, | ||
| next(): IteratorResult<number, void> { | ||
| if (this.index < this.values.length) { | ||
| return { value: this.values[this.index++], done: false }; | ||
| } | ||
| return { done: true, value: undefined }; | ||
| }, | ||
| }; | ||
| const iterator = Iterator.from(object) | ||
| .map((x) => x * 2) | ||
| .filter((x) => x > 0); | ||
|
|
||
| expect([...iterator]).toStrictEqual([2, 2, 18]); | ||
| }); | ||
|
|
||
| it("throws an error if the object is not an Iterator, Iterable, or an object with a next method", () => { | ||
| const object = {}; | ||
| // @ts-expect-error Invalid argument | ||
| expect(() => Iterator.from(object)).toThrow(TypeError); | ||
| }); | ||
| }); |
43 changes: 43 additions & 0 deletions
43
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
This file contains hidden or 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,43 @@ | ||
| import "../Iterator.prototype.toIterable/index.ts"; | ||
| import { iteratorPrototype } from "../Iterator.prototype/index.ts"; | ||
|
|
||
| declare global { | ||
| interface IteratorConstructor { | ||
| from<T>( | ||
| object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> }, | ||
| ): IterableIterator<T>; | ||
| } | ||
|
|
||
| const Iterator: IteratorConstructor; | ||
| } | ||
|
|
||
| (globalThis as Record<string, unknown>).Iterator ??= {}; | ||
|
|
||
| ((globalThis as Record<string, unknown>).Iterator as { from: unknown }).from ??= | ||
| function <T>( | ||
| object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> }, | ||
| ): IterableIterator<T> { | ||
elimanzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const toIterable = iteratorPrototype.toIterable as ( | ||
| this: Iterator<T>, | ||
| ) => IterableIterator<T>; | ||
|
|
||
| const iteratorFactory = (object as Iterable<T>)[Symbol.iterator]; | ||
| if (typeof iteratorFactory === "function") { | ||
| return toIterable.call(iteratorFactory.call(object)); | ||
| } | ||
|
|
||
| // eslint-disable-next-line no-prototype-builtins | ||
| if (iteratorPrototype.isPrototypeOf(object)) { | ||
| return toIterable.call(object as Iterator<T>); | ||
| } | ||
|
|
||
| if (typeof (object as Record<string, unknown>).next === "function") { | ||
| return (function* () { | ||
| yield* toIterable.call(object as Iterator<T>); | ||
| })(); | ||
| } | ||
|
|
||
| throw new TypeError( | ||
| "Object is not an iterator, iterable, or an object with a next method", | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.