Home Manual Reference Source Test Repository

src/tools/split.js


import { fromiterable } from './from' ;

import { exhaust } from './exhaust' ;

/**
 * Splits a stream into a stream of streams according to some set of
 * separators.
 * @param {Stream} stream
 * @param {Iterable} sep
 */
export function split ( stream , sep ) {

	return fromiterable( _split( stream , sep ) ) ;

}

export function* _split ( stream , sep ) {

	const _sep = new Set( sep ) ;

	while ( true ) {

		const token = stream.read( ) ;

		if ( token === null ) break ;

		if ( _sep.has( token ) ) continue ;

		const group = fromiterable( ( function* ( ) {

			yield token ;

			while ( true ) {

				const token = stream.read( ) ;

				if ( _sep.has( token ) ) break ;

				yield token ;

			}

		} )( ) ) ;

		yield group ;

		exhaust( group ) ;

	}

}