UNPKG

apisearch

Version:
156 lines (155 loc) 4.38 kB
"use strict"; exports.__esModule = true; exports.Filter = exports.FILTER_TYPE_QUERY = exports.FILTER_TYPE_GEO = exports.FILTER_TYPE_DATE_RANGE = exports.FILTER_TYPE_RANGE = exports.FILTER_TYPE_FIELD = exports.FILTER_PROMOTE = exports.FILTER_EXCLUDE = exports.FILTER_AT_LEAST_ONE = exports.FILTER_MUST_ALL_WITH_LEVELS = exports.FILTER_MUST_ALL = void 0; /** * filter constants */ exports.FILTER_MUST_ALL = 4; exports.FILTER_MUST_ALL_WITH_LEVELS = 5; exports.FILTER_AT_LEAST_ONE = 8; exports.FILTER_EXCLUDE = 16; exports.FILTER_PROMOTE = 32; exports.FILTER_TYPE_FIELD = "field"; exports.FILTER_TYPE_RANGE = "range"; exports.FILTER_TYPE_DATE_RANGE = "date_range"; exports.FILTER_TYPE_GEO = "geo"; exports.FILTER_TYPE_QUERY = "query"; /** * Filter class */ var Filter = /** @class */ (function () { /** * Constructor * * @param field * @param values * @param applicationType * @param filterType * @param filterTerms */ function Filter(field, values, applicationType, filterType, filterTerms) { this.field = field; this.values = values; this.applicationType = applicationType; this.filterType = filterType; this.filterTerms = filterTerms; } /** * Get field * * @returns {string} */ Filter.prototype.getField = function () { return this.field; }; /** * Get values * * @returns {any} */ Filter.prototype.getValues = function () { return this.values; }; /** * Has value * * @param value * * @returns {boolean} */ Filter.prototype.hasValue = function (value) { return typeof this.values[value] == "undefined"; }; /** * getApplicationType * * @returns {number} */ Filter.prototype.getApplicationType = function () { return this.applicationType; }; /** * Get filter type * * @return {string} */ Filter.prototype.getFilterType = function () { return this.filterType; }; /** * Get filter type * * @return {{}} */ Filter.prototype.getFilterTerms = function () { return this.filterTerms; }; /** * Create * * @param field * @param values * @param applicationType * @param filterType * @param filterTerms * * @return {Filter} */ Filter.create = function (field, values, applicationType, filterType, filterTerms) { if (filterTerms === void 0) { filterTerms = []; } return new Filter(field, values, applicationType, filterType, filterTerms); }; /** * To array * * @returns {Array} */ Filter.prototype.toArray = function () { var filterAsArray = {}; if (this.field != "uuid.type") { filterAsArray.field = this.field; } if (this.values.length > 0 || Object.keys(this.values).length > 0) { filterAsArray.values = this.values; } if (this.applicationType != exports.FILTER_AT_LEAST_ONE) { filterAsArray.application_type = this.applicationType; } if (this.filterType != exports.FILTER_TYPE_FIELD) { filterAsArray.filter_type = this.filterType; } if (this.filterTerms.length > 0) { filterAsArray.filter_terms = this.filterTerms; } return filterAsArray; }; /** * Create from array * * @param array * * @returns {Filter} */ Filter.createFromArray = function (array) { array = JSON.parse(JSON.stringify(array)); if (typeof array.field == "undefined") { array.field = "uuid.type"; } if (typeof array.values == "undefined") { array.values = []; } if (typeof array.application_type == "undefined") { array.application_type = exports.FILTER_AT_LEAST_ONE; } if (typeof array.filter_type == "undefined") { array.filter_type = exports.FILTER_TYPE_FIELD; } if (typeof array.filter_terms == "undefined") { array.filter_terms = []; } return Filter.create(array.field, array.values, array.application_type, array.filter_type, array.filter_terms); }; return Filter; }()); exports.Filter = Filter;