Skip to content

Commit

Permalink
Yield items in a retroable in retro()
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Aug 20, 2022
1 parent da781e4 commit 52c8f5f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/algorithm/src/retro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ export interface IRetroable<T> {
* Array.from(stream); // [6, 5, 4, 3, 2, 1]
* ```
*/
export function retro<T>(
export function* retro<T>(
object: IRetroable<T> | ArrayLike<T>
): IterableIterator<T> {
if (typeof (object as any).retro === 'function') {
return (object as IRetroable<T>).retro();
}
return (function* (object) {
for (let index = object.length - 1; index > -1; index--) {
yield object[index];
const it = (object as IRetroable<T>).retro();
let item: IteratorResult<T>;
while (!(item = it.next()).done) {
yield item.value;
}
} else {
for (let index = (object as ArrayLike<T>).length - 1; index > -1; index--) {
yield (object as ArrayLike<T>)[index];
}
})(object as ArrayLike<T>);
}
}

0 comments on commit 52c8f5f

Please sign in to comment.