aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/helpers/path.js
blob: 1468b8a885c2f4842f25cc1db61544823ab5f708 (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
25
26
27
import _ from 'underscore';


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('/');
}