mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
c0f87db914
refactor(fs): resolve directories when checking whether they are the same
35 lines
981 B
TypeScript
35 lines
981 B
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// Copyright the Browserify authors. MIT License.
|
|
|
|
import { resolve } from "@std/path/resolve";
|
|
import { SEPARATOR } from "@std/path/constants";
|
|
import { toPathString } from "./_to_path_string.ts";
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
export function isSubdir(
|
|
src: string | URL,
|
|
dest: string | URL,
|
|
sep = SEPARATOR,
|
|
): boolean {
|
|
src = toPathString(src);
|
|
dest = toPathString(dest);
|
|
|
|
if (resolve(src) === resolve(dest)) {
|
|
return false;
|
|
}
|
|
|
|
const srcArray = src.split(sep);
|
|
const destArray = dest.split(sep);
|
|
|
|
return srcArray.every((current, i) => destArray[i] === current);
|
|
}
|