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 2.0.0 to 3.0.0

33

index.js
'use strict'
const { sep: DEFAULT_SEPARATOR } = require('path')
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.
sep = m[0]
const determineSeparator = paths => {
for (const path of paths) {
const match = /(\/|\\)/.exec(path)
if (match !== null) return match[0]
}
return DEFAULT_SEPARATOR
}
module.exports = function commonPathPrefix (paths, sep = determineSeparator(paths)) {
const [first = '', ...remaining] = paths
if (first === '' || remaining.length === 0) return ''
const parts = first.split(sep)
let prefix = parts.length
for (const p of paths) {
const compare = p.split(sep)
for (let i = 0; i < prefix; i++) {
let endOfPrefix = parts.length
for (const path of remaining) {
const compare = path.split(sep)
for (let i = 0; i < endOfPrefix; i++) {
if (compare[i] !== parts[i]) {
prefix = i
endOfPrefix = i
}
}
if (prefix === 0) break
if (endOfPrefix === 0) return ''
}
return prefix === 0 ? '' : parts.slice(0, prefix).join(sep) + sep
const prefix = parts.slice(0, endOfPrefix).join(sep)
return prefix.endsWith(sep) ? prefix : prefix + sep
}
{
"name": "common-path-prefix",
"version": "2.0.0",
"version": "3.0.0",
"description": "Computes the longest prefix string that is common to each path, excluding the base component",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -27,3 +27,3 @@ # common-path-prefix

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 any of the paths is used. Otherwise the platform-default value is used:

@@ -45,2 +45,3 @@ ```js

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

@@ -47,0 +48,0 @@

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