UNPKG

@aurigma/design-atoms-model

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

152 lines 7.41 kB
import { BarcodeSubType } from "./BarcodeSubType"; import { BarcodeFormat } from "./BarcodeFormat"; import * as vcard from "vcard-parser"; import { equals, isEmpty } from "../../Utils/Utils"; import { arraysIsEqual } from "@aurigma/utils-js/algorithms/array"; import { VCardAddressType } from "../Interfaces/IBarcodeData"; export class VCardAddress { constructor(address = {}) { this.type = [VCardAddressType.none]; Object.assign(this, address); } get isEmpty() { return !this.POBox && !this.extendedAddress && !this.address && !this.locality && !this.state && !this.zipCode && !this.country; } equals(other) { return this.POBox === other.POBox && this.extendedAddress === other.extendedAddress && this.address === other.address && this.locality === other.locality && this.state === other.state && this.zipCode === other.zipCode && this.country === other.country; } toVCardValue() { return [ this.POBox, this.extendedAddress, this.address, this.locality, this.state, this.zipCode, this.country, ]; } static fromVCardValue(vCardAddress, meta) { const [POBox, extendedAddress, address, locality, state, zipCode, country] = vCardAddress; const type = Object.keys(meta).map((key) => VCardAddressType[key]); return new VCardAddress({ POBox, extendedAddress, address, state, zipCode, locality, country, type }); } } export class BarcodeData { get isEmpty() { return isEmpty(this.firstName) && isEmpty(this.lastName) && isEmpty(this.organization) && isEmpty(this.position) && isEmpty(this.email) && isEmpty(this.mobilePhone) && isEmpty(this.url) && isEmpty(this.phone) && isEmpty(this.data) && isEmpty(this.barcodeValue) && isEmpty(this.fax) && (this.addresses == null || this.addresses.length === 0); } constructor(rawData) { this.barcodeFormat = BarcodeFormat.QR_CODE; this.barcodeSubType = BarcodeSubType.None; this.addresses = []; if (typeof rawData === "string") { try { rawData = JSON.parse(rawData); } catch (e) { console.warn("Unable to parse BarcodeData from string ", rawData, e); rawData = null; } } if (typeof rawData === "object" && rawData != null) { //TODO Add a validation of raw objects. Object.assign(this, rawData); this.addresses = Array.isArray(rawData.addresses) ? rawData.addresses.map((address) => new VCardAddress(address)) : []; } } equals(other) { return other != null && this.barcodeFormat === other.barcodeFormat && this.barcodeSubType === other.barcodeSubType && this.firstName === other.firstName && this.lastName === other.lastName && this.organization === other.organization && this.position === other.position && this.email === other.email && this.mobilePhone === other.mobilePhone && this.url === other.url && this.data === other.data && this.phone === other.phone && this.barcodeValue === other.barcodeValue && this.fax === other.fax && arraysIsEqual(this.addresses, other.addresses, equals); } _makePunycodeUrl() { const withProtocol = this.url.startsWith("https://") || this.url.startsWith("http://"); if (!this.url) { return ""; } try { if (withProtocol) { return new URL(this.url).toString(); } else { return new URL(`https://${this.url}`).toString(); } } catch (error) { return this.url; } } toVCardString() { const vCard = { "n;CHARSET=UTF-8": [{ value: [this.lastName, this.firstName, "", "", ""] }], "email;internet": [{ value: this.email }], "org;CHARSET=UTF-8": [{ value: this.organization }], "tel;cell": [{ value: this.mobilePhone }], "tel;work": [{ value: this.phone }], "tel;fax": [{ value: this.fax }], "title;CHARSET=UTF-8": [{ value: this.position }], "url": [{ value: this._makePunycodeUrl() }], }; this.addresses.forEach((address) => { const types = address.type.map((type) => VCardAddressType[type]).join(";"); vCard[`adr;${types};CHARSET=UTF-8`] = [{ value: address.toVCardValue() }]; }); return vcard.generate(vCard); } static fromVCardString(value) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v; const vCard = value != null ? vcard.parse(value) : {}; const result = new BarcodeData(); result.barcodeFormat = BarcodeFormat.QR_CODE; result.barcodeSubType = BarcodeSubType.VCard; result.email = (_b = (_a = vCard.email) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.value; result.lastName = (_d = (_c = vCard.n) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.value[0]; result.firstName = (_f = (_e = vCard.n) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.value[1]; result.organization = (_h = (_g = vCard.org) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.value; result.mobilePhone = (_k = (_j = vCard.tel) === null || _j === void 0 ? void 0 : _j.find(item => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.meta) === null || _a === void 0 ? void 0 : _a.cell; })) === null || _k === void 0 ? void 0 : _k.value; result.phone = (_m = (_l = vCard.tel) === null || _l === void 0 ? void 0 : _l.find(item => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.meta) === null || _a === void 0 ? void 0 : _a.work; })) === null || _m === void 0 ? void 0 : _m.value; result.fax = (_p = (_o = vCard.tel) === null || _o === void 0 ? void 0 : _o.find(item => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.meta) === null || _a === void 0 ? void 0 : _a.fax; })) === null || _p === void 0 ? void 0 : _p.value; result.position = (_r = (_q = vCard.title) === null || _q === void 0 ? void 0 : _q[0]) === null || _r === void 0 ? void 0 : _r.value; result.url = (_t = (_s = vCard.url) === null || _s === void 0 ? void 0 : _s[0]) === null || _t === void 0 ? void 0 : _t.value; result.addresses = (_v = (_u = vCard.adr) === null || _u === void 0 ? void 0 : _u.map((adr) => VCardAddress.fromVCardValue(adr.value, adr.meta))) !== null && _v !== void 0 ? _v : []; return result; } } //# sourceMappingURL=BarcodeData.js.map