mindee
Version:
Mindee Client Library for Node.js
59 lines (58 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseField = void 0;
/**
* Base class for most fields.
*/
class BaseField {
/**
* @param {BaseFieldConstructor} constructor Constructor parameters.
*/
constructor({ prediction = {}, valueKey = "value", reconstructed = false, pageId = undefined, }) {
/** The value. */
this.value = undefined;
this.reconstructed = reconstructed;
if (prediction !== undefined &&
prediction !== null &&
valueKey in prediction &&
prediction[valueKey] !== null) {
this.value = prediction[valueKey];
this.pageId = pageId !== undefined ? pageId : prediction["page_id"];
}
}
compare(other) {
if (this.value === null && other.value === null)
return true;
if (this.value === null || other.value === null)
return false;
if (typeof this.value === "string" && typeof other.value === "string") {
return this.value.toLowerCase() === other.value.toLowerCase();
}
return this.value === other.value;
}
/**
* @param {BaseField[]} array - Array of Fields
* @returns {number} Sum of all the Fields values in the array
*/
static arraySum(array) {
let total = 0.0;
for (const field of array) {
if (typeof field.value !== "number")
return 0.0;
total += field.value;
if (isNaN(total))
return 0.0;
}
return total;
}
/**
* Default string representation.
*/
toString() {
if (this.value !== undefined) {
return `${this.value}`;
}
return "";
}
}
exports.BaseField = BaseField;