All files / path / _common / common.ts

100.00% Branches 8/8
100.00% Functions 1/1
100.00% Lines 21/21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
 
 
x51
x11
x11
 
x11
x11
x11
x14
x14
x10
x10
x10
 
x14
x43
x7
x7
x7
x7
x43
x14
x11
x11
























// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

export function common(paths: string[], sep: string): string {
  const [first = "", ...remaining] = paths;
  const parts = first.split(sep);

  let endOfPrefix = parts.length;
  let append = "";
  for (const path of remaining) {
    const compare = path.split(sep);
    if (compare.length <= endOfPrefix) {
      endOfPrefix = compare.length;
      append = "";
    }

    for (let i = 0; i < endOfPrefix; i++) {
      if (compare[i] !== parts[i]) {
        endOfPrefix = i;
        append = i === 0 ? "" : sep;
        break;
      }
    }
  }
  return parts.slice(0, endOfPrefix).join(sep) + append;
}