svelte-navigator-no-postinstall
Version:
Simple, accessible routing for Svelte
101 lines (88 loc) • 2.46 kB
JavaScript
export const paramRegex = /^:(.+)/;
export const substr = (str, start, end) => str.substr(start, end);
/**
* Check if `string` starts with `search`
* @param {string} string
* @param {string} search
* @return {boolean}
*/
export const startsWith = (string, search) =>
substr(string, 0, search.length) === search;
/**
* Check if `segment` is a root segment
* @param {string} segment
* @return {boolean}
*/
export const isRootSegment = segment => segment === "";
/**
* Check if `segment` is a dynamic segment
* @param {string} segment
* @return {boolean}
*/
export const isDynamic = segment => paramRegex.test(segment);
/**
* Check if `segment` is a splat
* @param {string} segment
* @return {boolean}
*/
export const isSplat = segment => segment[0] === "*";
/**
* Strip potention splat and splatname of the end of a path
* @param {string} str
* @return {string}
*/
export const stripSplat = str => str.replace(/\*.*$/, "");
/**
* Strip `str` of potential start and end `/`
* @param {string} str
* @return {string}
*/
export const stripSlashes = str => str.replace(/(^\/+|\/+$)/g, "");
/**
* Split up the URI into segments delimited by `/`
* @param {string} uri
* @return {string[]}
*/
export function segmentize(uri, filterFalsy = false) {
const segments = stripSlashes(uri).split("/");
return filterFalsy ? segments.filter(Boolean) : segments;
}
/**
* Add the query to the pathname if a query is given
* @param {string} pathname
* @param {string} [query]
* @return {string}
*/
export const addQuery = (pathname, query) =>
pathname + (query ? `?${query}` : "");
/**
* Combines the `basepath` and the `path` into one path.
* @param {string} basepath
* @param {string} path
*/
export function combinePaths(basepath, path) {
const barePath =
path === "/" ? basepath : `${stripSlashes(basepath)}/${stripSlashes(path)}`;
return `${stripSlashes(barePath)}/`;
}
/**
* Normalizes a basepath
*
* @param {string} path
* @returns {string}
*
* @example
* normalizePath("base/path/") // -> "/base/path"
*/
export const normalizePath = path => `/${stripSlashes(path)}`;
/**
* Joins and normalizes multiple path fragments
*
* @param {...string} pathFragments
* @returns {string}
*/
export function join(...pathFragments) {
const joinFragment = fragment => segmentize(fragment, true).join("/");
const joinedSegments = pathFragments.map(joinFragment).join("/");
return normalizePath(joinedSegments);
}