mindee
Version:
Mindee Client Library for Node.js
105 lines (104 loc) • 4.27 kB
JavaScript
import { InferenceFields } from "./inferenceFields.js";
import { BaseField } from "./baseField.js";
export class ObjectField extends BaseField {
/**
* Retrieves the simple sub-fields in the object.
*
* @return {Map<string, SimpleField>} A map of field names to their corresponding `SimpleField` instances.
*/
get simpleFields() {
const result = new Map();
for (const [fieldName, fieldValue] of this.fields) {
if (fieldValue.constructor.name === "SimpleField") {
result.set(fieldName, fieldValue);
}
}
return result;
}
/**
* Retrieves the list sub-fields in the object.
*
* @return {Map<string, ListField>} A map of field names to their corresponding `ListField` instances.
*/
get listFields() {
const result = new Map();
for (const [fieldName, fieldValue] of this.fields) {
if (fieldValue.constructor.name === "ListField") {
result.set(fieldName, fieldValue);
}
}
return result;
}
/**
* Retrieves the object sub-fields in the object.
*
* @return {Map<string, ObjectField>} A map of field names to their corresponding `ObjectField` instances.
*/
get objectFields() {
const result = new Map();
for (const [fieldName, fieldValue] of this.fields) {
if (fieldValue.constructor.name === "ObjectField") {
result.set(fieldName, fieldValue);
}
}
return result;
}
/**
* Retrieves a SimpleField by its name if it exists and is of the correct type.
*
* @param {string} fieldName - The name of the field to retrieve.
* @return {SimpleField} The SimpleField instance if it exists and is valid, or undefined if not.
* @throws {Error} If the field does not exist or is not of type SimpleField.
*/
getSimpleField(fieldName) {
if (!this.fields.has(fieldName) && !this.simpleFields.has(fieldName)) {
throw new Error(`The field '${fieldName}' was not found.`);
}
if (this.fields.get(fieldName)?.constructor.name !== "SimpleField") {
throw new Error(`The field '${fieldName}' is not a SimpleField.`);
}
return this.simpleFields.get(fieldName);
}
/**
* Retrieves a ListField by its name if it exists and is of the correct type.
*
* @param {string} fieldName - The name of the field to retrieve.
* @return {ListField} The ListField instance if it exists and is valid, or undefined if not.
* @throws {Error} If the field does not exist or is not of type ListField.
*/
getListField(fieldName) {
if (!this.fields.has(fieldName) || !this.listFields.has(fieldName)) {
throw new Error(`The field '${fieldName}' was not found.`);
}
if (this.fields.get(fieldName)?.constructor.name !== "ListField") {
throw new Error(`The field '${fieldName}' is not a ListField.`);
}
return this.listFields.get(fieldName);
}
/**
* Retrieves an ObjectField by its name if it exists and is of the correct type.
*
* @param {string} fieldName - The name of the field to retrieve.
* @return {ObjectField} The ObjectField instance if it exists and is valid, or undefined if not.
* @throws {Error} If the field does not exist or is not of type ObjectField.
*/
getObjectField(fieldName) {
if (!this.fields.has(fieldName) && !this.objectFields.has(fieldName)) {
throw new Error(`The field '${fieldName}' was not found.`);
}
if (this.fields.get(fieldName)?.constructor.name !== "ObjectField") {
throw new Error(`The field '${fieldName}' is not an ObjectField.`);
}
return this.objectFields.get(fieldName);
}
constructor(serverResponse, indentLevel = 0) {
super(serverResponse, indentLevel);
this.fields = new InferenceFields(serverResponse["fields"], this._indentLevel + 1);
}
toString() {
return "\n" + (this.fields ? this.fields.toString(1) : "");
}
toStringFromList() {
return this.fields ? this.fields.toString(2).substring(4) : "";
}
}