1 | export const hasBasename = (path, prefix) => {
|
2 | return (new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i')).test(path);
|
3 | };
|
4 | export const stripBasename = (path, prefix) => {
|
5 | return hasBasename(path, prefix) ? path.substr(prefix.length) : path;
|
6 | };
|
7 | export const stripTrailingSlash = (path) => {
|
8 | return path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path;
|
9 | };
|
10 | export const addLeadingSlash = (path) => {
|
11 | return path.charAt(0) === '/' ? path : '/' + path;
|
12 | };
|
13 | export const stripLeadingSlash = (path) => {
|
14 | return path.charAt(0) === '/' ? path.substr(1) : path;
|
15 | };
|
16 | export const stripPrefix = (path, prefix) => {
|
17 | return path.indexOf(prefix) === 0 ? path.substr(prefix.length) : path;
|
18 | };
|
19 | export const parsePath = (path) => {
|
20 | let pathname = path || '/';
|
21 | let search = '';
|
22 | let hash = '';
|
23 | const hashIndex = pathname.indexOf('#');
|
24 | if (hashIndex !== -1) {
|
25 | hash = pathname.substr(hashIndex);
|
26 | pathname = pathname.substr(0, hashIndex);
|
27 | }
|
28 | const searchIndex = pathname.indexOf('?');
|
29 | if (searchIndex !== -1) {
|
30 | search = pathname.substr(searchIndex);
|
31 | pathname = pathname.substr(0, searchIndex);
|
32 | }
|
33 | return {
|
34 | pathname,
|
35 | search: search === '?' ? '' : search,
|
36 | hash: hash === '#' ? '' : hash,
|
37 | query: {},
|
38 | key: ''
|
39 | };
|
40 | };
|
41 | export const createPath = (location) => {
|
42 | const { pathname, search, hash } = location;
|
43 | let path = pathname || '/';
|
44 | if (search && search !== '?') {
|
45 | path += (search.charAt(0) === '?' ? search : `?${search}`);
|
46 | }
|
47 | if (hash && hash !== '#') {
|
48 | path += (hash.charAt(0) === '#' ? hash : `#${hash}`);
|
49 | }
|
50 | return path;
|
51 | };
|
52 | export const parseQueryString = (query) => {
|
53 | if (!query) {
|
54 | return {};
|
55 | }
|
56 | return (/^[?#]/.test(query) ? query.slice(1) : query)
|
57 | .split('&')
|
58 | .reduce((params, param) => {
|
59 | let [key, value] = param.split('=');
|
60 | params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
|
61 | return params;
|
62 | }, {});
|
63 | };
|