@stryke/path
Version:
A package containing various utilities that expand the functionality of NodeJs's built-in `path` module
33 lines (31 loc) • 1.42 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_slash = require('./slash.cjs');
//#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 = require_slash.slash(childPath.replaceAll(/\\/g, "/").replace(/\/*$/, ""))?.toLowerCase();
const normalizedParent = require_slash.slash(parentPath.replaceAll(/\\/g, "/").replace(/\/*$/, ""))?.toLowerCase();
return childPath !== parentPath && normalizedChild !== normalizedParent && normalizedChild.startsWith(`${normalizedParent}/`);
}
//#endregion
exports.isParentPath = isParentPath;