add checks for array enumerators

This commit is contained in:
Rowan 2025-05-01 16:37:08 -05:00
parent 7f5561a0cc
commit f3bfae6e14

View file

@ -311,6 +311,14 @@ export class Enumerator<T> implements IEnumerator<T>, Iterator<T> {
}
}
static isEnumerator<T>(value: object): value is IEnumerator<T> {
return hasMethods(['moveNext', 'reset', 'toIterator'], value)
}
static isArrayEnumerator<T>(value: any): value is IArrayEnumerator<T> {
return typeof value['setIndex'] === 'function' && this.isEnumerator(value)
}
static toIterator<T>(enumerator: IEnumerator<T>): Iterator<T> {
return new this(enumerator)
}
@ -585,7 +593,9 @@ export class Enumerable<T> implements IEnumerable<T>, Iterable<T> {
static from<T>(value: IEnumerable<T> | IEnumerator<T> | Iterable<T> | Iterator<T>): Enumerable<T> {
if (this.isEnumerable<T>(value)) {
return value
} else if (this.isEnumerator<T>(value)) {
} else if (Enumerator.isArrayEnumerator<T>(value)) {
return new ArrayEnumerble(value)
} else if (Enumerator.isEnumerator<T>(value)) {
return new Enumerable(value)
} else if (isIterable(value)) {
const enumerator = Enumerator.fromIterable(value)
@ -608,10 +618,6 @@ export class Enumerable<T> implements IEnumerable<T>, Iterable<T> {
return typeof value['enumerator'] === 'function'
}
static isEnumerator<T>(value: object): value is IEnumerator<T> {
return hasMethods(['moveNext', 'reset', 'toIterator'], value)
}
[Symbol.iterator](): Iterator<T> {
return this._enumerator.toIterator()
}