hunspell-reader
Version:
A library for reading Hunspell Dictionary Files
45 lines • 1.51 kB
JavaScript
import * as stream from 'node:stream';
/**
* Transform an iterable into a node readable stream.
*/
export function iterableToStream(src, options) {
return new ReadableObservableStream(src, options ?? { encoding: 'utf8' });
}
class ReadableObservableStream extends stream.Readable {
_source;
iter;
done = false;
constructor(_source, options) {
super(options);
this._source = _source;
}
_read() {
if (!this.iter) {
this.iter = this._source[Symbol.iterator]();
}
if (this.done) {
// See: https://nodejs.org/api/stream.html#readablepushchunk-encoding
// Pushing null means the stream is done.
// eslint-disable-next-line unicorn/no-null
this.push(null);
return;
}
let r = this.iter.next();
while (!r.done && this.push(r.value)) {
r = this.iter.next();
}
if (r.done) {
this.done = true;
// since it is possible for r.value to have something meaningful, we must check.
if (r.value !== null && r.value !== undefined) {
this.push(r.value);
}
// See: https://nodejs.org/api/stream.html#readablepushchunk-encoding
// Pushing null means the stream is done.
// eslint-disable-next-line unicorn/no-null
this.push(null);
}
}
}
export default iterableToStream;
//# sourceMappingURL=iterableToStream.js.map