Skip to content

Commit c0e1fb6

Browse files
committed
Move logic to a helper
1 parent 0f5bcbd commit c0e1fb6

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

plugins/airtable/src/data.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ export async function getTables(baseId: string, signal: AbortSignal): Promise<Ai
6565
const EMAIL_REGEX = /\S[^\s@]*@\S+\.\S+/
6666
const PHONE_REGEX = /^(\+?[0-9])[0-9]{7,14}$/
6767

68-
const MAILTO_PREFIX = "mailto:"
69-
const TEL_PREFIX = "tel:"
70-
71-
const MAILTO_REGEX = new RegExp(MAILTO_PREFIX, "gi")
72-
const TEL_REGEX = new RegExp(TEL_PREFIX, "gi")
68+
/**
69+
* Helper function to ensure a value has the specified prefix,
70+
* removing any existing instances of the prefix first
71+
*/
72+
function ensurePrefix(prefix: string, value: string): string {
73+
const regex = new RegExp(prefix, "gi")
74+
const result = value.replace(regex, "")
75+
return `${prefix}${result}`
76+
}
7377

7478
const NonEmptyArrayOfAttachmentsSchema = v.pipe(
7579
v.array(v.object({ type: v.optional(v.string()), id: v.string(), url: v.string() })),
@@ -103,14 +107,14 @@ export function getFieldDataEntryForFieldSchema(
103107
if (typeof value === "string") {
104108
if (fieldSchema.airtableType === "email" || EMAIL_REGEX.test(value)) {
105109
return {
106-
value: `${MAILTO_PREFIX}${value.replace(MAILTO_REGEX, "")}`,
110+
value: ensurePrefix("mailto:", value),
107111
type: "link",
108112
}
109113
}
110114

111115
if (fieldSchema.airtableType === "phoneNumber" || PHONE_REGEX.test(value)) {
112116
return {
113-
value: `${TEL_PREFIX}${value.replace(TEL_REGEX, "")}`,
117+
value: ensurePrefix("tel:", value),
114118
type: "link",
115119
}
116120
}

0 commit comments

Comments
 (0)