mirror of
https://github.com/go-gitea/gitea.git
synced 2024-10-31 08:37:35 -04:00
dabc06d13b
This (short) PR builds upon #15028 and makes the file search case-insensitive. Previously, having a file named `TestFile.cs` would not be shown if `test` was typed in the search box. This now changes the matching function to be case-insensitive (without affecting the UI). The matching function, `strSubMatch`, is only used for this feature (it has been introduced by #15028), meaning that this PR does not affect the behaviour of any unrelated functionality of Gitea.
103 lines
3.3 KiB
JavaScript
103 lines
3.3 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};
|
|
}
|
|
|
|
// return the sub-match result as an array: [unmatched, matched, unmatched, matched, ...]
|
|
// res[even] is unmatched, res[odd] is matched, see unit tests for examples
|
|
export function strSubMatch(full, sub) {
|
|
const res = [''];
|
|
let i = 0, j = 0;
|
|
const subLower = sub.toLowerCase(), fullLower = full.toLowerCase();
|
|
while (i < subLower.length && j < fullLower.length) {
|
|
if (subLower[i] === fullLower[j]) {
|
|
if (res.length % 2 !== 0) res.push('');
|
|
res[res.length - 1] += full[j];
|
|
j++;
|
|
i++;
|
|
} else {
|
|
if (res.length % 2 === 0) res.push('');
|
|
res[res.length - 1] += full[j];
|
|
j++;
|
|
}
|
|
}
|
|
if (i !== sub.length) {
|
|
// if the sub string doesn't match the full, only return the full as unmatched.
|
|
return [full];
|
|
}
|
|
if (j < full.length) {
|
|
// append remaining chars from full to result as unmatched
|
|
if (res.length % 2 === 0) res.push('');
|
|
res[res.length - 1] += full.substring(j);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
// 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);
|
|
}
|