Repository navigation
Expand file tree
/
Copy pathremote-set.js
More file actions
63 lines (54 loc) · 1.37 KB
/
Copy pathremote-set.js
File metadata and controls
63 lines (54 loc) · 1.37 KB
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
53
54
55
56
57
58
59
60
61
62
63
import {nullRemote} from './remote';
import {pushAtKey} from '../helpers';
export default class RemoteSet {
constructor(iterable = []) {
this.byName = new Map();
this.byDotcomRepo = new Map();
this.protocolCount = new Map();
for (const remote of iterable) {
this.add(remote);
}
}
add(remote) {
this.byName.set(remote.getName(), remote);
if (remote.isGithubRepo()) {
pushAtKey(this.byDotcomRepo, remote.getSlug(), remote);
}
if (remote.getProtocol()) {
const count = this.protocolCount.get(remote.getProtocol()) || 0;
this.protocolCount.set(remote.getProtocol(), count + 1);
}
}
isEmpty() {
return this.byName.size === 0;
}
size() {
return this.byName.size;
}
withName(name) {
return this.byName.get(name) || nullRemote;
}
[Symbol.iterator]() {
return this.byName.values();
}
filter(predicate) {
return new this.constructor(
Array.from(this).filter(predicate),
);
}
matchingGitHubRepository(owner, name) {
return this.byDotcomRepo.get(`${owner}/${name}`) || [];
}
mostUsedProtocol(choices) {
let best = choices[0];
let bestCount = 0;
for (const protocol of choices) {
const count = this.protocolCount.get(protocol) || 0;
if (count > bestCount) {
bestCount = count;
best = protocol;
}
}
return best;
}
}