mindee
Version:
Mindee Client Library for Node.js
99 lines (98 loc) • 4.12 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ReceiptV5LineItem_instances, _ReceiptV5LineItem_printableValues;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReceiptV5LineItem = void 0;
const common_1 = require("../../parsing/common");
const geometry_1 = require("../../geometry");
/**
* List of all line items on the receipt.
*/
class ReceiptV5LineItem {
constructor({ prediction = {} }) {
_ReceiptV5LineItem_instances.add(this);
/** Confidence score */
this.confidence = 0.0;
/**
* Contains the relative vertices coordinates (points) of a polygon containing
* the field in the document.
*/
this.polygon = new geometry_1.Polygon();
this.description = prediction["description"];
if (prediction["quantity"] !== undefined &&
prediction["quantity"] !== null &&
!isNaN(prediction["quantity"])) {
this.quantity = +parseFloat(prediction["quantity"]);
}
else {
this.quantity = null;
}
if (prediction["total_amount"] !== undefined &&
prediction["total_amount"] !== null &&
!isNaN(prediction["total_amount"])) {
this.totalAmount = +parseFloat(prediction["total_amount"]);
}
else {
this.totalAmount = null;
}
if (prediction["unit_price"] !== undefined &&
prediction["unit_price"] !== null &&
!isNaN(prediction["unit_price"])) {
this.unitPrice = +parseFloat(prediction["unit_price"]);
}
else {
this.unitPrice = null;
}
this.pageId = prediction["page_id"];
this.confidence = prediction["confidence"] ? prediction.confidence : 0.0;
if (prediction["polygon"]) {
this.polygon = prediction.polygon;
}
}
/**
* Default string representation.
*/
toString() {
const printable = __classPrivateFieldGet(this, _ReceiptV5LineItem_instances, "m", _ReceiptV5LineItem_printableValues).call(this);
return ("Description: " +
printable.description +
", Quantity: " +
printable.quantity +
", Total Amount: " +
printable.totalAmount +
", Unit Price: " +
printable.unitPrice);
}
/**
* Output in a format suitable for inclusion in an rST table.
*/
toTableLine() {
const printable = __classPrivateFieldGet(this, _ReceiptV5LineItem_instances, "m", _ReceiptV5LineItem_printableValues).call(this);
return ("| " +
printable.description.padEnd(36) +
" | " +
printable.quantity.padEnd(8) +
" | " +
printable.totalAmount.padEnd(12) +
" | " +
printable.unitPrice.padEnd(10) +
" |");
}
}
exports.ReceiptV5LineItem = ReceiptV5LineItem;
_ReceiptV5LineItem_instances = new WeakSet(), _ReceiptV5LineItem_printableValues = function _ReceiptV5LineItem_printableValues() {
return {
description: this.description ?
this.description.length <= 36 ?
(0, common_1.cleanSpecialChars)(this.description) :
(0, common_1.cleanSpecialChars)(this.description).slice(0, 33) + "..." :
"",
quantity: this.quantity !== undefined ? (0, common_1.floatToString)(this.quantity) : "",
totalAmount: this.totalAmount !== undefined ? (0, common_1.floatToString)(this.totalAmount) : "",
unitPrice: this.unitPrice !== undefined ? (0, common_1.floatToString)(this.unitPrice) : "",
};
};