Skip to content

Commit e0a6a9c

Browse files
authored
refactor: make code more idiomatic
1 parent 85e81a8 commit e0a6a9c

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

.github/workflows/UpdateDirectory.mjs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import path from 'path'
22
import fs from 'fs'
33
import { globby } from 'globby'
44

5-
const URL_BASE = 'https://github.com/TheAlgorithms/Javascript/blob/master'
6-
75
function pathPrefix (i) {
86
const res = ' '.repeat(i)
97
return res + '*'
@@ -12,6 +10,7 @@ function pathPrefix (i) {
1210
function printPath (oldPath, newPath, output) {
1311
const oldParts = oldPath.split(path.sep)
1412
const newParts = newPath.split(path.sep)
13+
1514
for (let i = 0; i < newParts.length; ++i) {
1615
const newPart = newParts[i]
1716
if (i + 1 > oldParts.length || oldParts[i] !== newPart) {
@@ -20,6 +19,7 @@ function printPath (oldPath, newPath, output) {
2019
}
2120
}
2221
}
22+
2323
return newPath
2424
}
2525

@@ -32,38 +32,23 @@ function pathsToMarkdown (filePaths) {
3232
if (a.toLowerCase() > b.toLowerCase()) return 1
3333
return 0
3434
})
35+
3536
for (let filepath of filePaths) {
36-
const file = filepath.split(path.sep)
37-
let filename = ''
38-
if (file.length === 1) {
39-
filepath = ''
40-
filename = file[0]
41-
} else {
42-
const total = file.length
43-
filename = file[total - 1]
44-
filepath = file.splice(0, total - 1).join(path.sep)
45-
}
37+
let filename = path.basename(filepath)
38+
filepath = path.dirname(path.sep)
39+
4640
if (filepath !== oldPath) {
4741
oldPath = printPath(oldPath, filepath, output)
4842
}
49-
let indent = 0
50-
for (let i = 0; i < filepath.length; ++i) {
51-
if (filepath[i] === path.sep) {
52-
++indent
53-
}
54-
}
55-
if (filepath) {
56-
++indent
57-
}
43+
44+
let indent = filepath.replace(new RegExp(`/[^${path.sep}]/`)).length + 1
5845

5946
// prepare the markdown-esque prefix to the file's line
6047
const prefix = pathPrefix(indent)
6148

6249
// remove extension from filename
63-
const name = filename.split('.')[0]
64-
65-
// create URL to the actual file on github
66-
const url = encodeURI([URL_BASE, filepath, filename].join('/'))
50+
const name = path.basename(filename, ".js")
51+
const url = path.join(filepath, filename)
6752

6853
output.push(`${prefix} [${name}](${url})`)
6954
}

0 commit comments

Comments
 (0)