@stryke/path
Version:
A package containing various utilities that expand the functionality of NodeJs's built-in `path` module
1 lines • 3.93 kB
Source Map (JSON)
{"version":3,"file":"common.mjs","names":[],"sources":["../src/common.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 { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport { correctPath, stripStars, withoutTrailingSlash } from \"./normalize\";\nimport { slash } from \"./slash\";\n\n/**\n * Get the common path from an array of paths\n *\n * @example\n * ```ts\n * commonPath(['/foo/bar/baz', '/foo/bar/qux', '/foo/bar/baz/quux']);\n * // returns '/foo/bar'\n *\n * commonPath(['C:/foo/bar/baz', 'C:/foo/bar/qux', 'C:/foo/bar/baz/quux']);\n * // returns 'C:/foo/bar'\n * ```\n *\n * @param paths - The array of paths\n * @returns The common path\n */\nexport function commonPath(paths: string[]): string {\n const [first = \"\", ...remaining] = paths.map(path =>\n correctPath(slash(path))\n );\n if (!first) {\n return \"\";\n }\n if (remaining.length === 0) {\n return first;\n }\n\n let endOfPrefix = first.split(\"/\").length;\n for (const path of remaining) {\n const compare = path.split(\"/\");\n for (let i = 0; i < endOfPrefix; i++) {\n if (compare[i] !== first.split(\"/\")[i]) {\n endOfPrefix = i;\n }\n }\n\n if (endOfPrefix === 0) {\n return \"\";\n }\n }\n\n return withoutTrailingSlash(first.split(\"/\").slice(0, endOfPrefix).join(\"/\"));\n}\n\nexport const findCommonPath = commonPath;\n\n/**\n * Find the base path from a string path/glob or an array of string paths/globs. If a string is provided, it is returned as the base path. If an array of strings is provided, the common path among them is returned. If the input is invalid or empty, \"/\" is returned.\n *\n * @example\n * ```ts\n * findBasePath('/foo/bar/baz');\n * // returns '/foo/bar/baz'\n *\n * findBasePath(['/foo/bar/baz', '/foo/bar/qux', '/foo/bar/baz/quux']);\n * // returns '/foo/bar'\n *\n * findBasePath(['C:/foo/bar/baz', 'C:/foo/bar/qux', 'C:/foo/bar/baz/quux']);\n * // returns 'C:/foo/bar'\n *\n * findBasePath(['foo/bar/**\\/baz', 'foo/bar/qux/*', 'foo/bar/baz/quux']);\n * // returns 'foo/bar'\n *\n * findBasePath([]);\n * // returns '/'\n * ```\n *\n * @param paths - The string or array of strings to find the base path from\n * @returns The base path\n */\nexport function findBasePath(paths: string | string[]): string {\n if (isSetString(paths)) {\n return paths;\n } else if (Array.isArray(paths) && paths.length > 0) {\n return commonPath(paths.map(path => stripStars(path)));\n }\n\n return \"/\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,WAAW,OAAyB;CAClD,MAAM,CAAC,QAAQ,IAAI,GAAG,aAAa,MAAM,KAAI,SAC3C,YAAY,MAAM,KAAK,CAAC,CACzB;AACD,KAAI,CAAC,MACH,QAAO;AAET,KAAI,UAAU,WAAW,EACvB,QAAO;CAGT,IAAI,cAAc,MAAM,MAAM,IAAI,CAAC;AACnC,MAAK,MAAM,QAAQ,WAAW;EAC5B,MAAM,UAAU,KAAK,MAAM,IAAI;AAC/B,OAAK,IAAI,IAAI,GAAG,IAAI,aAAa,IAC/B,KAAI,QAAQ,OAAO,MAAM,MAAM,IAAI,CAAC,GAClC,eAAc;AAIlB,MAAI,gBAAgB,EAClB,QAAO;;AAIX,QAAO,qBAAqB,MAAM,MAAM,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC,KAAK,IAAI,CAAC;;AAG/E,MAAa,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;AA0B9B,SAAgB,aAAa,OAAkC;AAC7D,KAAI,YAAY,MAAM,CACpB,QAAO;UACE,MAAM,QAAQ,MAAM,IAAI,MAAM,SAAS,EAChD,QAAO,WAAW,MAAM,KAAI,SAAQ,WAAW,KAAK,CAAC,CAAC;AAGxD,QAAO"}