UNPKG

mindee

Version:

Mindee Client Library for Node.js

81 lines (80 loc) 2.92 kB
import { cleanOutString } from "../../../v1/parsing/common/index.js"; import { GeneratedListField, GeneratedObjectField } from "../../../v1/parsing/generated/index.js"; import { StringField } from "../../../v1/parsing/standard/index.js"; export class GeneratedV1Prediction { constructor() { this.fields = new Map(); } toString() { let outStr = ""; const pattern = /^(\n*[ ]*)( {2}):/; this.fields.forEach((fieldValue, fieldName) => { let strValue = ""; if (fieldValue instanceof GeneratedListField && fieldValue.values.length > 0) { if (fieldValue.values[0] instanceof GeneratedObjectField) { strValue += fieldValue.values[0].toStringLevel(1).replace(pattern, "$1* :"); } else { strValue += fieldValue.values[0].toString().replace(pattern, "$1* :") + "\n"; } for (const subValue of fieldValue.values.slice(1)) { if (subValue instanceof GeneratedObjectField) { strValue += subValue.toStringLevel(1).replace(pattern, "$1* :"); } else { strValue += ` ${" ".repeat(fieldName.length + 2)}${subValue}\n`; } } strValue = strValue.trimEnd(); } else { strValue = fieldValue.toString(); } outStr += `:${fieldName}: ${strValue}\n`; }); return cleanOutString(outStr.trimEnd()); } /** * Returns a dictionary of all fields that aren't a collection. */ getSingleField() { const singleFields = new Map(); for (const [fieldName, fieldValue] of Object.entries(this.fields)) { if (fieldValue instanceof StringField) { singleFields.set(fieldName, fieldValue); } } return singleFields; } /** * Returns a dictionary of all array-like fields. */ getArrayFields() { const arrayFields = new Map(); for (const [fieldName, fieldValue] of Object.entries(this.fields)) { if (fieldValue instanceof GeneratedListField) { arrayFields.set(fieldName, fieldValue); } } return arrayFields; } /** * Returns a dictionary of all object-like fields. */ getObjectFields() { const objectFields = new Map(); for (const [fieldName, fieldValue] of Object.entries(this.fields)) { if (fieldValue instanceof GeneratedObjectField) { objectFields.set(fieldName, fieldValue); } } return objectFields; } /** * Lists names of all top-level field keys. */ listFieldNames() { return Object.keys(this.fields); } }