UNPKG

@stryke/path

Version:

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

1 lines 3.48 kB
{"version":3,"file":"is-equal.mjs","names":[],"sources":["../src/is-equal.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { slash } from \"./slash\";\n\nexport interface IsEqualOptions {\n /**\n * Whether to ignore case sensitivity when comparing paths.\n * @default false\n */\n ignoreCase?: boolean;\n}\n\n/**\n * Check if two paths are equal.\n *\n * @example\n * ```ts\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/index.ts\");\n * // returns true\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project\");\n * // returns false\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/other\");\n * // returns false\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/other\");\n * // returns false\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/index.ts\");\n * // returns true\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/index.ts\", { ignoreCase: true });\n * // returns true\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/INDEX.TS\", { ignoreCase: true });\n * // returns true\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/INDEX.TS\");\n * // returns false\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/index.ts/\");\n * // returns true\n * isEqual(\"/home/user/project/src/index.ts\", \"/home/user/project/src/index.ts/\", { ignoreCase: true });\n * // returns true\n * ```\n *\n * @param path1 - The first path to compare.\n * @param path2 - The second path to compare.\n * @returns `true` if `path1` is equal to `path2`, otherwise `false`.\n */\nexport function isEqual(\n path1: string,\n path2: string,\n options?: IsEqualOptions\n): boolean {\n const { ignoreCase = false } = options ?? {};\n\n const normalizedPath1 = slash(\n path1.replaceAll(/\\\\/g, \"/\").replace(/\\/*$/, \"\")\n );\n const normalizedPath2 = slash(\n path2.replaceAll(/\\\\/g, \"/\").replace(/\\/*$/, \"\")\n );\n\n return (\n (ignoreCase && path1?.toLowerCase() === path2?.toLowerCase()) ||\n (!ignoreCase && path1 === path2) ||\n (ignoreCase &&\n normalizedPath1?.toLowerCase() === normalizedPath2?.toLowerCase()) ||\n (!ignoreCase && normalizedPath1 === normalizedPath2)\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,SAAgB,QACd,OACA,OACA,SACS;CACT,MAAM,EAAE,aAAa,UAAU,WAAW,EAAE;CAE5C,MAAM,kBAAkB,MACtB,MAAM,WAAW,OAAO,IAAI,CAAC,QAAQ,QAAQ,GAAG,CACjD;CACD,MAAM,kBAAkB,MACtB,MAAM,WAAW,OAAO,IAAI,CAAC,QAAQ,QAAQ,GAAG,CACjD;AAED,QACG,cAAc,OAAO,aAAa,KAAK,OAAO,aAAa,IAC3D,CAAC,cAAc,UAAU,SACzB,cACC,iBAAiB,aAAa,KAAK,iBAAiB,aAAa,IAClE,CAAC,cAAc,oBAAoB"}