UNPKG

apisearch

Version:
423 lines (422 loc) 12.5 kB
"use strict"; exports.__esModule = true; exports.Item = void 0; var tslib_1 = require("tslib"); var InvalidFormatError_1 = require("../Error/InvalidFormatError"); var Coordinate_1 = require("./Coordinate"); var ItemUUID_1 = require("./ItemUUID"); var AppUUID_1 = require("./AppUUID"); var IndexUUID_1 = require("./IndexUUID"); /** * Item class */ var Item = /** @class */ (function () { /** * Constructor * * @param uuid * @param coordinate * @param metadata * @param indexedMetadata * @param searchableMetadata * @param exactMatchingMetadata * @param suggest */ function Item(uuid, coordinate, metadata, indexedMetadata, searchableMetadata, exactMatchingMetadata, suggest) { this.metadata = {}; this.indexedMetadata = {}; this.searchableMetadata = {}; this.exactMatchingMetadata = []; this.suggest = []; this.highlights = {}; this.promoted = false; this.uuid = uuid; this.coordinate = coordinate; this.metadata = metadata; this.indexedMetadata = indexedMetadata; this.searchableMetadata = searchableMetadata; this.exactMatchingMetadata = exactMatchingMetadata; this.suggest = suggest; } /** * Create new Item * * @param uuid * @param metadata * @param indexedMetadata * @param searchableMetadata * @param exactMatchingMetadata * @param suggest * @returns {Item} */ Item.create = function (uuid, metadata, indexedMetadata, searchableMetadata, exactMatchingMetadata, suggest) { if (metadata === void 0) { metadata = {}; } if (indexedMetadata === void 0) { indexedMetadata = {}; } if (searchableMetadata === void 0) { searchableMetadata = {}; } if (exactMatchingMetadata === void 0) { exactMatchingMetadata = []; } if (suggest === void 0) { suggest = []; } return new Item(uuid, null, metadata, indexedMetadata, searchableMetadata, exactMatchingMetadata, suggest); }; /** * Create new located Item * * @param uuid * @param coordinate * @param metadata * @param indexedMetadata * @param searchableMetadata * @param exactMatchingMetadata * @param suggest * @returns {Item} */ Item.createLocated = function (uuid, coordinate, metadata, indexedMetadata, searchableMetadata, exactMatchingMetadata, suggest) { if (metadata === void 0) { metadata = {}; } if (indexedMetadata === void 0) { indexedMetadata = {}; } if (searchableMetadata === void 0) { searchableMetadata = {}; } if (exactMatchingMetadata === void 0) { exactMatchingMetadata = []; } if (suggest === void 0) { suggest = []; } return new Item(uuid, coordinate, metadata, indexedMetadata, searchableMetadata, exactMatchingMetadata, suggest); }; /** * Get uuid * * @returns ItemUUID */ Item.prototype.getUUID = function () { return this.uuid; }; /** * Get id * * @returns string */ Item.prototype.getId = function () { return this.uuid.getId(); }; /** * Get type * * @returns string */ Item.prototype.getType = function () { return this.uuid.getType(); }; /** * Get coordinate * * @returns Coordinate|null */ Item.prototype.getCoordinate = function () { return this.coordinate; }; /** * Get distance * * @returns int */ Item.prototype.getDistance = function () { return this.distance; }; /** * Get metadata * * @returns Array */ Item.prototype.getMetadata = function () { return this.metadata; }; /** * Set metadata * * @param metadata */ Item.prototype.setMetadata = function (metadata) { this.metadata = metadata; }; /** * Add metadata * * @param key * @param value */ Item.prototype.addMetadata = function (key, value) { this.metadata[key] = value; }; /** * Get indexed metadata * * @returns Array */ Item.prototype.getIndexedMetadata = function () { return this.indexedMetadata; }; /** * Set indexed metadata * * @param indexedMetadata */ Item.prototype.setIndexedMetadata = function (indexedMetadata) { this.indexedMetadata = indexedMetadata; }; /** * Add indexed metadata * * @param key * @param value */ Item.prototype.addIndexedMetadata = function (key, value) { this.indexedMetadata[key] = value; }; /** * Get searchable metadata * * @returns Array */ Item.prototype.getSearchableMetadata = function () { return this.searchableMetadata; }; /** * Set searchable metadata * * @param searchableMetadata */ Item.prototype.setSearchableMetadata = function (searchableMetadata) { this.searchableMetadata = searchableMetadata; }; /** * Add searchable metadata * * @param key * @param value */ Item.prototype.addSearchableMetadata = function (key, value) { this.searchableMetadata[key] = value; }; /** * Get exactMatching metadata * * @returns Array */ Item.prototype.getExactMatchingMetadata = function () { return this.exactMatchingMetadata; }; /** * Set exactMatching metadata * * @param exactMatchingMetadata */ Item.prototype.setExactMatchingMetadata = function (exactMatchingMetadata) { this.exactMatchingMetadata = exactMatchingMetadata; }; /** * Add exactMatching metadata * * @param value */ Item.prototype.addExactMatchingMetadata = function (value) { this.exactMatchingMetadata.push(value); }; /** * Get all metadata * * @returns {{}} */ Item.prototype.getAllMetadata = function () { return tslib_1.__assign(tslib_1.__assign({}, this.metadata), this.indexedMetadata); }; /** * Get * * @param key * * @returns mixed|null */ Item.prototype.get = function (key) { var allMetadata = this.getAllMetadata(); return (typeof allMetadata[key] != "undefined") ? allMetadata[key] : null; }; /** * Get suggest * * @returns Array */ Item.prototype.getSuggest = function () { return this.suggest; }; /** * Get highlights * * @returns Array */ Item.prototype.getHighlights = function () { return this.highlights; }; /** * Get highlight * * @param key * * @return string|null */ Item.prototype.getHighlight = function (key) { return (typeof this.highlights[key] != "undefined") ? this.highlights[key] : null; }; /** * Is promoted * * @returns boolean */ Item.prototype.isPromoted = function () { return this.promoted; }; /** * Set score * * @param score * * @return {Item} */ Item.prototype.setScore = function (score) { this.score = score; return this; }; /** * Get score * * @return {number} */ Item.prototype.getScore = function () { return this.score; }; /** * Set appUUID * * @return {AppUUID} */ Item.prototype.getAppUUID = function () { return this.appUUID; }; /** * Set indexUUID * * @return {IndexUUID} */ Item.prototype.getIndexUUID = function () { return this.indexUUID; }; /** * To array */ Item.prototype.toArray = function () { var itemAsArray = { uuid: this.uuid.toArray() }; if (this.coordinate instanceof Coordinate_1.Coordinate) { itemAsArray.coordinate = this.coordinate.toArray(); } if (Object.keys(this.metadata).length > 0) { itemAsArray.metadata = this.metadata; } if (Object.keys(this.indexedMetadata).length > 0) { itemAsArray.indexed_metadata = this.indexedMetadata; } if (Object.keys(this.searchableMetadata).length > 0) { itemAsArray.searchable_metadata = this.searchableMetadata; } if (this.exactMatchingMetadata.length > 0) { itemAsArray.exact_matching_metadata = this.exactMatchingMetadata; } if (this.suggest.length > 0) { itemAsArray.suggest = this.suggest; } if (Object.keys(this.highlights).length > 0) { itemAsArray.highlights = this.highlights; } if (this.isPromoted()) { itemAsArray.is_promoted = true; } if (typeof this.distance != "undefined") { itemAsArray.distance = this.distance; } if (typeof this.score != "undefined") { itemAsArray.score = this.score; } if (typeof this.appUUID != "undefined") { itemAsArray.app_uuid = this.appUUID.toArray(); } if (typeof this.indexUUID != "undefined") { itemAsArray.index_uuid = this.indexUUID.toArray(); } return itemAsArray; }; /** * Create from array * * @param array */ Item.createFromArray = function (array) { array = JSON.parse(JSON.stringify(array)); if (typeof array.uuid != "object") { throw InvalidFormatError_1.InvalidFormatError.itemUUIDRepresentationNotValid(); } if (typeof array.coordinate != "undefined" && typeof array.coordinate != "object") { throw InvalidFormatError_1.InvalidFormatError.coordinateFormatNotValid(); } var item = (typeof array.coordinate == "object" && array.coordinate != null) ? Item.createLocated(ItemUUID_1.ItemUUID.createFromArray(array.uuid), Coordinate_1.Coordinate.createFromArray(array.coordinate), ((typeof array.metadata == "undefined") ? {} : array.metadata), ((typeof array.indexed_metadata == "undefined") ? {} : array.indexed_metadata), ((typeof array.searchable_metadata == "undefined") ? {} : array.searchable_metadata), ((typeof array.exact_matching_metadata == "undefined") ? [] : array.exact_matching_metadata), ((typeof array.suggest == "undefined") ? [] : array.suggest)) : Item.create(ItemUUID_1.ItemUUID.createFromArray(array.uuid), ((typeof array.metadata == "undefined") ? {} : array.metadata), ((typeof array.indexed_metadata == "undefined") ? {} : array.indexed_metadata), ((typeof array.searchable_metadata == "undefined") ? {} : array.searchable_metadata), ((typeof array.exact_matching_metadata == "undefined") ? [] : array.exact_matching_metadata), ((typeof array.suggest == "undefined") ? [] : array.suggest)); if (typeof array.distance != "undefined" && array.distance != null) { item.distance = array.distance; } if (typeof array.highlights == "object" && array.highlights != null) { item.highlights = array.highlights; } if (typeof array.is_promoted != "undefined" && array.is_promoted != null) { item.promoted = array.is_promoted; } if (typeof array.score != "undefined" && array.score != null) { item.score = array.score; } if (typeof array.app_uuid != "undefined" && array.app_uuid != null) { item.appUUID = AppUUID_1.AppUUID.createFromArray(array.app_uuid); } if (typeof array.index_uuid != "undefined" && array.index_uuid != null) { item.indexUUID = IndexUUID_1.IndexUUID.createFromArray(array.index_uuid); } return item; }; /** * Compose uuid * * @returns string */ Item.prototype.composeUUID = function () { return this.uuid.composedUUID(); }; /** * Get path by field. * * @param field * * @returns {string} */ Item.getPathByField = function (field) { return (["id", "type"].indexOf(field) > -1) ? "uuid." + field : "indexed_metadata." + field; }; return Item; }()); exports.Item = Item;