Skip to content

Commit a182824

Browse files
committed
feat(streams): implement skip operator
1 parent 6258752 commit a182824

File tree

5 files changed

+504
-287
lines changed

5 files changed

+504
-287
lines changed

.changeset/twelve-things-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@yeger/streams': minor
3+
---
4+
5+
implement skip operator

packages/streams/src/async.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ export function zip<T, R>(other: MaybeAsyncIterable<R>): AsyncOperator<T, [T, R]
451451
})
452452
}
453453

454+
/**
455+
* Skips the first n items from the source.
456+
*/
457+
export function skip<T>(n: number): AsyncOperator<T, T> {
458+
return (source: AsyncIterable<T>) =>
459+
createAsyncIterable(async function* (): AsyncIterableIterator<T> {
460+
let skipped = 0
461+
for await (const item of source) {
462+
if (skipped++ < n) {
463+
continue
464+
}
465+
yield item
466+
}
467+
})
468+
}
469+
454470
/**
455471
* Emits at most the first n items from the source.
456472
*/

packages/streams/src/sync.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,22 @@ export function zip<T, R>(other: Iterable<R>): Operator<T, [T, R]> {
423423
})
424424
}
425425

426+
/**
427+
* Skips the first n items from the source.
428+
*/
429+
export function skip<T>(n: number): Operator<T, T> {
430+
return (source: Iterable<T>) =>
431+
createIterable(function* (): IterableIterator<T> {
432+
let skipped = 0
433+
for (const item of source) {
434+
if (skipped++ < n) {
435+
continue
436+
}
437+
yield item
438+
}
439+
})
440+
}
441+
426442
/**
427443
* Emits at most the first n items from the source.
428444
*/

0 commit comments

Comments
 (0)