mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-04 10:30:19 -05:00
768e16dad1
This PR is for: * https://github.com/go-gitea/gitea/issues/20231 Now, when a user searches `word`, they always see `/{word}.txt` before `/{w}e-g{o}t-{r}esult.{d}at` Demo: When searching "a", "a.ext" comes first. Then when searching "at", the longer matched "template" comes first. <details> ![image](https://user-images.githubusercontent.com/2114189/194588738-3644d891-956f-40e4-b79b-b97d34265456.png) ![image](https://user-images.githubusercontent.com/2114189/194588797-9b124670-4e1e-4510-a170-780295ed89b8.png) </details> This PR also makes the frontend tests could import feature JS files by introducing `jestSetup.js` Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: silverwind <me@silverwind.io>
72 lines
2.4 KiB
JavaScript
72 lines
2.4 KiB
JavaScript
// 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 || '';
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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() {
|
|
const style = window.getComputedStyle(document.documentElement);
|
|
return style.getPropertyValue('--is-dark-theme').trim().toLowerCase() === 'true';
|
|
}
|
|
|
|
// removes duplicate elements in an array
|
|
export function uniq(arr) {
|
|
return Array.from(new Set(arr));
|
|
}
|
|
|
|
// strip <tags> from a string
|
|
export function stripTags(text) {
|
|
return text.replace(/<[^>]*>?/gm, '');
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
export function parseIssueHref(href) {
|
|
const path = (href || '').replace(/[#?].*$/, '');
|
|
const [_, owner, repo, type, index] = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path) || [];
|
|
return {owner, repo, type, index};
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// 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);
|
|
}
|