ctucx.git: oeffisearch

[nimlang] fast and simple tripplanner

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 
74 
'use strict';

export const ElementById = (id) => {
	return document.getElementById(id);
};

export const showDiv = (id) => {
	const element = document.getElementById(id);
	if (element) element.classList.remove('hidden');
};

export const hideDiv = (id) => {
	const element = document.getElementById(id);
	if (element) element.classList.add('hidden');
};

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 parseDateTime = (timestamp, format) => {
	const date = new Date(timestamp * 1000);

	if (format != null) {
		switch (format) {
			case 'full':
				return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()) + ', ' + date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear();
				break;

			case 'date':
				return date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear();
				break;

			default:
				return false;
				break;
		}

	}

	if (date.toLocaleDateString() !== new Date().toLocaleDateString()) {
		return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()) + ', ' + date.getDate() + '.' + (date.getMonth() + 1) + '.';
	} else {
		return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes());
	}
};

export const formatDuration = (duration) => {
	const mins = duration / 60;
	const h = Math.floor(mins / 60);
	const m = mins % 60;

	if (h > 0) {
		return h+'h '+m+'min';
	}

	return m+'min';
};