Repository URL to install this package:
Version:
1.2.7 ▾
|
class IteratorWrap<Value> implements Iterator<Value> {
index = -1
array: Array<Value>
constructor(array: Array<Value>) {
this.array = array
}
hasNext() {
return this.index + 1 < this.array.length
}
next(): IteratorResult<Value> {
this.index += 1
if (this.index > this.array.length) {
return {
done: true,
value: undefined,
}
} else {
return {
value: this.array[this.index],
done: false,
}
}
}
remove() {
this.index -= 1
this.array.splice(this.index, 1)
}
}
function toIterator<Value>(
x: Array<Value> | Value
): Iterator<Value> | IterableIterator<[string, Value]> {
if (Array.isArray(x)) {
return x[Symbol.iterator]()
} else if (typeof x === 'object') {
return Object.entries(x)[Symbol.iterator]()
} else {
return new IteratorWrap([x])
}
}
export { IteratorWrap }
export { toIterator }