-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinguist.js
58 lines (51 loc) · 1.78 KB
/
linguist.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
var linguistObject, linguistInverseObject;
var convertLinguistJsonArrayToObject = jsonArray => {
var linguistJsonObject = {};
jsonArray.forEach( jsonObject => {
linguistJsonObject[jsonObject.language] = jsonObject.extensions;
});
return linguistJsonObject;
};
var convertLinguistDataToJson = linguistData => {
var linguistLines = linguistData.split('\n');
var jsonData = [];
var flag = false;
var flag2 = false;
linguistLines.forEach( linguistLine => {
if(flag && !linguistLine.startsWith(" ")) flag = false;
else if(flag) {
if(linguistLine.startsWith(" extensions:")) flag2 = true;
else if(flag2 && !linguistLine.startsWith(" -")) flag2 = false;
else if(flag2) jsonData[jsonData.length - 1].extensions.push(linguistLine.slice(5, -1));
}
if(!(linguistLine.startsWith("#") || linguistLine.startsWith(" ") || linguistLine.startsWith("-") || linguistLine.length == 0)) {
jsonData.push({language: linguistLine.slice(0, -1), extensions: []});
flag = true;
}
});
return convertLinguistJsonArrayToObject(jsonData);
};
var convertObjectToInverse = linguistObject => {
linguistInverseObject = {};
for(var language in linguistObject)
linguistObject[language].forEach( extensions => {
linguistInverseObject[extensions] = [];
});
for(var language in linguistObject)
linguistObject[language].forEach( extensions => {
linguistInverseObject[extensions].push(language);
});
return linguistInverseObject;
};
var linguistPromise = new Promise( (resolve, reject) => {
$.ajax({
url: 'https://api.github.com/repos/github/linguist/git/blobs/50854668d4f8c53e710d49c669be3d311849eef0',
success: result => {
var decoded_string = atob(result.content);
resolve(decoded_string);
},
error: (jqXHR, textStatus, errorThrown) => {
reject(textStatus);
},
});
});