summaryrefslogtreecommitdiff
path: root/history/modules/PathUtils.js
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2021-08-23 16:46:06 -0300
committerSebastian <sebasjm@gmail.com>2021-08-23 16:48:30 -0300
commit38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch)
tree453dbf70000cc5e338b06201af1eaca8343f8f73 /history/modules/PathUtils.js
parentf26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff)
downloadnode-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip
added web vendorsHEADmaster
Diffstat (limited to 'history/modules/PathUtils.js')
-rw-r--r--history/modules/PathUtils.js59
1 files changed, 59 insertions, 0 deletions
diff --git a/history/modules/PathUtils.js b/history/modules/PathUtils.js
new file mode 100644
index 0000000..e5abec7
--- /dev/null
+++ b/history/modules/PathUtils.js
@@ -0,0 +1,59 @@
+export function addLeadingSlash(path) {
+ return path.charAt(0) === '/' ? path : '/' + path;
+}
+
+export function stripLeadingSlash(path) {
+ return path.charAt(0) === '/' ? path.substr(1) : path;
+}
+
+export function hasBasename(path, prefix) {
+ return (
+ path.toLowerCase().indexOf(prefix.toLowerCase()) === 0 &&
+ '/?#'.indexOf(path.charAt(prefix.length)) !== -1
+ );
+}
+
+export function stripBasename(path, prefix) {
+ return hasBasename(path, prefix) ? path.substr(prefix.length) : path;
+}
+
+export function stripTrailingSlash(path) {
+ return path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path;
+}
+
+export function parsePath(path) {
+ let pathname = path || '/';
+ let search = '';
+ let hash = '';
+
+ const hashIndex = pathname.indexOf('#');
+ if (hashIndex !== -1) {
+ hash = pathname.substr(hashIndex);
+ pathname = pathname.substr(0, hashIndex);
+ }
+
+ const searchIndex = pathname.indexOf('?');
+ if (searchIndex !== -1) {
+ search = pathname.substr(searchIndex);
+ pathname = pathname.substr(0, searchIndex);
+ }
+
+ return {
+ pathname,
+ search: search === '?' ? '' : search,
+ hash: hash === '#' ? '' : hash
+ };
+}
+
+export function createPath(location) {
+ const { pathname, search, hash } = location;
+
+ let path = pathname || '/';
+
+ if (search && search !== '?')
+ path += search.charAt(0) === '?' ? search : `?${search}`;
+
+ if (hash && hash !== '#') path += hash.charAt(0) === '#' ? hash : `#${hash}`;
+
+ return path;
+}