Socket
Socket
Sign inDemoInstall

common-path-prefix

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 2.0.0

index.d.ts

52

index.js
'use strict'
function getDirectoryComponents (path, sep) {
var components = path.split(sep)
module.exports = function commonPathPrefix ([first, ...paths], sep) {
if (!sep) {
const m = /(\/|\\)/.exec(first)
if (!m) return '' // The first path did not contain any directory components. Bail now.
// Remove any trailing separators and the base component.
var last = ''
while (last === '') {
last = components.pop()
sep = m[0]
}
return components
}
const parts = first.split(sep)
module.exports = function commonPathPrefix (paths, sep) {
if (!sep) {
var m = /(\/|\\)/.exec(paths[0])
// The first path did not contain any directory components. Bail now.
if (!m) return ''
sep = m[0]
}
let prefix = parts.length
for (const p of paths) {
const compare = p.split(sep)
for (let i = 0; i < prefix; i++) {
if (compare[i] !== parts[i]) {
prefix = i
}
}
// Object to hold prefix strings formed of the directory components of each
// path. The value for each prefix string is the number of times that prefix
// occurred in the `paths` array.
var prefixes = Object.create(null)
for (var i = 0; i < paths.length; i++) {
var dirComponents = getDirectoryComponents(paths[i], sep)
var prefix = ''
for (var j = 0; j < dirComponents.length; j++) {
prefix += dirComponents[j] + sep
prefixes[prefix] = (prefixes[prefix] || 0) + 1
}
if (prefix === 0) break
}
// Find the prefixes that occurred for each path and sort them by length
// (longest first).
var common = Object.keys(prefixes).filter(function (prefix) {
return prefixes[prefix] === paths.length
}).sort(function (a, b) {
return b.length - a.length
})
// Return the longest common path prefix, or the empty string.
return common[0] || ''
return prefix === 0 ? '' : parts.slice(0, prefix).join(sep) + sep
}
{
"name": "common-path-prefix",
"version": "1.0.0",
"version": "2.0.0",
"description": "Computes the longest prefix string that is common to each path, excluding the base component",
"main": "index.js",
"files": [
"index.d.ts",
"index.js"
],
"scripts": {
"coverage": "nyc npm test",
"test": "ava",
"posttest": "standard"
"test": "standard && nyc ava"
},

@@ -24,7 +23,20 @@ "repository": {

"homepage": "https://github.com/novemberborn/common-path-prefix#readme",
"keywords": [
"common",
"path",
"directory",
"dir",
"file",
"root",
"typescript",
"common prefix",
"common path",
"common path start",
"common root"
],
"devDependencies": {
"ava": "^0.9.1",
"nyc": "^5.3.0",
"standard": "^5.4.1"
"ava": "^2.3.0",
"nyc": "^14.1.1",
"standard": "^14.0.2"
}
}
# common-path-prefix
Computes the longest prefix string that is common to each path, excluding the
base component. Tested with Node 0.10 and above.
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
```
npm install --save common-path-prefix
```

@@ -17,10 +16,9 @@ ## Usage

```js
var commonPathPrefix = require('common-path-prefix')
const commonPathPrefix = require('common-path-prefix')
```
Call `commonPathPrefix()` with an array of paths (strings) and an optional
separator character:
Call `commonPathPrefix()` with an array of paths (strings) and an optional separator character:
```js
var paths = ['templates/main.handlebars', 'templates/_partial.handlebars']
const paths = ['templates/main.handlebars', 'templates/_partial.handlebars']

@@ -30,4 +28,3 @@ commonPathPrefix(paths, '/') // returns 'templates/'

If the separator is not provided the first `/` or `\` found in the first path
string is used. This means the module works correctly no matter the platform:
If the separator is not provided the first `/` or `\` found in the first path string is used. This means the module works correctly no matter the platform:

@@ -34,0 +31,0 @@ ```js

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc