import { IterableX } from '../iterablex' ; import { OperatorFunction } from '../../interfaces' ; export class BufferIterable < TSource > extends IterableX < TSource [ ] > { private _source : Iterable < TSource > ; private _count : number ; private _skip : number ; constructor ( source , count , skip ) { super ( ) ; this . _source = source ; this . _count = count ; this . _skip = skip ; } * [ Symbol . iterator ] ( ) { const buffers = [ ] ; let i = 0 ; for ( const item of this . _source ) { if ( i % this . _skip === 0 ) { buffers . push ( [ ] ) ; } for ( const buff of buffers ) { buff . push ( item ) ; } if ( buffers . length > 0 && buffers [ 0 ] . length === this . _count ) { yield buffers . shift ( ) ! ; } i ++ ; } while ( buffers . length > 0 ) { yield buffers . shift ( ) ! ; } } } example param { Iterabel < TSource > } source param { number } count param { number } skip return { IterableX < TSource > [ ] } export function buffer < TSource > ( count , skip ? ) { let s = skip ; if ( s == null ) { s = count ; } return function bufferOperatorFunction ( source ) : IterableX < TSource [ ] > { return new BufferIterable ( source , count , s ! ) ; } ; }