2021-01-21 12:30:53 +01:00
|
|
|
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
|
|
|
|
|
2024-02-21 19:42:15 +01:00
|
|
|
export const FIND_HBS_REGEX = /{{([^{].*?)}}/g
|
|
|
|
export const FIND_ANY_HBS_REGEX = /{?{{([^{].*?)}}}?/g
|
|
|
|
export const FIND_TRIPLE_HBS_REGEX = /{{{([^{].*?)}}}/g
|
2022-02-15 15:48:32 +01:00
|
|
|
|
2024-10-07 13:04:45 +02:00
|
|
|
const isJest = () => typeof jest !== "undefined"
|
|
|
|
|
2024-02-21 19:42:15 +01:00
|
|
|
export const isBackendService = () => {
|
2024-10-07 13:04:45 +02:00
|
|
|
// We consider the tests for string-templates to be frontend, so that they
|
|
|
|
// test the frontend JS functionality.
|
|
|
|
if (isJest()) {
|
|
|
|
return false
|
|
|
|
}
|
2024-02-20 17:23:35 +01:00
|
|
|
return typeof window === "undefined"
|
|
|
|
}
|
|
|
|
|
2024-02-21 19:42:15 +01:00
|
|
|
export const isJSAllowed = () => {
|
2024-02-20 17:23:35 +01:00
|
|
|
return process && !process.env.NO_JS
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:48:32 +01:00
|
|
|
// originally this could be done with a single regex using look behinds
|
|
|
|
// but safari does not support this feature
|
|
|
|
// original regex: /(?<!{){{[^{}]+}}(?!})/g
|
2024-02-21 21:10:53 +01:00
|
|
|
export const findDoubleHbsInstances = (string: string): string[] => {
|
2022-02-15 15:48:32 +01:00
|
|
|
let copied = string
|
2024-02-21 19:57:05 +01:00
|
|
|
const doubleRegex = new RegExp(FIND_HBS_REGEX)
|
|
|
|
const regex = new RegExp(FIND_TRIPLE_HBS_REGEX)
|
2022-02-15 15:48:32 +01:00
|
|
|
const tripleMatches = copied.match(regex)
|
|
|
|
// remove triple braces
|
|
|
|
if (tripleMatches) {
|
2024-02-21 21:10:53 +01:00
|
|
|
tripleMatches.forEach((match: string) => {
|
2022-02-15 15:48:32 +01:00
|
|
|
copied = copied.replace(match, "")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const doubleMatches = copied.match(doubleRegex)
|
|
|
|
return doubleMatches ? doubleMatches : []
|
|
|
|
}
|
2021-01-21 12:30:53 +01:00
|
|
|
|
2024-02-21 21:10:53 +01:00
|
|
|
export const isAlphaNumeric = (char: string) => {
|
2021-01-21 12:30:53 +01:00
|
|
|
return char.match(ALPHA_NUMERIC_REGEX)
|
|
|
|
}
|
|
|
|
|
2024-02-21 21:10:53 +01:00
|
|
|
export const swapStrings = (
|
|
|
|
string: string,
|
|
|
|
start: number,
|
|
|
|
length: number,
|
|
|
|
swap: string
|
|
|
|
) => {
|
2021-01-21 12:30:53 +01:00
|
|
|
return string.slice(0, start) + swap + string.slice(start + length)
|
|
|
|
}
|
|
|
|
|
2024-02-21 19:42:15 +01:00
|
|
|
export const removeHandlebarsStatements = (
|
2024-02-21 21:10:53 +01:00
|
|
|
string: string,
|
2021-12-06 18:58:43 +01:00
|
|
|
replacement = "Invalid binding"
|
|
|
|
) => {
|
2024-02-21 19:57:05 +01:00
|
|
|
let regexp = new RegExp(FIND_HBS_REGEX)
|
2021-02-03 14:04:19 +01:00
|
|
|
let matches = string.match(regexp)
|
|
|
|
if (matches == null) {
|
|
|
|
return string
|
|
|
|
}
|
|
|
|
for (let match of matches) {
|
|
|
|
const idx = string.indexOf(match)
|
2024-02-21 19:57:05 +01:00
|
|
|
string = swapStrings(string, idx, match.length, replacement)
|
2021-02-03 14:04:19 +01:00
|
|
|
}
|
|
|
|
return string
|
|
|
|
}
|
2021-10-14 12:51:05 +02:00
|
|
|
|
2024-02-21 21:10:53 +01:00
|
|
|
export const btoa = (plainText: string) => {
|
2021-10-14 12:51:05 +02:00
|
|
|
return Buffer.from(plainText, "utf-8").toString("base64")
|
|
|
|
}
|
|
|
|
|
2024-02-21 21:10:53 +01:00
|
|
|
export const atob = (base64: string) => {
|
2021-10-14 12:51:05 +02:00
|
|
|
return Buffer.from(base64, "base64").toString("utf-8")
|
|
|
|
}
|
2024-08-14 18:21:40 +02:00
|
|
|
|
|
|
|
export const prefixStrings = (
|
|
|
|
baseString: string,
|
|
|
|
strings: string[],
|
|
|
|
prefix: string
|
|
|
|
) => {
|
|
|
|
// Escape any special characters in the strings to avoid regex errors
|
|
|
|
const escapedStrings = strings.map(str =>
|
|
|
|
str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
|
|
)
|
|
|
|
const regexPattern = new RegExp(`\\b(${escapedStrings.join("|")})\\b`, "g")
|
|
|
|
return baseString.replace(regexPattern, `${prefix}$1`)
|
|
|
|
}
|