main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[79a0317] | 1 | # common-path-prefix
|
---|
| 2 |
|
---|
| 3 | Computes the longest prefix string that is common to each path, excluding the base component. Tested with Node.js 8 and above.
|
---|
| 4 |
|
---|
| 5 | ## Installation
|
---|
| 6 |
|
---|
| 7 | ```console
|
---|
| 8 | npm install common-path-prefix
|
---|
| 9 | ```
|
---|
| 10 |
|
---|
| 11 | ## Usage
|
---|
| 12 |
|
---|
| 13 | The module has one default export, the `commonPathPrefix` function:
|
---|
| 14 |
|
---|
| 15 | ```js
|
---|
| 16 | const commonPathPrefix = require('common-path-prefix')
|
---|
| 17 | ```
|
---|
| 18 |
|
---|
| 19 | Call `commonPathPrefix()` with an array of paths (strings) and an optional separator character:
|
---|
| 20 |
|
---|
| 21 | ```js
|
---|
| 22 | const paths = ['templates/main.handlebars', 'templates/_partial.handlebars']
|
---|
| 23 |
|
---|
| 24 | commonPathPrefix(paths, '/') // returns 'templates/'
|
---|
| 25 | ```
|
---|
| 26 |
|
---|
| 27 | If the separator is not provided the first `/` or `\` found in any of the paths is used. Otherwise the platform-default value is used:
|
---|
| 28 |
|
---|
| 29 | ```js
|
---|
| 30 | commonPathPrefix(['templates/main.handlebars', 'templates/_partial.handlebars']) // returns 'templates/'
|
---|
| 31 | commonPathPrefix(['templates\\main.handlebars', 'templates\\_partial.handlebars']) // returns 'templates\\'
|
---|
| 32 | ```
|
---|
| 33 |
|
---|
| 34 | You can provide any separator, for example:
|
---|
| 35 |
|
---|
| 36 | ```js
|
---|
| 37 | commonPathPrefix(['foo$bar', 'foo$baz'], '$') // returns 'foo$''
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | An empty string is returned if no common prefix exists:
|
---|
| 41 |
|
---|
| 42 | ```js
|
---|
| 43 | commonPathPrefix(['foo/bar', 'baz/qux']) // returns ''
|
---|
| 44 | commonPathPrefix(['foo/bar']) // returns ''
|
---|
| 45 | ```
|
---|
| 46 |
|
---|
| 47 | Note that the following *does* have a common prefix:
|
---|
| 48 |
|
---|
| 49 | ```js
|
---|
| 50 | commonPathPrefix(['/foo/bar', '/baz/qux']) // returns '/'
|
---|
| 51 | ```
|
---|
Note:
See
TracBrowser
for help on using the repository browser.