UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

147 lines (146 loc) 3.72 kB
/*! * NENT 2022 */ /* istanbul ignore file */ import { isValue } from '../../../../services/common/values'; /** * Ensures basename * @param path * @param prefix * @returns */ export function ensureBasename(path, prefix) { let result = hasBasename(path, prefix) ? path : `${prefix}/${path}`; result = result.replace(/\/{2,}/g, '/'); // stripTrailingSlash() return addLeadingSlash(result); } /** * Paths has basename * @param path * @param prefix */ export const hasBasename = (path, prefix = '/') => path.startsWith(prefix) || new RegExp(`^${prefix}(\\/|\\?|#|$)`, 'i').test(path); /** * Paths strip basename * @param path * @param prefix * @returns */ export const stripBasename = (path, prefix) => { let stripped = hasBasename(path, prefix) ? path.slice(prefix.length) : path; return addLeadingSlash(stripped); }; /** * Paths is filename * @param path */ export const isFilename = (path) => path.includes('.'); /** * Paths add leading slash * @param path */ export const addLeadingSlash = (path) => (path === null || path === void 0 ? void 0 : path.startsWith('/')) ? path : `/${path}`; /** * Paths strip leading slash * @param path */ export const stripLeadingSlash = (path) => (path === null || path === void 0 ? void 0 : path.startsWith('/')) ? path.slice(1) : path; /** * Parses path * @param [path] * @returns path */ export function parsePath(path = '/') { let pathname = path; let search = ''; let hash = ''; const hashIndex = pathname.indexOf('#'); if (hashIndex !== -1) { hash = pathname.slice(hashIndex); pathname = pathname.slice(0, Math.max(0, hashIndex)); } const searchIndex = pathname.indexOf('?'); if (searchIndex !== -1) { search = pathname.slice(searchIndex); pathname = pathname.slice(0, Math.max(0, searchIndex)); } return { pathname, search: search === '?' ? '' : search, hash: hash === '#' ? '' : hash, query: {}, key: '', params: {}, }; } /** * Creates path * @param location * @returns */ export function createPath(location) { const { pathname, search, hash } = location; let path = pathname || '/'; if (search && search !== '?') { path += (search === null || search === void 0 ? void 0 : search.startsWith('?')) ? search : `?${search}`; } if (hash && hash !== '#') { path += (hash === null || hash === void 0 ? void 0 : hash.startsWith('#')) ? hash : `#${hash}`; } return path; } /** * Parses query string * @param query * @returns */ export function parseQueryString(query) { if (!query) { return {}; } return (/^[?#]/.test(query) ? query.slice(1) : query) .split('&') .reduce((parameters, parameter) => { const [key, value] = parameter.split('='); parameters[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : ''; return parameters; }, {}); } /** * Turn a URL path to an array of possible parent-routes * * '/home/profile' -> ['/','/home', '/home/profile'] */ export function getPossibleParentPaths(path) { if (!isValue(path)) return []; let workingPath = path.endsWith('/') ? path.slice(0, path.length - 1) : path.slice(); const results = [path.slice()]; let index = workingPath.lastIndexOf('/'); while (index > 0) { workingPath = workingPath.substr(0, index); results.push(workingPath.slice()); index = workingPath.lastIndexOf('/'); } if (path != '/') results.push('/'); return results.reverse(); } /** * Get the direct parent path */ export function getParentPath(path) { if (!isValue(path)) return null; const parents = getPossibleParentPaths(path); if (parents.length >= 2) return parents.reverse()[1]; return null; }