diff --git a/src/utils/commons.js b/src/utils/commons.js index 5c77f32..c95ee2c 100644 --- a/src/utils/commons.js +++ b/src/utils/commons.js @@ -922,3 +922,16 @@ export const buildTree = (list, keyMap = { rootKeys: [], ignoreKeys: [] }) => { return treeRoots; }; + +export const isValidUrl = (str) => { + if (typeof str !== "string") return false; + + try { + // If no protocol, prepend "http://" + const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(str); + new URL(hasProtocol ? str : `http://${str}`); + return true; + } catch { + return false; + } +}