Skip to content

Commit

Permalink
Refactored(utility.js): use regular expression to match file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
h2ls committed Jan 16, 2024
1 parent 83083b5 commit f9258da
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/BootstrapBlazor/wwwroot/modules/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function copyTextUsingDOM(str) {
tempElem.setAttribute("style", "-webkit-user-select: text !important");
let spanParent = tempElem;
if (tempElem.attachShadow) {
spanParent = tempElem.attachShadow({ mode: "open" });
spanParent = tempElem.attachShadow({mode: "open"});
}
const span = document.createElement("span");
span.innerText = str;
Expand Down Expand Up @@ -284,34 +284,34 @@ const removeLink = href => {
* 自动识别 css 或者 js 链接并添加到 head
* @param {string[]} fileList
*/
const autoAdd = async (fileList) => {
const autoAdd = (fileList) => {
const promises = fileList.map(async (item) => {
const extension = item.split('.').pop();
const extension = item.match(/\.(\w+)(\?|$)/)[1];
if (extension === 'js') {
return addScript(item);
} else if (extension === 'css') {
return addLink(item);
}
});

await Promise.all(promises);
return Promise.all(promises);
}

/**
* 自动识别 css 或者 js 链接并从 head 中移除
* @param {string[]} fileList
*/
const autoRemove = async (fileList) => {
const autoRemove = (fileList) => {
const promises = fileList.map(async (item) => {
const extension = item.split('.').pop();
const extension = item.match(/\.(\w+)(\?|$)/)[1];
if (extension === 'js') {
return removeScript(item);
} else if (extension === 'css') {
return removeLink(item);
}
});

await Promise.all(promises);
return Promise.all(promises);
}

const insertBefore = (element, newEl) => {
Expand Down Expand Up @@ -460,7 +460,7 @@ const getTransitionDurationFromElement = (element) => {
}

// Get transition-duration of the element
let { transitionDuration, transitionDelay } = window.getComputedStyle(element)
let {transitionDuration, transitionDelay} = window.getComputedStyle(element)

const floatTransitionDuration = Number.parseFloat(transitionDuration)
const floatTransitionDelay = Number.parseFloat(transitionDelay)
Expand Down Expand Up @@ -582,8 +582,7 @@ const debounce = function (fn, duration = 200, callback = null) {
handler = setTimeout(() => {
handler = null
}, duration)
}
else {
} else {
handler = setTimeout(() => {
fn.apply(this, arguments)
}, duration)
Expand All @@ -598,8 +597,7 @@ export function openUrl(url, target = '_blank', features = null) {
export function runEval(code) {
try {
return eval(code);
}
catch (e) {
} catch (e) {
console.warn(e.message);
return e.message;
}
Expand All @@ -609,8 +607,7 @@ export function runFunction(code, arg) {
try {
var func = new Function(code);
return func(...arg);
}
catch (e) {
} catch (e) {
console.warn(e.message);
return e.message;
}
Expand Down

0 comments on commit f9258da

Please sign in to comment.