Skip to content

Commit f923b1c

Browse files
author
Trevor Sears
committed
Large v1.0.0 update.
Documentation overhauls, added more malleable generic signature, etc.
1 parent 11bd608 commit f923b1c

5 files changed

Lines changed: 196 additions & 132 deletions

File tree

ts/abstract-iterable.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,60 @@
11
/*
2-
* Created by Trevor Sears <trevorsears.main@gmail.com>.
3-
* 7:45 PM -- October 06th, 2019.
4-
* Project: @jsdsl/iterator
2+
* Created by Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/).
3+
* 3:43 PM -- August 31st, 2021.
4+
* Project: @jsdsl/iterator
5+
*
6+
* @jsdsl/iterator - A collection of classes that allow iteration over a predefined collection of elements.
7+
* Copyright (C) 2021 Trevor Sears
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
521
*/
622

7-
import { Iterator as JSDSLIterator } from "./iterator";
8-
import { Iterable as JSDSLIterable } from "./iterable";
23+
import { Iterator } from "./iterator";
24+
import { Iterable } from "./iterable";
925

1026
/**
11-
* An abstract implementation of the JSDSL {@link Iterable} interface.
27+
* A basic abstract implementation of a class that is iterable (i.e. navigable via JavaScript's `for...of` construct).
28+
*
29+
* This class serves as an abstract implementation of the JSDSL {@link Iterable} interface.
30+
*
31+
* @param {any} E The type of element that this AbstractIterable intends to iterate over.
32+
* @param {any} U The type of the value that will be returned in the case that this AbstractIterable's content has been
33+
* exhausted. Can be set to 'void' in the case that this AbstractIterable has infinite content. Defaults to `undefined`.
1234
*
13-
* @author Trevor Sears <trevorsears.main@gmail.com>
14-
* @version v0.1.0
35+
* @author Trevor Sears <trevor@trevorsears.com>
36+
* @version v1.0.0
1537
* @since v0.1.0
1638
*/
17-
export abstract class AbstractIterable<E> implements JSDSLIterable<E> {
39+
export abstract class AbstractIterable<E, U = undefined> implements Iterable<E, U> {
1840

1941
/**
2042
* Returns an iterator over the element contents of this AbstractIterable.
2143
*
22-
* @return An iterator over the element contents of this AbstractIterable.
44+
* @return {Iterator<E, U>} An iterator over the element contents of this AbstractIterable.
2345
*/
24-
public abstract iterator(): JSDSLIterator<E>;
46+
public abstract iterator(): Iterator<E, U>;
2547

2648
/**
27-
* Returns an instance of an IterableIterator that allows 'this' to be iterable using the baked-in 'for...of'
49+
* Returns an instance of an IterableIterator that allows 'this' to be iterable using the baked-in `for...of`
2850
* syntax, rather than more verbose iteration (i.e. using a 'while' loop).
2951
*
30-
* @return An instance of an IterableIterator.
52+
* @return {IterableIterator<E>} An instance of an IterableIterator.
3153
*/
3254
public [Symbol.iterator](): IterableIterator<E> {
3355

34-
return this.iterator().getIterableIterator();
56+
return this.iterator()[Symbol.iterator]();
3557

3658
}
3759

38-
}
60+
}

ts/abstract-iterator.ts

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
/*
2-
* Created by Trevor Sears <trevorsears.main@gmail.com>.
3-
* 7:43 PM -- October 06th, 2019.
4-
* Project: @jsdsl/iterator
2+
* Created by Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/).
3+
* 3:43 PM -- August 31st, 2021.
4+
* Project: @jsdsl/iterator
5+
*
6+
* @jsdsl/iterator - A collection of classes that allow iteration over a predefined collection of elements.
7+
* Copyright (C) 2021 Trevor Sears
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
521
*/
622

7-
import { Iterator as JSDSLIterator } from "./iterator";
23+
import { Iterator } from "./iterator";
824
import { AbstractIterable } from "./abstract-iterable";
925

