summaryrefslogtreecommitdiff
path: root/src/lib/relativePath.ts
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-13 20:33:03 +0100
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-13 20:33:03 +0100
commitf19fc78a151c761642cf7c9b654e0da9db3c4418 (patch)
tree4fb1c40c51151572ebd2c283e5ae8a6e819b83f1 /src/lib/relativePath.ts
parentf6d45f7b9374444ae1ab751a213d86827b64eb8f (diff)
More robust relative path handling
Diffstat (limited to 'src/lib/relativePath.ts')
-rw-r--r--src/lib/relativePath.ts12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/lib/relativePath.ts b/src/lib/relativePath.ts
new file mode 100644
index 0000000..ad8582c
--- /dev/null
+++ b/src/lib/relativePath.ts
@@ -0,0 +1,12 @@
+export default function (currentPath: string, relativePath: string) {
+ if (relativePath.length > 2 && relativePath.slice(0, 3) === '../') {
+ throw new Error('Cannot calculate relative path because it goes up a directory.');
+ }
+
+ if (currentPath.at(-1) !== '/') {
+ currentPath = currentPath + '/';
+ }
+ relativePath = relativePath.replace(/^\.?\//, '');
+
+ return currentPath + relativePath;
+}