1. common-path-prefix
Computes the longest prefix string that is common to each path, excluding the base component
common-path-prefix
Package: common-path-prefix
Created by: novemberborn
Last modified: Fri, 09 Jun 2023 21:38:05 GMT
Version: 3.0.0
License: ISC
Downloads: 35,280,007
Repository: https://github.com/novemberborn/common-path-prefix

Install

npm install common-path-prefix
yarn add common-path-prefix

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

 npm install common-path-prefix

Usage

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 '/'

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code