1026
/**
11-
* An abstract implementation of the JSDSL {@link Iterator} interface.
27+
* A basic abstract implementation of the JSDSL {@link Iterator} protocol, a class that furnishes the functionality
28+
* required to use an object with the JavaScript `for...of` syntax.
1229
*
13-
* @author Trevor Sears <trevorsears.main@gmail.com>
14-
* @version v0.2.0
30+
* Note: Usually, classes that desire to be 'iterable' should not extend this class, but should instead implement the
31+
* {@link Iterable} interface (or extend the {@link AbstractIterable} abstract class). Extending this class
32+
* syntactically implies that the class in question 'is-a' iterator, whereas most use cases intend to assert that the
33+
* class in question 'has-a' iterator. In either case, iteration via `for...of` works as expected (with either an
34+
* {@link Iterator} or an {@link Iterable}).
35+
*
36+
* @param {any} E The type of element that this AbstractIterator intends to iterate over.
37+
* @param {any} U The type of the value that will be returned in the case that this AbstractIterator's content has been
38+
* exhausted. Can be set to 'void' in the case that this AbstractIterator has infinite content. Defaults to `undefined`.
39+
*
40+
* @author Trevor Sears <trevor@trevorsears.com>
41+
* @version v1.0.0
1542
* @since v0.1.0
1643
*/
17-
export abstract class AbstractIterator<E> extends AbstractIterable<E> implements JSDSLIterator<E> {
44+
export abstract class AbstractIterator<E, U = undefined> extends AbstractIterable<E, U> implements Iterator<E, U> {
1845

1946
/**
2047
* Returns true if a call to {@link #next} would return a valid and meaningful result after calling this method.
@@ -24,25 +51,23 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
2451
* present in the underlying structure versus the undefined value that is returned from {@link #next} when all other
2552
* valid results are exhausted.
2653
*
27-
* @return true if a call to {@link #next} would return a valid and meaningful result.
54+
* @return {boolean} true if a call to {@link #next} would return a valid and meaningful result.
2855
*/
2956
public abstract hasNext(): boolean;
3057

3158
/**
32-
* Returns the next element this AbstractIterator has to iterate over, over undefined if there are no more valid
33-
* elements to return.
59+
* Returns the next element this AbstractIterator has to iterate over.
3460
*
35-
* @return The next element this AbstractIterator has to iterate over, over undefined if there are no more valid
36-
* elements to return.
61+
* @return {E | U} The next element this AbstractIterator has to iterate over.
3762
*/
38-
public abstract next(): E | undefined;
63+
public abstract next(): E | U;
3964

4065
/**
4166
* Performs the specified action for all of the remaining elements in this AbstractIterator.
4267
*
43-
* @param callback The action to perform on the remaining elements of this AbstractIterator.
68+
* @param {(element: E) => any} callback The action to perform on the remaining elements of this AbstractIterator.
4469
*/
45-
public forEach(callback: (element: E) => void): void {
70+
public forEachRemaining(callback: (element: E) => any): void {
4671

4772
for (let element of this) callback(element);
4873

@@ -54,9 +79,9 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
5479
* Note if this method is not overridden in implementing child classes, the `#remove` method will throw an error,
5580
* with a warning that the method is not supported for the current implementation.
5681
*
57-
* @return The last element returned by the {@link #next} method.
82+
* @return {E | U} The last element returned by the {@link #next} method.
5883
*/
59-
public remove(): E | undefined {
84+
public remove(): E | U {
6085

6186
throw new Error("ERR | #remove operation is not supported for this implementation of AbstractIterator.");
6287

@@ -66,8 +91,11 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
6691
* Resets this AbstractIterator back to it's initial position, readying it to iterate over the underlying collection
6792
* from the 'beginning' again.
6893
*
69-
* Note if this method is not overridden in implementing child classes, the `#reset` method will throw an error,
94+
* Note: If this method is not overridden in implementing child classes, the `#reset` method will throw an error,
7095
* with a warning that the method is not supported for the current implementation.
96+
*
97+
* Note: This does not/should not modify the underlying data structure, meaning that any modifications to the
98+
* underlying structure will not be/should not be 'undone' by calling this method.
7199
*/
72100
public reset(): void {
73101

@@ -76,56 +104,17 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
76104
}
77105

78106
/**
79-
* Returns an Iterator over the contents of this AbstractIterator.
107+
* Returns an {@link Iterator} over the contents of this AbstractIterator.
80108
*
81-
* Because of the fact that this class is in-and-of-itself an Iterator, this method simply returns 'this'. This is
82-
* simply a formality so that this class will conform to the {@link Iterable} interface.
109+
* Because of the fact that this class is in-and-of-itself an {@link Iterator}, this method simply returns 'this'.
110+
* This is simply a formality so that this class will conform to the {@link Iterable} interface.
83111
*
84-
* @return An Iterator over the contents of this AbstractIterator.
112+
* @return {Iterator<E, U>} An {@link Iterator} over the contents of this AbstractIterator.
85113
*/
86-
public iterator(): JSDSLIterator<E> {
114+
public iterator(): Iterator<E, U> {
87115

88116
return this;
89117

90118
}
91119

92-
/**
93-
* This method is simply an ease-of-understanding alias method for the [Symbol.iterator] method.
94-
*
95-
* @return An instance of an IterableIterator.
96-
* @see AbstractIterator#[Symbol.iterator]
97-
*/
98-
public getIterableIterator(): IterableIterator<E> {
99-
100-
return new class implements IterableIterator<E> {
101-
102-
private iterator: AbstractIterator<E>;
103-
104-
public constructor(iterator: AbstractIterator<E>) {
105-
106-
this.iterator = iterator;
107-
108-
}
109-
110-
public [Symbol.iterator](): IterableIterator<E> {
111-
112-
return this;
113-
114-
}
115-
116-
public next(): IteratorResult<E> {
117-
118-
return {
119-
120-
done: !this.iterator.hasNext(),
121-
value: this.iterator.next() as E
122-
123-
};
124-
125-
}
126-
127-
}(this);
128-
129-
}
130-
131-
}
120+
}

ts/iterable.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
11
/*
2-
* Created by Trevor Sears <trevorsears.main@gmail.com>.
3-
* 7:41 PM -- October 06th, 2019.
4-
* Project: @jsdsl/iterator
2+
* Created by Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/).
3+
* 3:43 PM -- August 31st, 2021.
4+
* Project: @jsdsl/iterator
5+
*
6+
* @jsdsl/iterator - A collection of classes that allow iteration over a predefined collection of elements.
7+
* Copyright (C) 2021 Trevor Sears
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
521
*/
622

7-
import { Iterator as JSDSLIterator } from "./iterator";
23+
import { Iterator } from "./iterator";
824

925
/**
10-
* An interface representing some type that can be iterated over via a {@link Iterator}.
26+
* An interface representing some type that can be iterated over via an {@link Iterator}.
27+
*
28+
* @param {any} E The type of element that this Iterable intends to iterate over.
29+
* @param {any} U The type of the value that will be returned in the case that this Iterable's content has been
30+
* exhausted. Can be set to 'void' in the case that this Iterable has infinite content. Defaults to `undefined`.
1131
*
12-
* @author Trevor Sears <trevorsears.main@gmail.com>
13-
* @version v0.1.0
32+
* @author Trevor Sears <trevor@trevorsears.com>
33+
* @version v1.0.0
1434
* @since v0.1.0
1535
*/
16-
export interface Iterable<E> {
36+
export interface Iterable<E, U = undefined> {
1737

1838
/**
19-
* Returns an iterator over the element contents of this Iterable.
39+
* Returns an iterator over the contents of this Iterable.
2040
*
21-
* @return An iterator over the element contents of this Iterable.
41+
* @return {Iterator<E, U>} An iterator over the contents of this Iterable.
2242
*/
23-
iterator(): JSDSLIterator<E>;
43+
iterator(): Iterator<E, U>;
2444

2545
/**
26-
* Returns an instance of an IterableIterator that allows 'this' to be iterable using the baked-in 'for...of'
27-
* syntax, rather than more verbose iteration (i.e. using a 'while' loop).
46+
* Returns an instance of an IterableIterator.
47+
*
48+
* The presence of this method allows 'this' to be iterable using the baked-in 'for...of' syntax, rather than more
49+
* verbose iteration (i.e. using a 'while' loop).
2850
*
29-
* @return An instance of an IterableIterator.
51+
* @return {IterableIterator<E>} An instance of an IterableIterator.
3052
*/
3153
[Symbol.iterator](): IterableIterator<E>;
3254

33-
}
55+
}

0 commit comments

Comments
 (0)