UNPKG

@publidata/utils-data-manager

Version:

Collection of methods to extract data from publidata

159 lines (138 loc) 3.24 kB
const { getName, getData, getId, getModel, getBlurb, getOpeningHours } = require("@publidata/utils-mapper"); const { cleanTimezone } = require("@publidata/utils-dates"); /** Class representing a publidata object. */ class PublidataObject { /** * Create a PublidataObject. * @param {Object} object - Object coming from Publidata */ constructor(object, instance, dataManager) { this.object = object; this.instance = instance; this.isPublidataObject = true; this.dataManager = dataManager; } /** Delete current Record key accessor and set a read-only value * @param {String} key - Accessor which will be deleted * @param {Any} value - Value of the record key * @return {Any} - new Property created */ memoize(key, value, writable = false) { Object.defineProperty(this, key, { value, writable }); return this[key]; } /** * @return {Number} - Object Id */ get id() { const id = getId(this.object); return this.memoize("id", id); } /** * @return {String} - Object Model */ get model() { const model = getModel(this.object, false, true); return this.memoize("model", model); } /** * @return {String} - Object Model without modification */ get rawModel() { const rawModel = getModel(this.object, false, false); return this.memoize("rawModel", rawModel); } /** * @return {String} - Object Name */ get name() { const name = getName(this.object); return this.memoize("name", name); } /** * @return {Object} - Object Source */ get _source() { return this.memoize("_source", this.data); } /** * @return {String} Object Type */ get _type() { return this.memoize("_type", this.object._type); } /** * @return {Array} Object Sort */ get sort() { return this.memoize("_sort", this.object.sort); } get data() { const data = getData(this.object); return this.memoize("data", data); } /** * @return {String} Object Blurb */ get blurb() { const blurb = getBlurb(this.object); return this.memoize("blurb", blurb); } /** * @return {String} */ get openingHours() { const openingHours = getOpeningHours(this.object) || ""; return this.memoize("openingHours", openingHours); } /** * @return {String} */ get openingHoursBlurb() { const openingHoursBlurb = this.object._source.opening_hours_blurb; return this.memoize("openingHoursBlurb", openingHoursBlurb); } /** * @return {String} */ get type() { const { type } = this.object._source; return this.memoize("type", type); } /** * @return {String} */ get status() { return this.memoize("status", this.object._source.service_status); } /** * @return {String} */ get lastUpdate() { return this.memoize( "lastUpdate", cleanTimezone(this.object._source.updated_at) ); } /** * @return {Array} - Object tags */ get tags() { const tags = this.data.tags || []; return this.memoize("tags", tags); } /** * @return {String} - Object code */ get code() { return this.memoize("code", this.tags[0]); } } module.exports = PublidataObject;