I'm facing a challenge with processing a stream of strings where I need to emit each line individually. For example, starting with the string array:
let stream$ = from(['hello\n', 'world ', ' at\nhome\n and', ' more'])
I want to transform it into this stream:
'hello'
'world at'
'home'
' and more'
To accomplish this, I believe I need to use the merge
operator after ensuring that there are no lines spanning multiple values in the stream. My initial approach looks like:
let break$ = new Subject()
stream$.pipe(
flatMap(x => x.match(/[^\n]+\n?|\//g)),
map(x => {
if (x.endsWith('\n')) {
break$.next(true)
return x
}
return x
})
.buffer(break$)
)
However, the output of this pipe is currently a single array of values rather than grouping them by lines as expected:
[ 'hello\n', 'world ', ' at\n', 'home\n', ' and', ' more' ]
My desired outcome would be:
[
['hello\n'],
['world ', ' at\n'],
['home\n'],
[' and', ' more'],
]
I do have a functioning solution available here, but it requires a subscription which I'd prefer to avoid in favor of lazy evaluation using a pipe.