# common-path-prefix

> Computes the longest prefix string that is common to each path, excluding the base component

Latest version **3.0.0** (published 2019-11-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install common-path-prefix
pnpm add common-path-prefix
yarn add common-path-prefix
bun add common-path-prefix
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2019-11-03 |
| First published | 2016-01-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Mark Wubben |
| Maintainers | novemberborn |
| Keywords | common, path, directory, dir, file, root, typescript, common prefix, common path, common path start, common root |

## Links

- npm: https://www.npmjs.com/package/common-path-prefix
- Repository: https://github.com/novemberborn/common-path-prefix
- Homepage: https://github.com/novemberborn/common-path-prefix#readme
- Issues: https://github.com/novemberborn/common-path-prefix/issues
- npm.io page: https://npm.io/package/common-path-prefix

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 3.0.0 (latest) — 2019-11-03
- 2.0.0 — 2019-08-28
- 1.0.0 — 2016-01-11

## README

# common-path-prefix

Computes the longest prefix string that is common to each path, excluding the base component. Tested with Node.js 8 and above.

## Installation

```console
npm install common-path-prefix
```

## Usage

The module has one default export, the `commonPathPrefix` function:

```js
const commonPathPrefix = require('common-path-prefix')
```

Call `commonPathPrefix()` with an array of paths (strings) and an optional separator character:

```js
const paths = ['templates/main.handlebars', 'templates/_partial.handlebars']

commonPathPrefix(paths, '/') // returns 'templates/'
```

If the separator is not provided the first `/` or `\` found in any of the paths is used. Otherwise the platform-default value is used:

```js
commonPathPrefix(['templates/main.handlebars', 'templates/_partial.handlebars']) // returns 'templates/'
commonPathPrefix(['templates\\main.handlebars', 'templates\\_partial.handlebars']) // returns 'templates\\'
```

You can provide any separator, for example:

```js
commonPathPrefix(['foo$bar', 'foo$baz'], '$') // returns 'foo$''
```

An empty string is returned if no common prefix exists:

```js
commonPathPrefix(['foo/bar', 'baz/qux']) // returns ''
commonPathPrefix(['foo/bar']) // returns ''
```

Note that the following *does* have a common prefix:

```js
commonPathPrefix(['/foo/bar', '/baz/qux']) // returns '/'
```

---
_Source: https://npm.io/package/common-path-prefix · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
