@scribelabsai/amazon-trp
Version:
Amazon Textract Response Parser library for Node.
40 lines • 1.16 kB
JavaScript
import { readFile } from 'fs/promises';
import { Page } from './Page';
export class Document {
constructor(blocks) {
this.pages = [];
this.blocks = blocks;
this.blockMap = {};
this.parse();
}
toString() {
return `\nDocument\n==========\n${this.pages.map((p) => `${p}`).join('\n\n')}`;
}
parse() {
let blocks = [];
this.blocks.forEach((b) => {
if (b.Id) {
this.blockMap[b.Id] = b;
}
if (b.BlockType === 'PAGE') {
if (blocks.length > 0) {
this.pages.push(new Page(blocks, this.blockMap));
}
blocks = [];
}
blocks.push(b);
});
if (blocks.length > 0) {
this.pages.push(new Page(blocks, this.blockMap));
}
}
getBlockById(blockId) {
return this.blockMap[blockId];
}
static async fromFile(file) {
const content = await readFile(file, { encoding: 'utf8' });
const blocks = JSON.parse(content);
return new Document(blocks);
}
}
//# sourceMappingURL=Document.js.map