UNPKG

apisearch

Version:
107 lines (106 loc) 2.99 kB
"use strict"; exports.__esModule = true; exports.ResultAggregations = void 0; var ResultAggregation_1 = require("./ResultAggregation"); /** * ResultAggregation class */ var ResultAggregations = /** @class */ (function () { /** * Constructor * * @param totalElements */ function ResultAggregations(totalElements) { this.aggregations = {}; this.totalElements = totalElements; } /** * Add aggregation * * @param name * @param aggregation */ ResultAggregations.prototype.addAggregation = function (name, aggregation) { this.aggregations[name] = aggregation; }; /** * Get aggregations * * @returns {{}} */ ResultAggregations.prototype.getAggregations = function () { return this.aggregations; }; /** * Get aggregation * * @param name * * @returns {Aggregation|null} */ ResultAggregations.prototype.getAggregation = function (name) { return this.aggregations[name] instanceof ResultAggregation_1.ResultAggregation ? this.aggregations[name] : null; }; /** * Has not empty aggregation * * @param name * * @returns {boolean} */ ResultAggregations.prototype.hasNotEmptyAggregation = function (name) { var aggregation = this.getAggregation(name); return (aggregation instanceof ResultAggregation_1.ResultAggregation) && (!aggregation.isEmpty()); }; /** * Get total elements * * @return {number} */ ResultAggregations.prototype.getTotalElements = function () { return this.totalElements; }; /** * To array * * @return {{total_elements?: number, aggregations?: {}}} */ ResultAggregations.prototype.toArray = function () { var aggregationCollection = {}; for (var i in this.aggregations) { aggregationCollection[i] = this.aggregations[i].toArray(); } var array = {}; if (this.totalElements > 0) { array.total_elements = this.totalElements; } if (Object.keys(aggregationCollection).length > 0) { array.aggregations = aggregationCollection; } return array; }; /** * Create from array * * @param array * * @return {ResultAggregations} */ ResultAggregations.createFromArray = function (array) { var aggregations = new ResultAggregations(typeof array.total_elements === "number" ? array.total_elements : 0); if (typeof array.aggregations === typeof {}) { for (var i in array.aggregations) { aggregations.addAggregation(i, ResultAggregation_1.ResultAggregation.createFromArray(array.aggregations[i])); } } return aggregations; }; return ResultAggregations; }()); exports.ResultAggregations = ResultAggregations;