ctucx.git: trainsearch

web based trip-planner, fork of https://cyberchaos.dev/yuka/trainsearch

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
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;
};