{"_id":"common-path","_rev":"993520","name":"common-path","description":"Finds the longest common parent directory path in an array of file path strings or custom objects. Also returns sub and base parts for each path.","dist-tags":{"latest":"1.0.1"},"maintainers":[{"name":"mdjermanovic","email":"milos.djermanovic@gmail.com"}],"time":{"modified":"2024-09-07T00:22:39.000Z","created":"2018-12-09T23:31:46.787Z","1.0.1":"2020-02-17T21:33:14.751Z","1.0.0":"2018-12-09T23:31:46.787Z"},"users":{},"author":{"name":"Milos Djermanovic","email":"milos.djermanovic@gmail.com"},"repository":{"type":"git","url":"git+https://github.com/mdjermanovic/common-path.git"},"versions":{"1.0.1":{"name":"common-path","version":"1.0.1","description":"Finds the longest common parent directory path in an array of file path strings or custom objects. Also returns sub and base parts for each path.","main":"index.js","scripts":{"test":"eslint . -f tap && nyc --reporter=lcov --reporter=text mocha","coveralls":"cat ./coverage/lcov.info | coveralls"},"author":{"name":"Milos Djermanovic","email":"milos.djermanovic@gmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/mdjermanovic/common-path.git"},"homepage":"https://github.com/mdjermanovic/common-path#readme","bugs":{"url":"https://github.com/mdjermanovic/common-path/issues"},"engines":{"node":">=6"},"devDependencies":{"chai":"^4.2.0","coveralls":"^3.0.2","eslint":"^5.10.0","eslint-plugin-prettier":"^3.0.0","js-combinatorics":"^0.5.4","mocha":"^5.2.0","nyc":"^13.1.0","prettier":"^1.15.3","upath":"^1.1.0"},"keywords":["path","paths","common","shared","parent","prefix","same","longest","max","deepest","dir","directory","folder","absolute","relative","unc","find","array","file","files","root","cross-platform","windows","posix"],"gitHead":"e52c5a5cf581cf24247a2192317835709a3680c1","_id":"common-path@1.0.1","_nodeVersion":"12.14.0","_npmVersion":"6.13.4","dist":{"integrity":"sha512-C8zvr4tVGRIJpbrh7WxeFZPvUkc2PHWx2IvxAUtuJCAiOLx0n6N4Xaab0C7wM+HDfXLqUQg7H9FoIjyxn/4IiA==","shasum":"308d00ed897b1ef58029c720d48d56cf1277d0f7","tarball":"http://registry-npm.ywwl.com/common-path/download/common-path-1.0.1.tgz","fileCount":5,"unpackedSize":20825,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeSwabCRA9TVsSAnZWagAAHa0P/26mf9sb3lDy9N9lF+Ny\nZS/wigu2WQpMyXJph2HVlzw1WJmYwfJfb/W9Z8JaQ/rGW0BYldLQuIPke++N\nbWmOfgLyGwHpiwxBOsiOJAF+Dc0Y0tgql043oPV/nyGSqYvD57oH+IG1Sd1k\n9vb0/BdBdFB8TyP4Mx/M/g5VDrYL5EHhitAOmPt41aaa7+njjd9XB/HqL1oV\nRD1bszVrl6uJaQ9yRIBOHPiTAxHFpixhWpPyQv6bMsMkZ7jXJURtO/6GEp8V\nmjWsqYxae5eNFlGixtYPpK7jLgd7o92TtdAWKce8Vr4D6LBBZhWLE00A+Gp7\nWD4J6j/p0sNg8CH7tB4W2bwNc91RRRS7W9yuLaCD3emOkiuJNr6NSVRc5Z9U\ncRsq6cVIqwiPu6/2U/jjhJ+1vzxsTsVSuCyLq44jks7QsVQ+NjK5V3en75y4\nL8jfphPtcPbixBd5o4e6ou7Qjrmd4SgpsaflZDdaSl/34HZ45mUhR+FOEe5K\nmU9rmQ4M3Q4GUv88JJGyK3MY/8iSPEMa75QbRVLi2lrQxT2sCGJgUY2aC5G4\nHsI2M9eyt+WE98G31g5gGnjZ11QERplHagjYEKRxiMxv1ul4UzzCH39r9g04\nwkRT5GAoLfdJ/4Q1QKckDg2Yr30CRDlK93ppNui9+LIgemnlUdNt7XyID0Rd\nSaJW\r\n=Jy/a\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDwfVNm3MLLe9vad6zJl4U598eLpklozwhY+wu1mKYtpwIhAJVjarfmS3CZGP76QhunV25XlA7KnxZU6MpqocYgbEaf"}],"size":6466,"noattachment":false,"key":"/common-path/-/common-path-1.0.1.tgz"},"maintainers":[{"name":"mdjermanovic","email":"milos.djermanovic@gmail.com"}],"_npmUser":{"name":"mdjermanovic","email":"milos.djermanovic@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/common-path_1.0.1_1581975194619_0.5900474260220132"},"_hasShrinkwrap":false,"publish_time":1581975194751,"_cnpm_publish_time":1581975194751},"1.0.0":{"name":"common-path","version":"1.0.0","description":"Finds the longest common parent directory path in an array of file path strings or custom objects. Also returns sub and base parts for each path.","main":"index.js","scripts":{"test":"eslint . -f tap && nyc --reporter=lcov --reporter=text mocha","coveralls":"cat ./coverage/lcov.info | coveralls"},"author":{"name":"Milos Djermanovic","email":"milos.djermanovic@gmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/mdjermanovic/common-path.git"},"homepage":"https://github.com/mdjermanovic/common-path#readme","bugs":{"url":"https://github.com/mdjermanovic/common-path/issues"},"engines":{"node":">=6"},"devDependencies":{"chai":"^4.2.0","coveralls":"^3.0.2","eslint":"^5.10.0","eslint-plugin-prettier":"^3.0.0","js-combinatorics":"^0.5.4","mocha":"^5.2.0","nyc":"^13.1.0","prettier":"^1.15.3","upath":"^1.1.0"},"keywords":["path","paths","common","shared","parent","prefix","same","longest","max","deepest","dir","directory","folder","absolute","relative","unc","find","array","file","files","root","cross-platform","windows","posix"],"gitHead":"23f830b4e619beea7776f2d42b5c47446edcc5de","_id":"common-path@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"8.12.0","_npmUser":{"name":"mdjermanovic","email":"milos.djermanovic@gmail.com"},"dist":{"integrity":"sha512-OwDYqK3VNKKczcDuUUT3LtJwyMh52z/CCwSDbK/ge0eLm96uGpzgTBgyXEzEu436AMwECQUXATEYGtMlT6dcQA==","shasum":"0fd678f4f55aadfda50c130659d581822f44418f","tarball":"http://registry-npm.ywwl.com/common-path/download/common-path-1.0.0.tgz","fileCount":4,"unpackedSize":20535,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcDaXjCRA9TVsSAnZWagAA1QkP/2ML+toIZS6jXnV16cUv\nO8ejHf9lUEgQWBfLua8EGqfhDMSPRi/1ZvVPonFyMxAaKcZE9vg4izw0Udoj\nC+9dUyMDzL/BnCAgFhDT0wGukWBh47/Dd6XCccWzndsDLtZ5C3E7WR5gj1dN\nSqgoDzT12jtqFi162f3HKL+U+Ea1OxxgOLuB5b0a2Sho/eL/g1XUA8UKQw2J\n4dU4vbNQKwo5IZKUnBezBkMPukHal38TAtid9saiRdbxx/h5U00kY9Rkc2Gw\n21r88DUdhJHuiNi7vCMdhepwMq5fQeV5wmlkpWAnhHUxIMPJeP1tJZwf5zUO\nDC7+gAIk80AqIIWR5tGQpRnYOvRuXlYbtaJ/zlswYqm5RbGNX6J3wSp4UsWl\naHjpt3+oUOIStrylFkHiQr+F05qINxqdUauWC0aLnupYPKKeKAlTl1BggxiV\ntN3yiAcH/yeEGJ0ycW7WC4JPUnIPpPen3OmkRrMTKzcIj45BJb0KtWDRk+P0\nOsf7OYm0WeSlIBhpnt4m1MaQv39ivR9cQ6i+7O5XrWzjgyt+B/fKi+vnpgVo\noRjb/ildnYuiKyY4gavirjA+kPianN0LUxJjhhULaKy8MPC07bJZ5p1ymeYk\nXeGUFygXEzuC/YOSX00kkRflU7JxedOZRvnx4XJqQzCZw7YcaplVtaDDrv9R\nRW7v\r\n=ODmO\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCeTbDWzDXGBn1ib1AJltv07XOudUnef5eT/Dg9cC4hxQIhAKMqFghCco5R34G43sjKsqoS5+gQKUMLved4TWwz1ioi"}],"size":6294,"noattachment":false,"key":"/common-path/-/common-path-1.0.0.tgz"},"maintainers":[{"name":"mdjermanovic","email":"milos.djermanovic@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/common-path_1.0.0_1544398306623_0.1922263922659735"},"_hasShrinkwrap":false,"publish_time":1544398306787,"_cnpm_publish_time":1544398306787}},"readme":"# common-path\n\n[![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/mdjermanovic/common-path/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/common-path.svg?style=flat)](https://www.npmjs.com/package/common-path) [![Build Status](https://travis-ci.org/mdjermanovic/common-path.svg?branch=master)](https://travis-ci.org/mdjermanovic/common-path) [![Coverage Status](https://coveralls.io/repos/github/mdjermanovic/common-path/badge.svg?branch=master)](https://coveralls.io/github/mdjermanovic/common-path?branch=master)\n\nFinds the longest common parent directory path in an array of file paths.\n\nAlso returns parsed paths with common, sub, base and other useful parts.\n\nFile paths can be: \n\n* Path strings. \n* Custom objects with a property whose value is path string.\n\nWorks well on POSIX and Windows, with absolute, relative, UNC, namespaced paths etc.\n\nAlso works well with third-party path libraries like [upath](https://www.npmjs.com/package/upath) (see API below).\n\nExample with POSIX absolute paths:\n\n```js\n/*\n   If you want to run this example on Windows, use commonPath.posix(paths)\n   If you use upath, then commonPath.custom(upath, paths)\n*/\n\nconst commonPath = require('common-path');\n\nconst paths = [\n  '/projects/myapp/src/util/one.js',\n  '/projects/myapp/test/fixtures/two.js',\n]\n\nconst common = commonPath(paths);\nconsole.log(common);\n/*\n  {\n    commonRoot: '/',\n    commonDir: '/projects/myapp',\n    parsedPaths: \n    [\n      {\n        original: '/projects/myapp/src/util/one.js',\n        subdir: 'src/util',\n        commonPart: '/projects/myapp/',\n        subPart: 'src/util/',\n        basePart: 'one.js',\n        namePart: 'one',\n        extPart: '.js',\n      },\n      {\n        original: '/projects/myapp/test/fixtures/two.js',\n        subdir: 'test/fixtures',\n        commonPart: '/projects/myapp/',\n        subPart: 'test/fixtures/',\n        basePart: 'two.js',\n        namePart: 'two',\n        extPart: '.js',\n      }\n    ]\n  }\n*/\n```\n\nSome quick notes, see API for details:\n\n* `commonPath()` does not resolve paths and expects normalized paths. It's probably best to use this\nlibrary on path strings generated by [path.resolve()](https://nodejs.org/api/path.html#path_path_resolve_paths),\nwhich returns normalized absolute paths.\n* If you pass an array of objects, `original` will be a reference to the original object. \n* If there is no common path, `commonRoot`, `commonDir` and all `subdir` values will be `null`. \nFor example, `C:\\a.js` and `D:\\a.js` don't have a common path. This will also happen if\nyou pass an array with different kinds of paths, e.g. absolute and relative paths in the same array.\n* To clarify the above, `commonDir` can be an empty string, which represents the current directory.\nThis can happen only if you pass an array of relative paths that don't have a longer common path.\n* `commonPart`, `subPart`, `basePart`, `namePart` and `extPart` are always `string` values (never `null`). These values are\nsliced from the original path string:\n  * `commonPart + subPart + basePart === original` (or `=== original[pathKey]` if you pass objects)\n  * `namePart + extPart === basePart`\n\nThis library correctly recognizes different kinds of paths and is aware of their specific rules, for instance:\n\n* `\\` is __not__ a common path for `\\\\server\\share\\a.js` and `\\b.js`. The first one is a UNC path.\n* `\\\\server` is __not__ a common path for `\\\\server\\share1\\a.js` and `\\\\server\\share2\\b.js`. UNC path without\na shared folder is not actually a path.\n* `C:` is __not__ a common path for `C:a.js` and `C:\\b.js`. The first one is a relative path.\n* `C:` __is__ a common path for `C:a.js` and `C:b.js`.\n* `\\\\?\\` is __not__ a common path for `\\\\?\\C:\\a.js` and `\\\\?\\D:\\b.js`. It's just a namespace prefix.\n* On POSIX, `\\` is __not__ a common path for `\\a.js` and `\\b.js`. It's a part of the filenames.\n\n## Installation\n\n```console\n$ npm install common-path\n```\n\nWorks on Node 6 and above.\n\n## API\n\n### Overview\n\n#### commonPath(paths, [pathKey])\n\nCross-platform function. \n\nUses [Node.js path module](https://nodejs.org/api/path.html) utilities, namely \n[path.parse()](https://nodejs.org/api/path.html#path_path_parse_path) and [path.sep](https://nodejs.org/api/path.html#path_path_sep).\n\nDepending on the local platform, `commonPath()` works like `commonPath.posix()` or `commonPath.windows()`.\n\n#### commonPath.posix(paths, [pathKey])\n\nPOSIX-specific function.\n\nCalls `parse()` and `sep` via [path.posix](https://nodejs.org/api/path.html#path_path_posix).\n\n#### commonPath.windows(paths, [pathKey])\n\nWindows-specific function.\n\nCalls `parse()` and `sep` via [path.win32](https://nodejs.org/api/path.html#path_path_win32).\n\n#### commonPath.custom(Path, paths, [pathKey])\n\nThe custom function can be used if you generate paths via a third-party library, e.g. upath:\n\n```js\nconst common = commonPath.custom(upath, paths);\n```\n\n`Path` is a custom replacement for Node's path module and should provide `parse()` and `sep`.\n\nBe aware that third-party libraries might not correctly parse certain kinds of paths (e.g. UNC paths).\nIf your application supports just 'usual' absolute or relative paths, everything should work fine.\n\n### commonPath(paths, [pathKey])\n\nEverything in this section applies to all four functions.\n\n#### paths\n\nArray of strings or objects, or even a mixed array of strings and objects.\n\nIf an element is a `string`, that element itself represents a file path.\n\nIf an element is an `object`, file path string will be read from its `pathKey` property.\n\nNotes:\n\n* This library does __not resolve__ paths. For example, `D:\\a\\b.js`, `\\a\\b.js` and `a\\b.js` \nmight all represent the same file, but they are seen as different kinds of paths,\nand different kinds of paths will always have `null` as the common path.\nIf your array can contain different kinds of paths (e.g. both absolute and relative), \nuse [path.resolve()](https://nodejs.org/api/path.html#path_path_resolve_paths) on all paths.\n* This library expects and works well with __normalized__ paths. \nUse [path.resolve()](https://nodejs.org/api/path.html#path_path_resolve_paths) as it returns normalized paths,\nor just [path.normalize()](https://nodejs.org/api/path.html#path_path_normalize_path) \nif your array contains only relative paths and you want to find the relative common path. \n* This library is designed to work with __file paths__. Directory paths will be treated as file paths.\nFor example, common path for `/projects/myapp` and `/projects/myapp/test` will be `/projects`.\n\n`commonPath()` will throw an exception if `paths` is not an `array`. \n\n`commonPath()` will throw an exception if `paths` array contains elements that are not `string` or `object`.\n\n#### pathKey\n\nIf you want to pass an array of custom objects that contain path strings, \nuse `pathKey `to specify the name of a property whose value represents the path string.\n\nThis optional parameter will be ignored if the `paths` array contains just strings.\n\nExample with objects, this time on Windows and with one file in the common directory:\n\n```js\n/*\n   If you want to run this example on POSIX, use commonPath.windows(paths)\n*/\n\nconst commonPath = require('common-path');\n\nconst obj1 = { filePath: 'C:\\\\lib\\\\hash.js' };\nconst obj2 = { filePath: 'C:\\\\lib\\\\encode\\\\url.js' };\n\nconst paths = [obj1, obj2];\n\nconst common = commonPath(paths, 'filePath');\n/*\n  {\n    commonRoot: 'C:\\\\',\n    commonDir: 'C:\\\\lib',\n    parsedPaths: \n    [\n      {\n        original: obj1, // reference to the same object\n        subdir: '',\n        commonPart: 'C:\\\\lib\\\\',\n        subPart: '',\n        basePart: 'hash.js',\n        namePart: 'hash',\n        extPart: '.js',\n      },\n      {\n        original: obj2, // reference to the same object\n        subdir: 'encode',\n        commonPart: 'C:\\\\lib\\\\',\n        subPart: 'encode\\\\',\n        basePart: 'url.js',\n        namePart: 'url',\n        extPart: '.js',\n      }\n    ]\n  }\n*/\n```\n\n`commonPath()` will throw an exception if `pathKey` is not a `string` or `undefined`. \n\n`commonPath()` will throw an exception if the `paths` array contains `object` elements and `pathKey` is `undefined`.\nIt will also throw an exception if at least one `object[pathKey]` is not a `string`.\n\n#### returns\n\nA structure as shown in the example.\n\nEach element in the `parsedPaths` array represents an element from the original `paths` array,\nin the same order.\n\n##### commonRoot\n\nRoot is the `root` value returned by [path.parse()](https://nodejs.org/api/path.html#path_path_parse_path).\n\nIf all paths have the same root, `commonRoot` will have that value.\n\nOtherwise, `commonRoot` will be `null`. Consequently, `commonDir` and all `subdir` values will be `null`.\n\nIf the `paths` array contains only relative paths, `commonRoot` will be an empty string\n(it can also be `C:` on Windows, if all paths are in the form of `C:a.js`, `C:b\\c.js` etc. these are also relative paths)\n\n##### commonDir\n\nPath to the longest common directory.\n\n`commonRoot` is a substring of `commonDir`.\n\n`commonDir` can have trailing separator only if it equals `commonRoot`, and `commonRoot` has trailing separator.\nFor example: `/`, `\\`, `C:\\`,  `\\\\server\\share\\`.\n\nIn all other cases, this string will not have trailing path separator.\n\n##### original\n\nOriginal path string, or a reference to the original object from the `paths` array.\n\n##### subdir\n\n`subdir` is a _relative_ path from the `commonDir` to the directory in which the file is.\n\nIt will be `null` if the `commonDir` is `null`.\n\nIt will be an empty string if the file is in the `commonDir`.\n\nThis string will never have a trailing path separator, provided that you pass normalized paths.\n\nThis string will also never have a leading path separator.\n\n##### commonPart, subPart, basePart\n\nThese strings are sliced directly from the original path string. They are never `null`.\n\n`commonPart` is `commonDir` plus a trailing separator if there is one in the original path string\nand the `commonDir` itself doesn't have a trailing separator.\n\n`basePart` is `base` returned by [path.parse()](https://nodejs.org/api/path.html#path_path_parse_path).\n\n`commonPart` is sliced from the start, `basePart` from the end. `subPart` is anything left between.\n\n`commonPart + subPart + basePart` always equals the original path string.\n\nThis holds even if the `commonDir` is `null`, in which case the `commonPart` will be an empty string:\n\n```js\n/*\n   If you want to run this example on POSIX, use commonPath.windows(paths)\n*/\n\nconst commonPath = require('common-path');\n\nconst paths = [\n  'C:\\\\a.js',\n  'D:\\\\a.js',\n]\n\nconst common = commonPath(paths);\nconsole.log(common);\n/*\n  {\n    commonRoot: null,\n    commonDir: null,\n    parsedPaths: \n    [\n      {\n        original: 'C:\\\\a.js',\n        subdir: null,\n        commonPart: '',\n        subPart: 'C:\\\\',\n        basePart: 'a.js',\n        namePart: 'a',\n        extPart: '.js',\n      },\n      {\n        original: 'D:\\\\a.js',\n        subdir: null,\n        commonPart: '',\n        subPart: 'D:\\\\',\n        basePart: 'a.js',\n        namePart: 'a',\n        extPart: '.js',\n      }\n    ]\n  }\n*/\n```\n\n##### namePart, extPart\n\n`namePart` and `extPart` are  `name` and `ext` values returned by [path.parse()](https://nodejs.org/api/path.html#path_path_parse_path).\n\n## Related\n\n[cross-path-sort](https://github.com/mdjermanovic/cross-path-sort) - Sort file paths\n\n## Author\n\n[Milos Djermanovic](https://github.com/mdjermanovic)\n\n## License\n\n[MIT License](https://github.com/mdjermanovic/common-path/blob/master/LICENSE)\n","_attachments":{},"homepage":"https://github.com/mdjermanovic/common-path#readme","bugs":{"url":"https://github.com/mdjermanovic/common-path/issues"},"license":"MIT"}