Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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 6,813,010 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.