Matěj Pokorný
Matěj Pokorný

Reputation: 17895

How to refactor simple for-await-of loop?

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);

Upvotes: 1

Views: 605

Answers (1)

Matěj Pokorn&#253;
Matěj Pokorn&#253;

Reputation: 17895

OK, I'm playing with that code little bit more and I think it is not currently possible to handle this task better than with for-await-of loop. But, you can hide that loop behind prototyped function...

declare global {
    interface AsyncIterableIterator<T> {
        toPromise(): Promise<T[]>;
    }
}

(async function* (): any {})().constructor.prototype.toPromise = async function<T>(this: AsyncIterableIterator<T>): Promise<T[]> {
    let result = new Array<T>();

    for await (const item of this)
        result.push(item);

    return result;
};

so my code

const array = new Array<string>();

for await (const page of readPages(...))
    array.push(page);

const result = array.join(pagesDelimiter);

becomes

const array = await readPages(...).toPromise();
const result = array.join(pagesDelimiter);

Yeah, and I'm aware, that prototyping is questionable. But it was interesting, how to prototype async iterator :-).

Upvotes: 1

Related Questions