UNPKG

1.51 kBJavaScriptView Raw
1export var addLeadingSlash = function addLeadingSlash(path) {
2 return path.charAt(0) === '/' ? path : '/' + path;
3};
4
5export var stripLeadingSlash = function stripLeadingSlash(path) {
6 return path.charAt(0) === '/' ? path.substr(1) : path;
7};
8
9export var stripPrefix = function stripPrefix(path, prefix) {
10 return path.indexOf(prefix) === 0 ? path.substr(prefix.length) : path;
11};
12
13export var stripTrailingSlash = function stripTrailingSlash(path) {
14 return path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path;
15};
16
17export var parsePath = function parsePath(path) {
18 var pathname = path || '/';
19 var search = '';
20 var hash = '';
21
22 var hashIndex = pathname.indexOf('#');
23 if (hashIndex !== -1) {
24 hash = pathname.substr(hashIndex);
25 pathname = pathname.substr(0, hashIndex);
26 }
27
28 var searchIndex = pathname.indexOf('?');
29 if (searchIndex !== -1) {
30 search = pathname.substr(searchIndex);
31 pathname = pathname.substr(0, searchIndex);
32 }
33
34 pathname = decodeURI(pathname);
35
36 return {
37 pathname: pathname,
38 search: search === '?' ? '' : search,
39 hash: hash === '#' ? '' : hash
40 };
41};
42
43export var createPath = function createPath(location) {
44 var pathname = location.pathname,
45 search = location.search,
46 hash = location.hash;
47
48
49 var path = encodeURI(pathname || '/');
50
51 if (search && search !== '?') path += search.charAt(0) === '?' ? search : '?' + search;
52
53 if (hash && hash !== '#') path += hash.charAt(0) === '#' ? hash : '#' + hash;
54
55 return path;
56};
\No newline at end of file