forked from livestyle/chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact-paths.js
52 lines (46 loc) · 1.02 KB
/
compact-paths.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Compacts given list of paths: keeps smallest right-hand
* difference between paths
*/
'use strict';
import {unique} from '../lib/utils';
export default function(list) {
var data = unique(list).map(function(path) {
return {
parts: path.split(/\/|\\/).filter(Boolean),
rightParts: [],
path: path
};
});
var lookup = {};
var hasCollision = true, hasNext = true;
var process = function(item) {
if (item.parts.length) {
item.rightParts.unshift(item.parts.pop());
var lookupKey = item.rightParts.join('/');
if (!lookup[lookupKey]) {
lookup[lookupKey] = true;
} else {
hasCollision = true;
}
}
return !!item.parts.length;
};
while (hasNext) {
hasNext = false;
hasCollision = false;
lookup = {};
for (var i = 0, il = data.length; i < il; i++) {
hasNext = process(data[i]) || hasNext;
}
if (!hasCollision) {
break;
}
}
return data.map(function(item) {
return {
label: item.parts.length ? item.rightParts.join('/') : item.path,
value: item.path
};
});
};