summaryrefslogtreecommitdiff
path: root/node_modules/swup/src/modules/resolveUrl.ts
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2024-08-30 17:21:18 +0800
committerlittleblack111 <littleblack11111@gmail.com>2024-08-30 17:21:18 +0800
commitefbc2ffe6a1ee5ec38a4396305561d6515d0943e (patch)
tree2b4c94f18f2c792dfda813050755d5c0273fae22 /node_modules/swup/src/modules/resolveUrl.ts
parent43acceb75957c880a5ba5a31aae221394a36d5f6 (diff)
node fixes, updates
Diffstat (limited to 'node_modules/swup/src/modules/resolveUrl.ts')
-rwxr-xr-xnode_modules/swup/src/modules/resolveUrl.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/node_modules/swup/src/modules/resolveUrl.ts b/node_modules/swup/src/modules/resolveUrl.ts
new file mode 100755
index 00000000..a687d6fe
--- /dev/null
+++ b/node_modules/swup/src/modules/resolveUrl.ts
@@ -0,0 +1,33 @@
+import type Swup from '../Swup.js';
+
+/**
+ * Utility function to validate and run the global option 'resolveUrl'
+ * @param {string} url
+ * @returns {string} the resolved url
+ */
+export function resolveUrl(this: Swup, url: string): string {
+ if (typeof this.options.resolveUrl !== 'function') {
+ console.warn(`[swup] options.resolveUrl expects a callback function.`);
+ return url;
+ }
+ const result = this.options.resolveUrl(url);
+ if (!result || typeof result !== 'string') {
+ console.warn(`[swup] options.resolveUrl needs to return a url`);
+ return url;
+ }
+ if (result.startsWith('//') || result.startsWith('http')) {
+ console.warn(`[swup] options.resolveUrl needs to return a relative url`);
+ return url;
+ }
+ return result;
+}
+
+/**
+ * Compares the resolved version of two paths and returns true if they are the same
+ * @param {string} url1
+ * @param {string} url2
+ * @returns {boolean}
+ */
+export function isSameResolvedUrl(this: Swup, url1: string, url2: string): boolean {
+ return this.resolveUrl(url1) === this.resolveUrl(url2);
+}