UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

80 lines (79 loc) • 2.46 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var extractPathFromURL_exports = {}; __export(extractPathFromURL_exports, { extractPathFromURL: () => extractPathFromURL }); module.exports = __toCommonJS(extractPathFromURL_exports); function extractExactPathFromURL(url) { if (url.match(/^https?:\/\//)) { const { origin, href } = new URL(url); return href.replace(origin, ""); } return fromDeepLink(url); } function fromDeepLink(url) { let res; try { res = new URL(url); } catch { if (url.startsWith("/")) { return url; } return url.replace(/^[^:]+:\/\//, ""); } let results = ""; if (res.host) { results += res.host; } if (res.pathname) { results += res.pathname; } const qs = !res.search ? "" : [...res.searchParams.entries()].map(([k, v]) => `${k}=${decodeURIComponent(v)}`).join("&"); if (qs) { results += "?" + qs; } return results; } function extractPathFromURL(_prefixes, url = "") { const pathFromPrefix = extractPathFromPrefix(_prefixes, url); if (pathFromPrefix !== void 0) { return pathFromPrefix.replace(/^\//, ""); } return extractExactPathFromURL(url).replace(/^\//, ""); } function extractPathFromPrefix(prefixes = [], url) { const prefix = getMatchingPrefix(prefixes, url); if (!prefix) return void 0; return url.slice(prefix.length); } const PREFIX_BOUNDARY_CHARS = [ "/", "?", "#" ]; function getMatchingPrefix(prefixes, url) { return prefixes.filter((prefix) => matchesPrefix(prefix, url)).sort((a, b) => b.length - a.length)[0]; } function matchesPrefix(prefix, url) { if (!url.startsWith(prefix)) return false; if (url.length === prefix.length) return true; if (prefix.endsWith("/")) return true; return PREFIX_BOUNDARY_CHARS.includes(url[prefix.length]); }