Last active
July 29, 2022 16:41
-
-
Save kazuma0129/7b8a350c8f377b49e2c7474a5755e66d to your computer and use it in GitHub Desktop.
Collect files exact match using Map in Deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const collectExactMatchFiles = async (p: string, cache: Map<string, string[]>) => { | |
const collect = async (key: string) => { | |
const c = await Deno.readTextFile(key); | |
if (!cache.has(c)) { | |
cache.set(c, [key]); | |
return; | |
} | |
const e = cache.get(c); | |
e && cache.set(c, [...e, key]); | |
}; | |
const traverse = async (p: string) => { | |
const entries = Deno.readDir(p); | |
for await (const e of entries) { | |
const compPath = `${p}/${e.name}`; | |
if (e.isFile) { | |
await collect(compPath); | |
} | |
if (e.isDirectory) { | |
await traverse(compPath); | |
} | |
} | |
}; | |
await traverse(p); | |
return cache; | |
}; | |
const TARGET_DIR = Deno.args[0]; | |
console.time('time'); | |
const result = await collectExactMatchFiles(TARGET_DIR, new Map<string, string[]>()); | |
console.log(result); | |
console.timeEnd('time'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment