-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
161 lines (145 loc) · 4.47 KB
/
utils.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import addrs from 'email-addresses'
import psl from 'psl'
import URL from 'url-parse'
import { z } from 'zod'
import Handlebars from 'handlebars'
import * as R from 'remeda'
export { R }
export const zUrl = z.string().url()
export const zUuid = z.string().uuid()
/** Supports parsing email from RFC 5322 */
export const zEmail = z.string().transform((str, ctx) => {
const ret = parseEmail(str)
if (!ret) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Invaild email based on RFC 5322 ${str}`,
})
return z.NEVER
}
return ret
})
export const zDomain = z.string().transform((arg, ctx) => {
try {
return parseDomain(arg)
} catch (err) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `${err}`,
})
return z.NEVER
}
})
export const zEmailOrDomain = z.string().transform((arg) => {
const emailRes = zEmail.safeParse(arg)
if (emailRes.success) {
return {
type: 'email' as const,
value: emailRes.data.address,
name: emailRes.data.name,
}
}
const domainRes = zDomain.safeParse(arg)
if (domainRes.success) {
return { type: 'domain' as const, value: domainRes.data }
}
return { type: 'error' as const, value: null }
})
// We need the url-parse package because the native one is not availble in Coda pack runtime
export function parseEmail(str: string) {
return parseEmails(str)[0]
}
export function parseEmails(str: string) {
let res = addrs.parseAddressList({ input: str, simple: false })
res = (res as any)?.addresses // Type here is wrong when simple: false https://share.cleanshot.com/gfdJBR3w
return (res ?? [])
.flatMap((p) => (p.type === 'mailbox' ? [p] : p.addresses))
.map((mb) => {
const [firstName, lastName] = splitName(mb.name)
return {
// https://github.com/jackbearheart/email-addresses/issues/62
display: mb.node.tokens.trim(),
address: mb.address,
name: mb.name,
domain: mb.domain,
firstName,
lastName,
}
})
}
/** Supports parsing domain from RFC5322 email address, and both with + without protocol */
export function parseDomain(input: string, includeSubdomain?: boolean) {
let email = parseEmail(input)
if (email) {
return parseDomain(email.domain, includeSubdomain)
}
const prefix = input.includes('://') ? '' : 'https://'
const url = new URL(prefix + input)
const parsed = psl.parse(url.hostname)
if ('domain' in parsed && parsed.domain) {
return R.compact([includeSubdomain && parsed.subdomain, parsed.domain]).join(
'.',
)
}
// TODO: Use verror
throw new Error(`${parsed.error?.code}: ${parsed.error?.message}`)
}
export function parsePathname(urlString: string) {
const prefix = urlString.includes('://') ? '' : 'https://'
const url = new URL(prefix + urlString)
return url.pathname
}
export function splitName(
name: string | null | undefined,
): [firstName: string, lastName: string] {
// Omitting empty string
const [firstName, ...rest] = (name ?? '').split(' ').filter((p) => !!p)
return [firstName, rest.length ? rest.join(' ') : undefined]
}
export function arrayToSentence(
parts: string[],
{
separator = ',',
lastSeparator = '&',
}: { separator?: string; lastSeparator?: string } = {},
) {
const firstPart = parts.slice(0, parts.length - 2)
const last2 = parts.slice(parts.length - 2)
return [...firstPart, last2.join(` ${lastSeparator} `)].join(`${separator} `)
}
export function buildUrl(urlString: string, query: Record<string, unknown>) {
const url = new URL(urlString)
url.set('query', URL.qs.stringify(query))
return url.toString()
}
export function renderTemplate(
templateStr: string,
variables: Record<string, unknown>,
options: { strict: boolean } = { strict: true },
) {
const template = Handlebars.compile(templateStr, { ...options })
try {
return template(variables)
} catch (err) {
const msg = `${err}`
const regex = /(.+) not defined in \[object Object\] (.+)/
const [, prefix, suffix] = regex.exec(msg)
throw new Error(`${prefix} is missing in variables ${suffix}`)
}
}
/** Inspired by postgres jsonBuildObject */
export function jsonBuildObject(...keyValues: (string | number | boolean)[]) {
return R.pipe(
keyValues,
R.chunk(2),
R.map(([key, value]) => [key, jsonParsePrimitive(value)]),
(pairs) => R.fromPairs(pairs as [string, string][]),
)
}
export function jsonParsePrimitive(value: unknown) {
try {
return JSON.parse(`${value}`)
} catch {
return value
}
}