I'm playing with some new JavaScript features like async/await and generators. I have function readPages with signature
async function* readPages(....): AsyncIterableIterator<string> {}
and I want to concat result of this function with some delimiter. Here is how I'm doing it now
let array = new Array<string>();
for await (const page of readPages(...))
array.push(page);
let result = array.join(pagesDelimiter);
That's pretty verbose I think. Can it be done better?
Here is full code for reference
import * as fs from 'fs';
import { PDFJSStatic, PDFDocumentProxy } from 'pdfjs-dist';
const PDFJS: PDFJSStatic = require('pdfjs-dist');
PDFJS.disableWorker = true;
async function* readPages(doc: PDFDocumentProxy, wordsDelimiter = '\t'): AsyncIterableIterator<string> {
for (let i = 1; i <= doc.numPages; i++) {
const page = await doc.getPage(i);
const textContent = await page.getTextContent();
yield textContent.items.map(item => item.str).join(wordsDelimiter);
}
}
async function pdfToText(filename: string, pagesDelimiter = '\n', wordsDelimiter = '\t') {
const data = new Uint8Array(fs.readFileSync(filename));
const doc = await PDFJS.getDocument(data);
const array = new Array<string>();
for await (const page of readPages(doc, wordsDelimiter))
array.push(page);
return array.join(pagesDelimiter);
}
pdfToText('input.pdf').then(console.log);
readPages(...).join(delimiter):-)['a', 'b', 'c'].join('_')is preferable than for loop, this should be too (if solution exist). RxJS can't operate with Promises (or maybe I'm wrong) and it is another lib. I'm looking for native JavaScript solution.Observable.fromPromise();) ) . It can operate with Iterable as well (Observable.from) . Not sure how to mix both right now, though.