UNPKG

@stryke/path

Version:

A package containing various utilities that expand the functionality of NodeJs's built-in `path` module

33 lines (31 loc) 1.34 kB
import { slash } from "./slash.mjs"; //#region src/is-parent-path.ts /** * Check if a given path is a parent of another path. * * @example * ```ts * isParentPath("/home/user/project/src/index.ts", "/home/user/project/src"); * // returns true * isParentPath("/home/user/project/src/index.ts", "/home/user/project"); * // returns true * isParentPath("/home/user/project/src/index.ts", "/home/user/project/src/other"); * // returns false * isParentPath("/home/user/project/src/index.ts", "/home/user/other"); * // returns false * isParentPath("/home/user/project/src/index.ts", "/home/user/project/src/index.ts"); * // returns false * ``` * * @param childPath - The path to check if it is a child of the parent path. * @param parentPath - The path to check if it is a parent of the child path. * @returns `true` if `childPath` is a child of `parentPath`, otherwise `false`. */ function isParentPath(childPath, parentPath) { const normalizedChild = slash(childPath.replaceAll(/\\/g, "/").replace(/\/*$/, ""))?.toLowerCase(); const normalizedParent = slash(parentPath.replaceAll(/\\/g, "/").replace(/\/*$/, ""))?.toLowerCase(); return childPath !== parentPath && normalizedChild !== normalizedParent && normalizedChild.startsWith(`${normalizedParent}/`); } //#endregion export { isParentPath }; //# sourceMappingURL=is-parent-path.mjs.map