https://github.com/prantlf/common-path-start
Detects the same starting part of the path in an array of file paths and returns it, its length, or it cuts it from the specified file paths.
https://github.com/prantlf/common-path-start
common cut path start
Last synced: 11 months ago
JSON representation
Detects the same starting part of the path in an array of file paths and returns it, its length, or it cuts it from the specified file paths.
- Host: GitHub
- URL: https://github.com/prantlf/common-path-start
- Owner: prantlf
- License: mit
- Created: 2018-05-14T10:44:11.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-05-27T06:57:31.000Z (about 4 years ago)
- Last Synced: 2025-06-16T11:08:34.692Z (12 months ago)
- Topics: common, cut, path, start
- Language: JavaScript
- Size: 158 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# common-path-start
[

](https://www.npmjs.com/package/common-path-start)
[](https://codecov.io/gh/prantlf/common-path-start)
Detects the same starting part of the path in an array of file paths and returns it, its length, or it cuts it from the specified file paths.
## Example
If you have the following file paths:
```text
snapshots/fun/hmpg.net.html
snapshots/fun/xkcd.com.html
snapshots/wikimedia.org.html
```
This plugin lets you detects "snapshots/" as the common path start, its length 10 characters, or cut the common start from every file path producing:
```text
fun/hmpg.net.html
fun/xkcd.com.html
wikimedia.org.html
```
## Installation
You need [node >= 6][node]. Install the package using your favourite package manager:
```sh
npm i common-path-start
pnpm i common-path-start
yarn add common-path-start
```
## Programmatic Usage
This module exports several static functions operating on file paths, which use forward slash as a separator.
```js
const {getCommonPathStart} = require('common-path-start')
const paths = [
'snapshots/fun/hmpg.net.html',
'snapshots/fun/xkcd.com.html',
'snapshots/wikimedia.org.html'
]
const commonPathStart = getCommonPathStart(paths) // "snapshots/"
```
### getCommonPathStart (paths) : string
Expects an array of strings with file paths and returns a string with the common path start, or an empty string, if there is no common starting directory among all file paths.
### getCommonPathLength (paths) : number
Expects an array of strings with file paths and returns the count of characters of the common path start, or zero, if there is no common starting directory among all file paths.
### cutCommonPathStart (paths) : array
Expects an array of strings with file paths and returns a new array with file paths with the common path start cut away, or the same file path array, if there is no common starting directory among all file paths.
## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style.
## License
Copyright (c) 2018-2022 Ferdinand Prantl
Licensed under the MIT license.
[node]: http://nodejs.org