@stryke/path
Version:
A package containing various utilities that expand the functionality of NodeJs's built-in `path` module
44 lines (42 loc) • 1.89 kB
JavaScript
import { slash } from "./slash.mjs";
//#region src/is-equal.ts
/**
* Check if two paths are equal.
*
* @example
* ```ts
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/index.ts");
* // returns true
* isEqual("/home/user/project/src/index.ts", "/home/user/project");
* // returns false
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/other");
* // returns false
* isEqual("/home/user/project/src/index.ts", "/home/user/other");
* // returns false
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/index.ts");
* // returns true
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/index.ts", { ignoreCase: true });
* // returns true
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/INDEX.TS", { ignoreCase: true });
* // returns true
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/INDEX.TS");
* // returns false
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/index.ts/");
* // returns true
* isEqual("/home/user/project/src/index.ts", "/home/user/project/src/index.ts/", { ignoreCase: true });
* // returns true
* ```
*
* @param path1 - The first path to compare.
* @param path2 - The second path to compare.
* @returns `true` if `path1` is equal to `path2`, otherwise `false`.
*/
function isEqual(path1, path2, options) {
const { ignoreCase = false } = options ?? {};
const normalizedPath1 = slash(path1.replaceAll(/\\/g, "/").replace(/\/*$/, ""));
const normalizedPath2 = slash(path2.replaceAll(/\\/g, "/").replace(/\/*$/, ""));
return ignoreCase && path1?.toLowerCase() === path2?.toLowerCase() || !ignoreCase && path1 === path2 || ignoreCase && normalizedPath1?.toLowerCase() === normalizedPath2?.toLowerCase() || !ignoreCase && normalizedPath1 === normalizedPath2;
}
//#endregion
export { isEqual };
//# sourceMappingURL=is-equal.mjs.map