common-path-start

0.0.4 • Public • Published

common-path-start

NPM version Build Status semantic-release

NPM Downloads

Detects the same starting part of the path in an array of file paths and returns it, its length, or it cuts it from the specified file paths.

Example

If you have the following file paths:

snapshots/fun/hmpg.net.html
snapshots/fun/xkcd.com.html
snapshots/wikimedia.org.html

This plugin lets you detects "snapshots/" as the common path start, its length 10 characters, or cut the common start from every file path producing:

fun/hmpg.net.html
fun/xkcd.com.html
wikimedia.org.html

Installation

You need node >= 6 and npm installed. You may install this module with the following command:

$ npm install common-path-start --save

Programmatic Usage

This module exports several static functions operating on file paths, which use forward slash as a separator.

const {getCommonPathStart} = require('common-path-start')
const paths = [
  'snapshots/fun/hmpg.net.html',
  'snapshots/fun/xkcd.com.html',
  'snapshots/wikimedia.org.html'
]
const commonPathStart = getCommonPathStart(paths) // "snapshots/"

getCommonPathStart (paths) : string

Expects an array of strings with file paths and returns a string with the common path start, or an empty string, if there is no common starting directory among all file paths.

getCommonPathLength (paths) : number

Expects an array of strings with file paths and returns the count of characters of the common path start, or zero, if there is no common starting directory among all file paths.

cutCommonPathStart (paths) : array

Expects an array of strings with file paths and returns a new array with file paths with the common path start cut away, or the same file path array, if there is no common starting directory among all file paths.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style.

Release History

  • 2018-05-14 v0.0.1 Initial release

License

Copyright (c) 2018-2019 Ferdinand Prantl

Licensed under the MIT license.

Package Sidebar

Install

npm i common-path-start

Weekly Downloads

47

Version

0.0.4

License

MIT

Unpacked Size

23.9 kB

Total Files

9

Last publish

Collaborators

  • prantlf