source: imaps-frontend/node_modules/common-path-prefix/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 843 bytes
RevLine 
[79a0317]1'use strict'
2const { sep: DEFAULT_SEPARATOR } = require('path')
3
4const determineSeparator = paths => {
5 for (const path of paths) {
6 const match = /(\/|\\)/.exec(path)
7 if (match !== null) return match[0]
8 }
9
10 return DEFAULT_SEPARATOR
11}
12
13module.exports = function commonPathPrefix (paths, sep = determineSeparator(paths)) {
14 const [first = '', ...remaining] = paths
15 if (first === '' || remaining.length === 0) return ''
16
17 const parts = first.split(sep)
18
19 let endOfPrefix = parts.length
20 for (const path of remaining) {
21 const compare = path.split(sep)
22 for (let i = 0; i < endOfPrefix; i++) {
23 if (compare[i] !== parts[i]) {
24 endOfPrefix = i
25 }
26 }
27
28 if (endOfPrefix === 0) return ''
29 }
30
31 const prefix = parts.slice(0, endOfPrefix).join(sep)
32 return prefix.endsWith(sep) ? prefix : prefix + sep
33}
Note: See TracBrowser for help on using the repository browser.