2020-05-14 21:36:01 +05:30
|
|
|
// 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-13 00:06:53 +05:30
|
|
|
// 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 21:36:01 +05:30
|
|
|
// 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-11-25 12:44:48 +05:30
|
|
|
const style = window.getComputedStyle(document.documentElement);
|
|
|
|
return style.getPropertyValue('--is-dark-theme').trim().toLowerCase() === 'true';
|
2020-05-14 21:36:01 +05:30
|
|
|
}
|
2020-05-21 07:30:43 +05:30
|
|
|
|
|
|
|
// removes duplicate elements in an array
|
|
|
|
export function uniq(arr) {
|
|
|
|
return Array.from(new Set(arr));
|
|
|
|
}
|
2020-07-27 11:54:09 +05:30
|
|
|
|
2020-10-21 16:32:24 +05:30
|
|
|
// strip <tags> from a string
|
|
|
|
export function stripTags(text) {
|
|
|
|
return text.replace(/<[^>]*>?/gm, '');
|
2020-07-27 11:54:09 +05:30
|
|
|
}
|
2021-03-19 05:13:43 +05:30
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-10-22 20:04:01 +05:30
|
|
|
|
|
|
|
export function parseIssueHref(href) {
|
|
|
|
const path = (href || '').replace(/[#?].*$/, '');
|
|
|
|
const [_, owner, repo, type, index] = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path) || [];
|
|
|
|
return {owner, repo, type, index};
|
|
|
|
}
|
2022-06-09 16:45:08 +05:30
|
|
|
|
2022-06-12 17:38:23 +05:30
|
|
|
// pretty-print a number using locale-specific separators, e.g. 1200 -> 1,200
|
|
|
|
export function prettyNumber(num, locale = 'en-US') {
|
|
|
|
if (typeof num !== 'number') return '';
|
|
|
|
const {format} = new Intl.NumberFormat(locale);
|
|
|
|
return format(num);
|
|
|
|
}
|
2022-08-23 18:28:04 +05:30
|
|
|
|
|
|
|
// parse a URL, either relative '/path' or absolute 'https://localhost/path'
|
|
|
|
export function parseUrl(str) {
|
|
|
|
return new URL(str, str.startsWith('http') ? undefined : window.location.origin);
|
|
|
|
}
|
2022-10-28 19:18:24 +05:30
|
|
|
|
|
|
|
// return current locale chosen by user
|
|
|
|
function getCurrentLocale() {
|
|
|
|
return document.documentElement.lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a month (0-11), returns it in the documents language
|
|
|
|
export function translateMonth(month) {
|
|
|
|
return new Date(Date.UTC(2022, month, 12)).toLocaleString(getCurrentLocale(), {month: 'short'});
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a weekday (0-6, Sunday to Saturday), returns it in the documents language
|
|
|
|
export function translateDay(day) {
|
|
|
|
return new Date(Date.UTC(2022, 7, day)).toLocaleString(getCurrentLocale(), {weekday: 'short'});
|
|
|
|
}
|