summaryrefslogtreecommitdiff
path: root/fs/utils.ts
diff options
context:
space:
mode:
authorAxetroy <troy450409405@gmail.com>2019-03-18 00:34:55 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-03-17 12:34:55 -0400
commit942df0be0d8bb37862195c438017df7746b0f3f0 (patch)
tree8d52e754c60944c7cc4fc825558b5f6d64deadc0 /fs/utils.ts
parent8acdecd72da494044065cb0615d9db3ce0a39a1c (diff)
extract internal method isSubdir to fs/utils.ts (denoland/deno_std#285)
Original: https://github.com/denoland/deno_std/commit/da4abcd9a3a5775939c3941a884d1c6f4d287d0f
Diffstat (limited to 'fs/utils.ts')
-rw-r--r--fs/utils.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/fs/utils.ts b/fs/utils.ts
new file mode 100644
index 000000000..dd7ee5fa4
--- /dev/null
+++ b/fs/utils.ts
@@ -0,0 +1,23 @@
+import * as path from "./path/mod.ts";
+
+/**
+ * Test whether or not `dest` is a sub-directory of `src`
+ * @param src src file path
+ * @param dest dest file path
+ * @param sep path separator
+ */
+export function isSubdir(
+ src: string,
+ dest: string,
+ sep: string = path.sep
+): boolean {
+ if (src === dest) {
+ return false;
+ }
+ const srcArray = src.split(sep);
+ const destArray = dest.split(sep);
+
+ return srcArray.reduce((acc, current, i) => {
+ return acc && destArray[i] === current;
+ }, true);
+}