-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocrataExternalRepo.ts
54 lines (44 loc) · 1.3 KB
/
socrataExternalRepo.ts
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
// Experimental - port slugify logic to JS
interface SocrataDatasetKey {
id: string;
domain: string;
name: string;
}
export const socrataExternalRepo = (datasets: Array<SocrataDatasetKey>) => {
return datasets.map((dataset) => ({
namespace: getNamespace(dataset.domain),
repository: getRepository(dataset.name, dataset.id)
}))
}
const getNamespace = (domain: string, suffix?: string) => {
let domainParts = domain.split(".")
// Strip common TLDs
if (["com", "org", "net"].includes(domainParts[domainParts.length - 1])) {
domainParts.pop()
}
// Strip common prefixes
if (["data", "www"].includes(domainParts[0])) {
const [_, ...rest] = domainParts;
domainParts = rest;
}
if (suffix) {
domainParts.push(suffix)
}
return domainParts.join("-")
}
const getRepository = (datasetName: string, socrataId: string): string => {
return slugify(datasetName).replace("_", "-") + "-" + socrataId
}
const slugify = (text: string, maxLength: number = 50): string => {
text = text.replace(new RegExp(/[^\sa-zA-Z0-9]/), text.toLowerCase()).trim()
const parts = text.split(/\s+/)
let result = parts[0]
for (const r of result.slice(1)) {
const j = result + "_" + r;
if (j.length > maxLength) {
break
}
result = j
}
return result.slice(0, maxLength-1)
}