-
Notifications
You must be signed in to change notification settings - Fork 1
/
namespace-author-contributor-repository.js
137 lines (111 loc) · 4.28 KB
/
namespace-author-contributor-repository.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const namespaceAuthorsScore = {};
exports.save = (fileNameToSave, author, score) => {
if (!namespaceAuthorsScore.hasOwnProperty(fileNameToSave)) {
namespaceAuthorsScore[fileNameToSave] = {};
namespaceAuthorsScore[fileNameToSave][author] = score;
updateNamespace(fileNameToSave, author, score);
return;
}
if (!namespaceAuthorsScore[fileNameToSave].hasOwnProperty(author)) {
namespaceAuthorsScore[fileNameToSave][author] = score;
namespaceAuthorsScore[fileNameToSave] = sortObjects(namespaceAuthorsScore[fileNameToSave]);
updateNamespace(fileNameToSave, author, score);
return;
}
namespaceAuthorsScore[fileNameToSave][author] += score;
namespaceAuthorsScore[fileNameToSave] = sortObjects(namespaceAuthorsScore[fileNameToSave]);
updateNamespace(fileNameToSave, author, score);
};
exports.get = (namespace, author) => {
if (namespaceAuthorsScore.hasOwnProperty(namespace) && namespaceAuthorsScore[namespace].hasOwnProperty(author))
return namespaceAuthorsScore[namespace][author];
throw `No namespace or author was found for ${namespace}, ${author}!`;
};
function sortProperties(obj)
{
// convert object into array
var sortable=[];
for(var key in obj)
if(obj.hasOwnProperty(key))
sortable.push([key, obj[key]]); // each item is an array in format [key, value]
// sort items by value
sortable.sort(function(a, b)
{
return b[1]-a[1]; // compare numbers
});
return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
}
function sortObjects(objects) {
var newObject = {};
var sortedArray = sortProperties(objects);
for (var i = 0; i < sortedArray.length; i++) {
var key = sortedArray[i][0];
var value = sortedArray[i][1];
newObject[key] = value;
}
return newObject;
}
exports.getAll = () => namespaceAuthorsScore;
exports.getByKey = (key) => {
if (!namespaceAuthorsScore.hasOwnProperty(key))
return null;
let scores = namespaceAuthorsScore[key];
let sumScores = 0;
for (let author in scores) {
sumScores += scores[author];
}
let result = {};
for (let author in scores) {
result[author] = (scores[author] / sumScores * 100).toFixed(2) + '%';
}
return result;
};
function saveNamespace(deepestNamespace, author, score) {
if (!deepestNamespace)
return;
if (!namespaceAuthorsScore.hasOwnProperty(deepestNamespace)) {
namespaceAuthorsScore[deepestNamespace] = {};
namespaceAuthorsScore[deepestNamespace][author] = score;
saveNamespace(getDeepestNamespaceForNamespace(deepestNamespace), author, score);
return;
}
if (!namespaceAuthorsScore[deepestNamespace].hasOwnProperty(author)) {
namespaceAuthorsScore[deepestNamespace][author] = score;
namespaceAuthorsScore[deepestNamespace] = sortObjects(namespaceAuthorsScore[deepestNamespace]);
saveNamespace(getDeepestNamespaceForNamespace(deepestNamespace), author, score);
return;
}
namespaceAuthorsScore[deepestNamespace][author] += score;
namespaceAuthorsScore[deepestNamespace] = sortObjects(namespaceAuthorsScore[deepestNamespace]);
saveNamespace(getDeepestNamespaceForNamespace(deepestNamespace), author, score);
}
function updateNamespace(fileNameToSave, author, score) {
let deepestNamespace = getDeepestNamespaceForFile(fileNameToSave);
saveNamespace(deepestNamespace, author, score);
}
function getDeepestNamespaceForFile(fileName) {
const indexOfOneToLastDot = getIndexOfOneToLastDot(fileName);
return indexOfOneToLastDot
? fileName.substring(0, indexOfOneToLastDot)
: "";
}
function getDeepestNamespaceForNamespace(namespace) {
const indexOfLastDot = getIndexOfLastDot(namespace);
return indexOfLastDot
? namespace.substring(0, indexOfLastDot)
: "";
}
function getIndexOfLastDot(str) {
let indicesOfDot = indicesOfCharsInString(str, '.');
return indicesOfDot[indicesOfDot.length-1];
}
function getIndexOfOneToLastDot(str) {
let indicesOfDot = indicesOfCharsInString(str, '.');
return indicesOfDot[indicesOfDot.length-2];
}
function indicesOfCharsInString(str, char) {
let indices = [];
for(let i=0; i<str.length;i++)
if (str[i] === char) indices.push(i);
return indices;
}