aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/helpers/path.js
blob: ea389d48cb8fd4e0d8bf21a0b3e9b4a8dc2df7fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export function collapsePath (path, limit = 30) {
  if (typeof path !== 'string') {
    return '';
  }

  var tokens = path.split('/');

  if (tokens.length <= 2) {
    return path;
  }

  var head = _.first(tokens),
      tail = _.last(tokens),
      middle = _.initial(_.rest(tokens)),
      cut = false;

  while (middle.join().length > limit && middle.length > 0) {
    middle.shift();
    cut = true;
  }

  var body = [].concat(head, cut ? ['...'] : [], middle, tail);
  return body.join('/');
}