/
ch-2.js
executable file
·59 lines (56 loc) · 1.42 KB
/
ch-2.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
#! /usr/bin/node
function kdd(dirlist0) {
const fs = require("fs")
let dirlist = Array.from(dirlist0)
dirlist.sort()
let fx = new Map()
for (let d of dirlist) {
for (let entry of fs.readdirSync(d, { withFileTypes: true })) {
let nn = entry.name
if (entry.isDirectory()) {
nn += "/"
}
if (!fx.has(nn)) {
fx.set(nn,new Set())
}
let t = fx.get(nn)
t.add(d)
fx.set(nn,t)
}
}
let mm = dirlist.length
let out = [dirlist]
for (let f of [...fx.keys()].sort()) {
if (fx.get(f).size != mm) {
let l = []
for (let d of dirlist) {
if (fx.get(f).has(d)) {
l.push(f)
} else {
l.push("")
}
}
out.push(l)
}
}
tabular(out)
}
function tabular(d) {
let columnlength = []
d.forEach(row => {
row.forEach((col, i) => {
while (columnlength.length <= i) {
columnlength.push(0)
}
columnlength[i] = Math.max(columnlength[i], col.length)
})
})
d.forEach(row => {
let s = []
row.forEach((col, i) => {
s.push(col.padEnd(columnlength[i]))
})
console.log(s.join(" | "))
})
}
kdd(["dir_a","dir_b","dir_c"])