|
|
@@ -1,4 +1,11 @@
|
|
|
-
|
|
|
+/**
|
|
|
+ * Encode specified `path`
|
|
|
+ * with [encodeURIComponent]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent}.
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {string}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function encodePagePath(path) {
|
|
|
const paths = path.split('/');
|
|
|
paths.forEach((item, index) => {
|
|
|
@@ -7,6 +14,14 @@ function encodePagePath(path) {
|
|
|
return paths.join('/');
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Encode specified `pages` with
|
|
|
+ * [encodeURIComponent]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent}.
|
|
|
+ *
|
|
|
+ * @param {Page[]} pages
|
|
|
+ * @returns {string}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function encodePagesPath(pages) {
|
|
|
pages.forEach((page) => {
|
|
|
if (!page.path) {
|
|
|
@@ -17,21 +32,47 @@ function encodePagesPath(pages) {
|
|
|
return pages;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @private
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {RegExpMatchArray}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function matchSlashes(path) {
|
|
|
// https://regex101.com/r/Z21fEd/5
|
|
|
return path.match(/^((\/+)?(.+?))(\/+)?$/);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {boolean}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function hasHeadingSlash(path) {
|
|
|
const match = matchSlashes(path);
|
|
|
return (match[2] != null);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {boolean}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function hasTrailingSlash(path) {
|
|
|
const match = matchSlashes(path);
|
|
|
return (match[4] != null);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {string}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function addHeadingSlash(path) {
|
|
|
if (path === '/') {
|
|
|
return path;
|
|
|
@@ -43,6 +84,12 @@ function addHeadingSlash(path) {
|
|
|
return path;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {string}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function addTrailingSlash(path) {
|
|
|
if (path === '/') {
|
|
|
return path;
|
|
|
@@ -54,6 +101,12 @@ function addTrailingSlash(path) {
|
|
|
return path;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {string}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function removeTrailingSlash(path) {
|
|
|
if (path === '/') {
|
|
|
return path;
|
|
|
@@ -63,6 +116,13 @@ function removeTrailingSlash(path) {
|
|
|
return match[1];
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * A short-hand method to add heading slash and remove trailing slash.
|
|
|
+ *
|
|
|
+ * @param {string} path
|
|
|
+ * @returns {string}
|
|
|
+ * @memberof pathUtils
|
|
|
+ */
|
|
|
function normalizePath(path) {
|
|
|
const match = matchSlashes(path);
|
|
|
if (match == null) {
|
|
|
@@ -71,6 +131,9 @@ function normalizePath(path) {
|
|
|
return `/${match[3]}`;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @namespace pathUtils
|
|
|
+ */
|
|
|
module.exports = {
|
|
|
encodePagePath,
|
|
|
encodePagesPath,
|