2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-12-21 07:39:51 +00:00
|
|
|
// Copyright the Browserify authors. MIT License.
|
|
|
|
|
2024-06-18 21:50:14 +00:00
|
|
|
import { resolve } from "@std/path/resolve";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { SEPARATOR } from "@std/path/constants";
|
2023-12-21 07:39:51 +00:00
|
|
|
import { toPathString } from "./_to_path_string.ts";
|
|
|
|
|
|
|
|
/**
|
2024-06-07 03:54:50 +00:00
|
|
|
* Checks whether `src` is a sub-directory of `dest`.
|
|
|
|
*
|
|
|
|
* @param src Source file path as a string or URL.
|
|
|
|
* @param dest Destination file path as a string or URL.
|
|
|
|
* @param sep Path separator. Defaults to `\\` for Windows and `/` for other
|
|
|
|
* platforms.
|
|
|
|
*
|
|
|
|
* @returns `true` if `src` is a sub-directory of `dest`, `false` otherwise.
|
2023-12-21 07:39:51 +00:00
|
|
|
*/
|
|
|
|
export function isSubdir(
|
|
|
|
src: string | URL,
|
|
|
|
dest: string | URL,
|
2024-01-18 05:54:39 +00:00
|
|
|
sep = SEPARATOR,
|
2023-12-21 07:39:51 +00:00
|
|
|
): boolean {
|
2024-06-18 21:50:14 +00:00
|
|
|
src = toPathString(src);
|
|
|
|
dest = toPathString(dest);
|
|
|
|
|
|
|
|
if (resolve(src) === resolve(dest)) {
|
2023-12-21 07:39:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-06-18 21:50:14 +00:00
|
|
|
|
2023-12-21 07:39:51 +00:00
|
|
|
const srcArray = src.split(sep);
|
|
|
|
const destArray = dest.split(sep);
|
2024-06-18 21:50:14 +00:00
|
|
|
|
2023-12-21 07:39:51 +00:00
|
|
|
return srcArray.every((current, i) => destArray[i] === current);
|
|
|
|
}
|