
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
common-path-prefix
Advanced tools
Computes the longest prefix string that is common to each path, excluding the base component
The 'common-path-prefix' npm package is a utility that helps in finding the longest common path prefix from an array of file paths. This can be particularly useful in scenarios where you need to determine the shared directory structure among multiple files.
Finding Common Path Prefix
This feature allows you to find the longest common path prefix from an array of file paths. In the provided code sample, the common prefix for the given paths is '/foo/bar'.
const commonPathPrefix = require('common-path-prefix');
const paths = ['/foo/bar/baz', '/foo/bar/qux', '/foo/bar/quux'];
const prefix = commonPathPrefix(paths);
console.log(prefix); // Output: '/foo/bar'
The 'path' module is a core Node.js module that provides utilities for working with file and directory paths. While it does not directly offer a method to find the common path prefix, it provides various methods to manipulate and analyze paths, which can be used in conjunction to achieve similar results.
Lodash is a utility library that provides a wide range of functions for common programming tasks. While it does not specifically focus on path manipulation, it includes methods for working with arrays and strings that can be used to implement similar functionality.
Computes the longest prefix string that is common to each path, excluding the base component. Tested with Node.js 8 and above.
npm install common-path-prefix
The module has one default export, the commonPathPrefix
function:
const commonPathPrefix = require('common-path-prefix')
Call commonPathPrefix()
with an array of paths (strings) and an optional separator character:
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:
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:
commonPathPrefix(['foo$bar', 'foo$baz'], '$') // returns 'foo$''
An empty string is returned if no common prefix exists:
commonPathPrefix(['foo/bar', 'baz/qux']) // returns ''
commonPathPrefix(['foo/bar']) // returns ''
Note that the following does have a common prefix:
commonPathPrefix(['/foo/bar', '/baz/qux']) // returns '/'
FAQs
Computes the longest prefix string that is common to each path, excluding the base component
The npm package common-path-prefix receives a total of 7,724,856 weekly downloads. As such, common-path-prefix popularity was classified as popular.
We found that common-path-prefix demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.