Created
May 27, 2021 08:11
-
-
Save kdepp/dd95d20e9682c539aa462e7f418e22fa to your computer and use it in GitHub Desktop.
Group appfolio url
This file contains hidden or 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 fs = require("fs"); | |
const url = require("url"); | |
type UrlInfo = { | |
fullUrl: string; | |
hostname: string; | |
platform: string | undefined; | |
path: string; | |
count: number; | |
}; | |
function isSameUrlInfo(a: UrlInfo, b: UrlInfo): boolean { | |
return a.platform === b.platform && a.path === b.path; | |
} | |
function getPlatform(hostname: string): string | undefined { | |
if (/appfolio/.test(hostname)) { | |
return "appfolio"; | |
} | |
return undefined; | |
} | |
function getPathPattern(pathname: string): string { | |
return pathname | |
.replace(/\w+-\w+-\w+-\w+-\w+/g, ":id") | |
.replace(/\d+/g, ":number"); | |
} | |
function parseUrl(urlStr: string): UrlInfo | undefined { | |
try { | |
const u = url.parse(urlStr); | |
return { | |
fullUrl: urlStr, | |
hostname: u.hostname, | |
platform: getPlatform(u.hostname), | |
path: getPathPattern(u.pathname), | |
count: 1, | |
}; | |
} catch (e) { | |
return undefined; | |
} | |
} | |
function processUrls(urls: string[]): UrlInfo[] { | |
const dict: Record<string, UrlInfo> = {}; | |
urls.forEach((url) => { | |
const info = parseUrl(url); | |
if (!info?.platform) { | |
return; | |
} | |
if (/help/.test(info.hostname)) { | |
return; | |
} | |
const key = `${info.platform}+${info.path}`; | |
if (!dict[key]) { | |
dict[key] = info; | |
} else { | |
dict[key].count++; | |
} | |
}); | |
const list = Object.values(dict); | |
list.sort((a, b) => { | |
return Math.sign(b.count - a.count) || (a.path < b.path ? -1 : 1); | |
}); | |
return list; | |
} | |
function run(): void { | |
fs.readFile("./urls.txt", "utf-8", (err: Error, data: string) => { | |
if (err) { | |
console.error("Failed to read file", err); | |
return; | |
} | |
const txt = data.toString(); | |
const lines = txt.split("\n"); | |
const list = processUrls(lines); | |
const result = list | |
.map((item) => { | |
const editOrAdd = /edit|new/.test(item.path); | |
return `${item.platform}, ${item.path}, ${item.count}, ${editOrAdd}, ${item.fullUrl}`; | |
}) | |
.join("\n"); | |
console.log(result); | |
}); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment