index.ts 5.02 KB
Newer Older
1
import * as cookies from 'es-cookie';
Maxym Borodenko's avatar
Maxym Borodenko committed
2
import * as _ from 'lodash';
markchkhotua's avatar
markchkhotua committed
3

Valeriy Panov's avatar
Valeriy Panov committed
4
5
import {log} from 'utilities/debug';

markchkhotua's avatar
markchkhotua committed
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
export function noTime(date: Date): Date {
	return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

export function firstOf(date: Date): Date {
	return new Date(date.getFullYear(), date.getMonth(), 1);
}

export function firstOfYear(date: Date): Date {
	return new Date(date.getFullYear(), 0, 1);
}

export function addMonths(date: Date, months: number): Date {
	const d: Date = new Date(date.valueOf());
	d.setMonth(d.getMonth() + months);
	return d;
}

export function addYears(date: Date, years: number): Date {
	const d: Date = new Date(date.valueOf());
	d.setFullYear(d.getFullYear() + years);
	return d;
}

export function addDays(date: Date, days: number): Date {
	const d: Date = new Date(date.valueOf());
	d.setDate(d.getDate() + days);
	return d;
}

export function fixDate(value: any): Date {
	if (value === null) {
		return null;
39
	} else if (('string' === typeof value)) {
markchkhotua's avatar
markchkhotua committed
40
		return new Date(value);
41
	} else if (('number' === typeof value)) {
42
		return new Date(value);
43
	} else {
markchkhotua's avatar
markchkhotua committed
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
		// Perhaps it's a date
		return value;
	}
}
/**
 * A very wordy comparison of two arrays
 */
export function arraysEqual(a: any[], b: any[]): boolean {
	if ((a === null || a === undefined) && (b === null || b === undefined)) {
		return true;
	} else if ((a === null || a === undefined) && (b !== null || b !== undefined)) {
		return false;
	} else if ((a !== null || a !== undefined) && (b === null || b === undefined)) {
		return false;
	} else if (a.length !== b.length) {
		return false;
	} else {
		for (let i = 0; i < a.length && i < b.length; i++) {
			if (a[i] !== b[i]) {
				return false;
			}
		}
		return true;
	}
}
69

Maxym Borodenko's avatar
Maxym Borodenko committed
70
71
72
73
74
75
76
77
78
79
80
/**
 *  Remove empty arrays and null filters
 */
export function cleanFilters(filter, keysToSkip?): string {
	const result: any = {};
	for (const k of Object.keys(filter).sort()) {
		if (keysToSkip && keysToSkip.indexOf(k) !== -1) {
			continue;
		}
		const val = filter[k];
		if (_.isObject(val)) {
Matija Obreza's avatar
Cleanup    
Matija Obreza committed
81
82
83
84
85
			if (_.isArray(val)) {
				const notUndefined = val.filter((x) => x !== undefined).sort();
				if (notUndefined && notUndefined.length > 0) {
					result[k] = notUndefined;
				}
Maxym Borodenko's avatar
Maxym Borodenko committed
86
87
88
89
90
91
92
93
94
95
96
97
98
			} else {
				const sub = cleanFilters(val, keysToSkip);
				if (Object.keys(sub).length) {
					result[k] = sub;
				}
			}
		} else if (val !== null) {
			result[k] = val;
		}
	}
	return result;
}

99
100
101
102
103
export function saveCookies(resp, expireOn: number) {
	const expDate = new Date(expireOn);
	log(`Saving cookies to expire on ${expDate}`);
	cookies.set('access_token', resp.access_token, { path: '/', expires: expDate });
	cookies.set('authorities', JSON.stringify(resp.authorities), { path: '/', expires: expDate });
104
105
}

Valeriy Panov's avatar
Valeriy Panov committed
106
export function clearCookies() {
Valeriy Panov's avatar
Valeriy Panov committed
107
	log('Clearing cookies');
Valeriy Panov's avatar
Valeriy Panov committed
108
	['authorities', 'access_token'].forEach((key) => cookies.remove(key));
109
}
110
111

export function isNumeric(value: any): boolean {
112
	return !isNaN(value - parseFloat(value));
113
}
114
115
116
117
118
119
120
121
122
123
124
125
126

/**
 * This function remaps reference-by-ID to full objects
 *
 * Examples:
 *  dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
 *  dereferenceReferences(paged.content, 'somethingById', (o) => new Foobar(o), 'id');
 *
 * @param content The list of objects
 * @param prop The property name to convert
 * @param conv Conversion function (e.g (o) => new Partner(o))
 * @param id The identifier property used as reference (string | number)
 */
127
128
export function dereferenceReferences(content: any[], prop: string, conv: (o: any) => any = (o) => o, id: string = 'uuid', refs: { [key: string]: any; } = {}) {
	// Convert cross-references to Partner object
Matija Obreza's avatar
Matija Obreza committed
129
130
131
	if (! content) {
		return;
	}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

	// deep search first
	content.forEach((entry) => {
		if (entry && typeof entry === 'object') {
			for (const p of Object.keys(entry)) {
				if (entry[p] && typeof entry[p] === 'object') {
					dereferenceReferences([ entry[p] ], prop, conv, id, refs);
				}
			}
		}
	});

	content.filter((entry) => entry && entry[prop] && typeof entry[prop] !== 'string')
		.map((entry) => entry[prop] = conv(entry[prop])).forEach((ref) => refs[ref[id]] = ref);

	content.filter((entry) => entry && entry[prop] && typeof entry[prop] === 'string')
		.forEach((entry) => entry[prop] = refs[entry[prop]]);

150
}
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177


export function insertAtCaret(element, myValue) {
  const doc = document as any;
  if (doc.selection) {
    // For browsers like Internet Explorer
    element.focus();
    const sel = doc.selection.createRange();
    sel.text = myValue;
    element.focus();

  } else if (element.selectionStart || element.selectionStart === '0') {
    // For browsers like Firefox and Webkit based
    const startPos = element.selectionStart;
    const endPos = element.selectionEnd;
    const scrollTop = element.scrollTop;
    element.value = element.value.substring(0, startPos) + myValue + element.value.substring(endPos, element.value.length);
    element.focus();
    element.selectionStart = startPos + myValue.length;
    element.selectionEnd = startPos + myValue.length;
    element.scrollTop = scrollTop;

  } else {
    element.value += myValue;
    element.focus();
  }
}