-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetLettersInFile.mjs
45 lines (38 loc) · 1.66 KB
/
getLettersInFile.mjs
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
import { Octokit } from '@octokit/rest'
export default async function getLettersInFile(token) {
const octokit = new Octokit({
auth: token
}),
letters = new Map(),
user = await octokit.request('GET /user'),
countLettersInFile = async (repo, path) => {
const response = await octokit.repos.getContent({
owner: 'lodash',
repo,
path
})
if (Array.isArray(response.data)) {
for (const item of response.data) {
await countLettersInFile(repo, item.path)
}
} else if (/\.js$|\.ts$/.test(response.data.name)){
const content = Buffer.from(response.data.content, 'base64').toString()
const lines = content.split('\n')
for (const line of lines) {
const chars = line.split('')
for (const char of chars) {
if (/^[a-zA-Z]$/.test(char)) {
const count = letters.get(char.toLowerCase()) || 0
letters.set(char.toLowerCase(), count + 1)
}
}
}
console.log(`FILE: ${ response.data.name} - ✅`)
}
};
console.log(`Nice to see you, ${ user.data.login } 👋! Welcome to this little application. Sit tight, it might take a while... 🙇️`)
await countLettersInFile('lodash', '')
const sortedLetters = [...letters.entries()].sort((a, b) => b[1] - a[1])
console.log('🎉Hooray!! Here you have the results!')
console.table(sortedLetters)
}