Skip to content

Commit

Permalink
feat: transformOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Apr 19, 2024
1 parent 25fff9e commit 01a6e03
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from './stream/progressLogger'
export * from './stream/transform/transformChunk'
export * from './stream/transform/transformFilter'
export * from './stream/transform/transformLimit'
export * from './stream/transform/transformOffset'
export * from './stream/transform/transformLogProgress'
export * from './stream/transform/transformMap'
export * from './stream/transform/transformMapSimple'
Expand Down
5 changes: 1 addition & 4 deletions src/stream/transform/transformLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export interface TransformLimitOptions extends TransformOptions {
*/
export class TransformLimit extends AbortableTransform {}

/**
* 0 or falsy value means "no limit"
*/
export function transformLimit<IN>(opt: TransformLimitOptions = {}): TransformTyped<IN, IN> {
export function transformLimit<IN>(opt: TransformLimitOptions): TransformTyped<IN, IN> {
const { logger = console, limit, debug } = opt

if (!limit) {
Expand Down
17 changes: 17 additions & 0 deletions src/stream/transform/transformOffset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { _range } from '@naturalcycles/js-lib'
import { readableFrom } from '../..'
import { _pipelineToArray } from '../pipeline/pipeline'
import { transformOffset } from './transformOffset'

test('transformOffset', async () => {
const data = _range(1, 30).map(n => ({ id: String(n) }))
const readable = readableFrom(data)

const arr = await _pipelineToArray([
readable,
// transformTap((r, i) => console.log(i)),
transformOffset({ offset: 10 }),
])

expect(arr).toEqual(data.slice(10))
})
34 changes: 34 additions & 0 deletions src/stream/transform/transformOffset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Transform } from 'node:stream'
import { transformNoOp } from '../../index'
import { TransformOptions, TransformTyped } from '../stream.model'

export interface TransformOffsetOptions extends TransformOptions {
/**
* How many items to skip (offset) in the stream.
*
* Nullish value (e.g 0 or undefined) would mean "no offset".
*/
offset?: number
}

export function transformOffset<IN>(opt: TransformOffsetOptions): TransformTyped<IN, IN> {
const { offset } = opt

if (!offset) {
// No offset - returning pass-through transform
return transformNoOp()
}

let i = 0 // so we start first chunk with 1
return new Transform({
objectMode: true,
...opt,
transform(chunk: IN, _, cb) {
if (++i <= offset) {
return cb() // skip
}

cb(null, chunk)
},
})
}

0 comments on commit 01a6e03

Please sign in to comment.