Skip to content

Commit

Permalink
Add peekVal, hasCached, and asyncIterplus
Browse files Browse the repository at this point in the history
  • Loading branch information
Aplet123 committed Sep 1, 2021
1 parent 499495a commit b9b9918
Show file tree
Hide file tree
Showing 28 changed files with 1,018 additions and 665 deletions.
2 changes: 1 addition & 1 deletion bundle/index.min.js

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions deno_compat/AsyncIterPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {nullVal} from "./IterPlus.ts";
/**
* The type of null to use.
*/
//export type AsyncNull = typeof asyncNullVal;
//export type Null = typeof nullVal;
import {Null} from "./IterPlus.ts";
/**
* A wrapper around an iterator to add additional functionality. The types intentionally ignore return value.
Expand Down Expand Up @@ -3134,7 +3134,7 @@ export class AsyncPeekable<T> extends AsyncIterPlus<T> {
/**
* Peeks the next element in the iterator and does not consume it.
*
* @returns The next element.
* @returns The next element as an iterator result.
*/
async peek(): Promise<IteratorResult<T>> {
if (this.storedVal.has) {
Expand All @@ -3143,4 +3143,30 @@ export class AsyncPeekable<T> extends AsyncIterPlus<T> {
this.storedVal = {has: true, val: await this.internal.next()};
return this.storedVal.val;
}

/**
* Peeks the next element in the iterator and does not consume it.
*
* Nullable version of `peek`.
*
* @returns The next element, or `null` if the iterator is finished.
*/
async peekVal(): Promise<T | Null> {
const res = await this.peek();
if (res.done) {
return nullVal;
}
return res.value;
}

/**
* Checks if there's a value cached from a previous `peek`.
*
* Will return `true` even if the cached value is the end of the iterator.
*
* @returns If there's a value cached.
*/
hasCached() {
return this.storedVal.has;
}
}
31 changes: 28 additions & 3 deletions deno_compat/IterPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ type /* o:Async- */ IterableMap<T extends unknown[]> = {
/**
* The type of null to use.
*/
/* o://- */ export type /* o:Async- */ Null =
typeof /* r:asyncNullVal */ nullVal;
/* o://- */ export type Null = typeof nullVal;
/* o:import {Null} from "./IterPlus"; */

/**
Expand Down Expand Up @@ -3253,7 +3252,7 @@ export class /* o:Async- */ Peekable<
/**
* Peeks the next element in the iterator and does not consume it.
*
* @returns The next element.
* @returns The next element as an iterator result.
*/
/* o:async */ peek(): /* o:Promise<- */ IteratorResult<T> /* o:-> */ {
if (this.storedVal.has) {
Expand All @@ -3262,4 +3261,30 @@ export class /* o:Async- */ Peekable<
this.storedVal = {has: true, val: /* o:await */ this.internal.next()};
return this.storedVal.val;
}

/**
* Peeks the next element in the iterator and does not consume it.
*
* Nullable version of `peek`.
*
* @returns The next element, or `null` if the iterator is finished.
*/
/* o:async */ peekVal(): /* o:Promise<- */ T | Null /* o:-> */ {
const res = /* o:await */ this.peek();
if (res.done) {
return nullVal;
}
return res.value;
}

/**
* Checks if there's a value cached from a previous `peek`.
*
* Will return `true` even if the cached value is the end of the iterator.
*
* @returns If there's a value cached.
*/
hasCached() {
return this.storedVal.has;
}
}
35 changes: 14 additions & 21 deletions deno_compat/util.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import {IterPlus, canIter} from "./IterPlus.ts";
import {AsyncIterPlus, canAsyncIter} from "./AsyncIterPlus.ts";
import {IterPlus} from "./IterPlus.ts";
import {AsyncIterPlus} from "./AsyncIterPlus.ts";
/**
* A promise or a value that can be awaited.
*/
export type PromiseOrValue<T> = T | Promise<T>;

/**
* The return type of `iterplus`.
* Generates an `IterPlus` from an iterable.
*
* @typeParam T The iteration type.
* @param iter The iterable to upgrade.
*/
type UpgradeIter<T> = T extends Iterable<infer I>
? IterPlus<I>
: T extends AsyncIterable<infer I>
? AsyncIterPlus<I>
: IterPlus<unknown> | AsyncIterPlus<unknown>;
export function iterplus<T>(iter: Iterable<T>): IterPlus<T> {
return new IterPlus(iter[Symbol.iterator]());
}

/**
* Generates an `IterPlus` from an iterable or async iterable.
* Generates an `AsyncIterPlus` from an async iterable.
*
* @typeParam T The iterable/async iterable to upgrade.
* @param iter The iterable to upgrade.
* @typeParam T The iteration type.
* @param iter The async iterable to upgrade.
*/
export function iterplus<T>(iter: Iterable<T>): IterPlus<T>;
export function iterplus<T>(iter: AsyncIterable<T>): AsyncIterPlus<T>;
export function iterplus(iter: any): any {
if (canIter(iter)) {
return new IterPlus(iter[Symbol.iterator]());
} else if (canAsyncIter(iter)) {
return new AsyncIterPlus(iter[Symbol.asyncIterator]());
} else {
throw new Error("Object is not an iterable.");
}
export function asyncIterplus<T>(iter: AsyncIterable<T>): AsyncIterPlus<T> {
return new AsyncIterPlus(iter[Symbol.asyncIterator]());
}

/**
Expand Down
18 changes: 17 additions & 1 deletion dest/src/AsyncIterPlus.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,24 @@ export declare class AsyncPeekable<T> extends AsyncIterPlus<T> {
/**
* Peeks the next element in the iterator and does not consume it.
*
* @returns The next element.
* @returns The next element as an iterator result.
*/
peek(): Promise<IteratorResult<T>>;
/**
* Peeks the next element in the iterator and does not consume it.
*
* Nullable version of `peek`.
*
* @returns The next element, or `null` if the iterator is finished.
*/
peekVal(): Promise<T | Null>;
/**
* Checks if there's a value cached from a previous `peek`.
*
* Will return `true` even if the cached value is the end of the iterator.
*
* @returns If there's a value cached.
*/
hasCached(): boolean;
}
export {};
26 changes: 25 additions & 1 deletion dest/src/AsyncIterPlus.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dest/src/AsyncIterPlus.js.map

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion dest/src/IterPlus.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,24 @@ export declare class Peekable<T>/* r:extends Async- */ extends IterPlus<T> {
/**
* Peeks the next element in the iterator and does not consume it.
*
* @returns The next element.
* @returns The next element as an iterator result.
*/
peek(): IteratorResult<T>;
/**
* Peeks the next element in the iterator and does not consume it.
*
* Nullable version of `peek`.
*
* @returns The next element, or `null` if the iterator is finished.
*/
peekVal(): /* o:Promise<- */ T | Null;
/**
* Checks if there's a value cached from a previous `peek`.
*
* Will return `true` even if the cached value is the end of the iterator.
*
* @returns If there's a value cached.
*/
hasCached(): boolean;
}
export {};
26 changes: 25 additions & 1 deletion dest/src/IterPlus.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dest/src/IterPlus.js.map

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions dest/src/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import { AsyncIterPlus } from "./AsyncIterPlus";
*/
export declare type PromiseOrValue<T> = T | Promise<T>;
/**
* Generates an `IterPlus` from an iterable or async iterable.
* Generates an `IterPlus` from an iterable.
*
* @typeParam T The iterable/async iterable to upgrade.
* @typeParam T The iteration type.
* @param iter The iterable to upgrade.
*/
export declare function iterplus<T>(iter: Iterable<T>): IterPlus<T>;
export declare function iterplus<T>(iter: AsyncIterable<T>): AsyncIterPlus<T>;
/**
* Generates an `AsyncIterPlus` from an async iterable.
*
* @typeParam T The iteration type.
* @param iter The async iterable to upgrade.
*/
export declare function asyncIterplus<T>(iter: AsyncIterable<T>): AsyncIterPlus<T>;
/**
* Creates an inclusive-exclusive range iterator that's useful for loops.
*
Expand Down
28 changes: 18 additions & 10 deletions dest/src/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dest/src/util.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions dest/test/iterplus.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dest/test/iterplus.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Large diffs are not rendered by default.

Loading

0 comments on commit b9b9918

Please sign in to comment.