katja's git: oeffisearch

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 
export const sleep         = delay => new Promise((resolve) => setTimeout(resolve, delay));
export const isEmptyObject = obj   => Object.keys(obj).length === 0;
export const padZeros      = str   => (('00' + str).slice(-2));

export const setThemeColor        = color           => document.querySelector('meta[name="theme-color"]').setAttribute('content', color);
export const queryBackgroundColor = (target, query) => window.getComputedStyle(target.querySelector(query)).getPropertyValue('background-color');

export class CustomDate extends Date {
	formatDate()        { return `${this.getDate()}.${this.getMonth() + 1}.${this.getFullYear()}`; }
	formatISODate()     { return `${this.getFullYear()}-${padZeros(this.getMonth()+1)}-${padZeros(this.getDate())}`; }
	formatISODateTime() { return `${this.formatISODate()}T${this.formatTime()}`; }
	formatTime()        { return `${padZeros(this.getHours())}:${padZeros(this.getMinutes())}`; }

	setDate(value) {
		const splitValue = value.split('-');
		if (splitValue.length !== 3) return false;
		this.setFullYear(splitValue[0], splitValue[1]-1, splitValue[2]);
	}

	setTime(value) {
		const splitValue = value.split(':');
		if (splitValue.length !== 2) return false;
		this.setHours(splitValue[0], splitValue[1]);
	}

	setDateTime(value) {
		const splitValue = value.split('T');
		if (splitValue.length !== 2) return false;
		this.setDate(splitValue[0]);
		this.setTime(splitValue[1]);
	}
}