mindee
Version:
Mindee Client Library for Node.js
118 lines (117 loc) • 5.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NutritionFactsLabelV1Document = void 0;
const common_1 = require("../../parsing/common");
const nutritionFactsLabelV1ServingSize_1 = require("./nutritionFactsLabelV1ServingSize");
const nutritionFactsLabelV1Calorie_1 = require("./nutritionFactsLabelV1Calorie");
const nutritionFactsLabelV1TotalFat_1 = require("./nutritionFactsLabelV1TotalFat");
const nutritionFactsLabelV1SaturatedFat_1 = require("./nutritionFactsLabelV1SaturatedFat");
const nutritionFactsLabelV1TransFat_1 = require("./nutritionFactsLabelV1TransFat");
const nutritionFactsLabelV1Cholesterol_1 = require("./nutritionFactsLabelV1Cholesterol");
const nutritionFactsLabelV1TotalCarbohydrate_1 = require("./nutritionFactsLabelV1TotalCarbohydrate");
const nutritionFactsLabelV1DietaryFiber_1 = require("./nutritionFactsLabelV1DietaryFiber");
const nutritionFactsLabelV1TotalSugar_1 = require("./nutritionFactsLabelV1TotalSugar");
const nutritionFactsLabelV1AddedSugar_1 = require("./nutritionFactsLabelV1AddedSugar");
const nutritionFactsLabelV1Protein_1 = require("./nutritionFactsLabelV1Protein");
const nutritionFactsLabelV1Sodium_1 = require("./nutritionFactsLabelV1Sodium");
const nutritionFactsLabelV1Nutrient_1 = require("./nutritionFactsLabelV1Nutrient");
const standard_1 = require("../../parsing/standard");
/**
* Nutrition Facts Label API version 1.0 document data.
*/
class NutritionFactsLabelV1Document {
constructor(rawPrediction, pageId) {
/** The amount of nutrients in the product. */
this.nutrients = [];
this.addedSugars = new nutritionFactsLabelV1AddedSugar_1.NutritionFactsLabelV1AddedSugar({
prediction: rawPrediction["added_sugars"],
pageId: pageId,
});
this.calories = new nutritionFactsLabelV1Calorie_1.NutritionFactsLabelV1Calorie({
prediction: rawPrediction["calories"],
pageId: pageId,
});
this.cholesterol = new nutritionFactsLabelV1Cholesterol_1.NutritionFactsLabelV1Cholesterol({
prediction: rawPrediction["cholesterol"],
pageId: pageId,
});
this.dietaryFiber = new nutritionFactsLabelV1DietaryFiber_1.NutritionFactsLabelV1DietaryFiber({
prediction: rawPrediction["dietary_fiber"],
pageId: pageId,
});
rawPrediction["nutrients"] &&
rawPrediction["nutrients"].map((itemPrediction) => this.nutrients.push(new nutritionFactsLabelV1Nutrient_1.NutritionFactsLabelV1Nutrient({
prediction: itemPrediction,
pageId: pageId,
})));
this.protein = new nutritionFactsLabelV1Protein_1.NutritionFactsLabelV1Protein({
prediction: rawPrediction["protein"],
pageId: pageId,
});
this.saturatedFat = new nutritionFactsLabelV1SaturatedFat_1.NutritionFactsLabelV1SaturatedFat({
prediction: rawPrediction["saturated_fat"],
pageId: pageId,
});
this.servingPerBox = new standard_1.AmountField({
prediction: rawPrediction["serving_per_box"],
pageId: pageId,
});
this.servingSize = new nutritionFactsLabelV1ServingSize_1.NutritionFactsLabelV1ServingSize({
prediction: rawPrediction["serving_size"],
pageId: pageId,
});
this.sodium = new nutritionFactsLabelV1Sodium_1.NutritionFactsLabelV1Sodium({
prediction: rawPrediction["sodium"],
pageId: pageId,
});
this.totalCarbohydrate = new nutritionFactsLabelV1TotalCarbohydrate_1.NutritionFactsLabelV1TotalCarbohydrate({
prediction: rawPrediction["total_carbohydrate"],
pageId: pageId,
});
this.totalFat = new nutritionFactsLabelV1TotalFat_1.NutritionFactsLabelV1TotalFat({
prediction: rawPrediction["total_fat"],
pageId: pageId,
});
this.totalSugars = new nutritionFactsLabelV1TotalSugar_1.NutritionFactsLabelV1TotalSugar({
prediction: rawPrediction["total_sugars"],
pageId: pageId,
});
this.transFat = new nutritionFactsLabelV1TransFat_1.NutritionFactsLabelV1TransFat({
prediction: rawPrediction["trans_fat"],
pageId: pageId,
});
}
/**
* Default string representation.
*/
toString() {
let nutrientsSummary = "";
if (this.nutrients && this.nutrients.length > 0) {
const nutrientsColSizes = [13, 22, 10, 13, 6];
nutrientsSummary += "\n" + (0, common_1.lineSeparator)(nutrientsColSizes, "-") + "\n ";
nutrientsSummary += "| Daily Value ";
nutrientsSummary += "| Name ";
nutrientsSummary += "| Per 100g ";
nutrientsSummary += "| Per Serving ";
nutrientsSummary += "| Unit ";
nutrientsSummary += "|\n" + (0, common_1.lineSeparator)(nutrientsColSizes, "=");
nutrientsSummary += this.nutrients.map((item) => "\n " + item.toTableLine() + "\n" + (0, common_1.lineSeparator)(nutrientsColSizes, "-")).join("");
}
const outStr = `:Serving per Box: ${this.servingPerBox}
:Serving Size: ${this.servingSize.toFieldList()}
:Calories: ${this.calories.toFieldList()}
:Total Fat: ${this.totalFat.toFieldList()}
:Saturated Fat: ${this.saturatedFat.toFieldList()}
:Trans Fat: ${this.transFat.toFieldList()}
:Cholesterol: ${this.cholesterol.toFieldList()}
:Total Carbohydrate: ${this.totalCarbohydrate.toFieldList()}
:Dietary Fiber: ${this.dietaryFiber.toFieldList()}
:Total Sugars: ${this.totalSugars.toFieldList()}
:Added Sugars: ${this.addedSugars.toFieldList()}
:Protein: ${this.protein.toFieldList()}
:sodium: ${this.sodium.toFieldList()}
:nutrients: ${nutrientsSummary}`.trimEnd();
return (0, common_1.cleanOutString)(outStr);
}
}
exports.NutritionFactsLabelV1Document = NutritionFactsLabelV1Document;