-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
100 lines (88 loc) · 2.92 KB
/
Copy pathutils.js
File metadata and controls
100 lines (88 loc) · 2.92 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Utility functions for the Training Search Portal.
*/
const Utils = (() => {
'use strict';
function debounce(fn, delay) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
function escapeHtml(str) {
if (!str) return '';
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
function stripHtml(str) {
if (!str) return '';
return str.replace(/<[^>]*>/g, ' ').replace(/ /g, ' ').replace(/\s+/g, ' ').trim();
}
function highlightText(text, terms) {
if (!text || !terms || terms.length === 0) return escapeHtml(text);
const escaped = escapeHtml(text);
let result = escaped;
for (const term of terms) {
if (!term) continue;
const escapedTerm = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(`(${escapedTerm})`, 'gi');
result = result.replace(regex, '<mark>$1</mark>');
}
return result;
}
function formatDuration(durStr) {
if (!durStr) return '';
const dayMatch = durStr.match(/(\d+)\s*day/i);
const hourMatch = durStr.match(/(\d+)\s*hour/i);
const minMatch = durStr.match(/(\d+)\s*min(?:ute)?s?/i);
const parts = [];
if (dayMatch) {
const d = parseInt(dayMatch[1]);
parts.push(`${d} Day${d !== 1 ? 's' : ''}`);
}
if (hourMatch) {
const h = parseInt(hourMatch[1]);
if (h > 0) parts.push(`${h} Hour${h !== 1 ? 's' : ''}`);
}
if (minMatch) {
const m = parseInt(minMatch[1]);
if (m > 0) parts.push(`${m} Min`);
}
return parts.join(' ') || durStr;
}
function saveSettings(settings) {
try {
localStorage.setItem('trainingSearchSettings', JSON.stringify(settings));
} catch (e) { /* localStorage not available */ }
}
function loadSettings() {
try {
const data = localStorage.getItem('trainingSearchSettings');
return data ? JSON.parse(data) : null;
} catch (e) {
return null;
}
}
function getUrlParam(name) {
const params = new URLSearchParams(window.location.search);
return params.get(name);
}
function setUrlParams(params) {
const url = new URL(window.location);
for (const [key, value] of Object.entries(params)) {
if (value) {
url.searchParams.set(key, value);
} else {
url.searchParams.delete(key);
}
}
window.history.replaceState({}, '', url);
}
return {
debounce, escapeHtml, stripHtml, highlightText,
formatDuration, saveSettings, loadSettings,
getUrlParam, setUrlParams
};
})();