2020-05-14 09:06:01 -07:00
|
|
|
// transform /path/to/file.ext to file.ext
|
|
|
|
export function basename(path = '') {
|
|
|
|
return path ? path.replace(/^.*\//, '') : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform /path/to/file.ext to .ext
|
|
|
|
export function extname(path = '') {
|
|
|
|
const [_, ext] = /.+(\.[^.]+)$/.exec(path) || [];
|
|
|
|
return ext || '';
|
|
|
|
}
|
|
|
|
|
2021-05-12 11:36:53 -07:00
|
|
|
// join a list of path segments with slashes, ensuring no double slashes
|
|
|
|
export function joinPaths(...parts) {
|
|
|
|
let str = '';
|
|
|
|
for (const part of parts) {
|
|
|
|
if (!part) continue;
|
|
|
|
str = !str ? part : `${str.replace(/\/$/, '')}/${part.replace(/^\//, '')}`;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2020-05-14 09:06:01 -07:00
|
|
|
// test whether a variable is an object
|
|
|
|
export function isObject(obj) {
|
|
|
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns whether a dark theme is enabled
|
|
|
|
export function isDarkTheme() {
|
2021-09-27 07:47:44 -07:00
|
|
|
if (document.documentElement.classList.contains('theme-auto')) {
|
|
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
|
|
}
|
|
|
|
if (document.documentElement.classList.contains('theme-arc-green')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-05-14 09:06:01 -07:00
|
|
|
}
|
2020-05-20 19:00:43 -07:00
|
|
|
|
|
|
|
// removes duplicate elements in an array
|
|
|
|
export function uniq(arr) {
|
|
|
|
return Array.from(new Set(arr));
|
|
|
|
}
|
2020-07-26 23:24:09 -07:00
|
|
|
|
2020-10-21 04:02:24 -07:00
|
|
|
// strip <tags> from a string
|
|
|
|
export function stripTags(text) {
|
|
|
|
return text.replace(/<[^>]*>?/gm, '');
|
2020-07-26 23:24:09 -07:00
|
|
|
}
|
2021-03-18 16:43:43 -07:00
|
|
|
|
|
|
|
// searches the inclusive range [minValue, maxValue].
|
|
|
|
// credits: https://matthiasott.com/notes/write-your-media-queries-in-pixels-not-ems
|
|
|
|
export function mqBinarySearch(feature, minValue, maxValue, step, unit) {
|
|
|
|
if (maxValue - minValue < step) {
|
|
|
|
return minValue;
|
|
|
|
}
|
|
|
|
const mid = Math.ceil((minValue + maxValue) / 2 / step) * step;
|
|
|
|
if (matchMedia(`screen and (min-${feature}:${mid}${unit})`).matches) {
|
|
|
|
return mqBinarySearch(feature, mid, maxValue, step, unit); // feature is >= mid
|
|
|
|
}
|
|
|
|
return mqBinarySearch(feature, minValue, mid - step, step, unit); // feature is < mid
|
|
|
|
}
|