UNPKG

idea-toolbox

Version:
101 lines (100 loc) 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AddressGeolocation = exports.Address = void 0; const resource_model_1 = require("./resource.model"); const countries_enum_1 = require("./countries.enum"); class Address extends resource_model_1.Resource { load(x) { super.load(x); this.address = this.clean(x.address, String); if (x.address2) this.address2 = this.clean(x.address2, String); this.postcode = this.clean(x.postcode, String); this.city = this.clean(x.city, String); this.province = this.clean(x.province, String); this.country = this.clean(x.country, String); if (x.geolocation) this.geolocation = new AddressGeolocation(x.geolocation); if (x.contact) this.contact = this.clean(x.contact, String); if (x.phone) this.phone = this.clean(x.phone, String); if (x.email) this.email = this.clean(x.email, String); } validate() { const e = super.validate(); if (this.iE(this.address)) e.push('address'); if (this.iE(this.city)) e.push('city'); if (!Object.values(countries_enum_1.Countries).includes(this.country)) e.push('country'); if (this.phone && this.iE(this.phone, 'phone')) e.push('phone'); if (this.email && this.iE(this.email, 'email')) e.push('email'); return e; } /** * Get a string representing the formatted full address. */ getFullAddress(display = {}) { display = Object.assign({ address: true, address2: true, city: true, postcode: true, province: true, country: true }, display); let res = ''; if (this.address && this.address.trim() && display.address) res = res.concat(this.address.trim()); if (this.address2 && this.address2.trim() && display.address2) { if (res.length) res = res.concat(` (${this.address2.trim()})`); else res = res.concat(this.address2.trim()); } if (this.city && this.city.trim() && display.city) { if (res.length) res = res.concat(`, ${this.city.trim()}`); else res = res.concat(this.city.trim()); } if (this.postcode && this.postcode.trim() && display.postcode) { if (this.city && this.city.trim() && display.city) res = res.concat(` ${this.postcode.trim()}`); else if (res.length) res = res.concat(`, ${this.postcode.trim()}`); else res = res.concat(this.postcode.trim()); } if (this.province && this.province.trim() && display.province) { if (res.length) res = res.concat(` (${this.province.trim()})`); else res = res.concat(this.province.trim()); } if (this.country && this.country.trim() && display.country) { if (res.length) res = res.concat(` - ${this.country.trim()}`); else res = res.concat(this.country.trim()); } return res.trim(); } } exports.Address = Address; /** * An address' geolocation expressed in latitute and longitude. */ class AddressGeolocation extends resource_model_1.Resource { load(x) { super.load(x); this.lat = this.clean(x.lat, Number); this.lng = this.clean(x.lng, Number); } } exports.AddressGeolocation = AddressGeolocation;