-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathindex.js
126 lines (109 loc) · 3.19 KB
/
index.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
const fs = require("fs");
const algorithmsFolder = "../algorithms/";
const readMeFileName = "../README";
const languageAlgorithmMap = {};
const algorithmCountMap = {};
const getReadHeaderAndFooter = () => {
const headerAndFooterData = fs.readFileSync("readme-header-footer.json");
return JSON.parse(headerAndFooterData);
};
const readLanguages = () => {
const languages = fs
.readdirSync(algorithmsFolder, {
withFileTypes: true,
})
.reduce((a, c) => {
c.isDirectory() && a.push(c.name);
return a;
}, []);
languages.forEach((language) => {
const algorithms = readAlgorithms(language);
languageAlgorithmMap[language] = algorithms;
algorithms.forEach((algorithm) => {
if (algorithmCountMap[algorithm]) {
algorithmCountMap[algorithm] += 1;
} else {
algorithmCountMap[algorithm] = 1;
}
});
});
const languageSorted = Object.keys(languageAlgorithmMap).sort(function (
a,
b
) {
return languageAlgorithmMap[b].length - languageAlgorithmMap[a].length;
});
const rows = [];
rows.push(generateSubHeader(languageSorted.length));
let algorithms = Object.keys(algorithmCountMap).sort(function (a, b) {
return algorithmCountMap[b] - algorithmCountMap[a];
});
algorithms.forEach((algorithm) => {
rows.push(generateAlgorithmRow(algorithm, languageSorted));
});
const readHeaderAndFooter = getReadHeaderAndFooter();
Object.keys(readHeaderAndFooter).forEach((lang) => {
let fileName;
if (lang === "Default") {
fileName = `${readMeFileName}.md`;
} else {
fileName = `${readMeFileName}-${lang}.md`;
}
const data = [
readHeaderAndFooter[lang].Header,
generateHeaderRow(languageSorted, readHeaderAndFooter[lang].Language),
...rows,
readHeaderAndFooter[lang].Footer,
].join("\n");
fs.writeFile(fileName, data, function (err) {
if (err) {
return console.log(err);
}
console.log(`The file was saved! ${lang}`);
});
});
};
const readAlgorithms = (language) => {
const languageFolder = `${algorithmsFolder}${language}/`;
const algorithms = fs
.readdirSync(languageFolder, {
withFileTypes: true,
})
.reduce((a, c) => {
c.isDirectory() && c.name != "node_modules" && a.push(c.name);
return a;
}, []);
return algorithms;
};
const generateHeaderRow = (languages, language) => {
const headerRow = [language, ...languages, ""];
const header = headerRow.join(" | ");
return header;
};
const generateSubHeader = (count) => {
const subHeaderElement = "|:---:";
let subHeaderRow = "";
for (let i = 0; i < count + 1; i += 1) {
subHeaderRow += subHeaderElement;
}
subHeaderRow += "|";
return subHeaderRow;
};
const generateAlgorithmRow = (algorithm, languages) => {
const algorithmRow = [algorithm];
languages.forEach((language) => {
const algorithmsForLanguage = languageAlgorithmMap[language];
if (
algorithmsForLanguage &&
algorithmsForLanguage.indexOf(`${algorithm}`) >= 0
) {
algorithmRow.push(":+1:");
} else {
algorithmRow.push(" ");
}
});
algorithmRow.push("");
const row = algorithmRow.join(" | ");
return row;
};
readLanguages();