const loyaltyCards = { NONE: Symbol('no loyalty card'), BAHNCARD: Symbol('Bahncard'), VORTEILSCARD: Symbol('VorteilsCard'), HALBTAXABO: Symbol('HalbtaxAbo'), VOORDEELURENABO: Symbol('Voordeelurenabo'), SHCARD: Symbol('SH-Card'), GENERALABONNEMENT: Symbol('General-Abonnement'), }; const loyaltyCardsReverse = { 'Symbol(no loyalty card)': 'NONE', 'Symbol(Bahncard)': 'BAHNCARD', 'Symbol(VorteilsCard)': 'VORTEILSCARD', 'Symbol(HalbtaxAbo)': 'HALBTAXABO', 'Symbol(Voordeelurenabo)': 'VOORDEELURENABO', 'Symbol(SH-Card)': 'SHCARD', 'Symbol(General-Abonnement)': 'GENERALABONNEMENT', }; export const sleep = delay => new Promise((resolve) => setTimeout(resolve, delay)); export const ElementById = id => document.getElementById(id); export const showElement = element => element.classList.remove('hidden'); export const hideElement = element => element.classList.add('hidden'); export const elementHidden = element => element.classList.contains('hidden'); export const unflipElement = element => element.classList.remove('flipped'); export const flipElement = element => element.classList.add('flipped'); export const setThemeColor = color => document.querySelector('meta[name="theme-color"]').setAttribute('content', color); export const queryBackgroundColor = query => window.getComputedStyle(document.querySelector(query)).getPropertyValue('background-color'); export const isEmptyObject = (obj) => Object.keys(obj).length === 0; export const padZeros = str => { return ('00' + str).slice(-2); }; export const isValidDate = date => { const matches = /^(\d{4})[-\/](\d{2})[-\/](\d{2})$/.exec(date); if (matches == null) { return false; } const d = matches[3]; const m = matches[2]-1; const y = matches[1]; const composedDate = new Date(y, m, d); return composedDate.getDate() == d && composedDate.getMonth() == m && composedDate.getFullYear() == y; }; export const loyaltyCardToString = loyaltyCard => `${loyaltyCardsReverse[loyaltyCard.type.toString()]}-${loyaltyCard.discount}-${loyaltyCard.class}`; export const loyaltyCardFromString = string => { const splitedString = string.split('-'); if (splitedString[0] === 'NONE') return { type: loyaltyCards[splitedString[0]] }; return { type: loyaltyCards[splitedString[0]], discount: splitedString[1], class: splitedString[2] }; }; export const generateSlug = () => { const len = 8; let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (let i = 0; i < len; i++) result += characters.charAt(Math.floor(Math.random() * characters.length)); return result; };