-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.ts
61 lines (55 loc) · 2.06 KB
/
string.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
export const escapeSpecialCharactersForMarkdown = (input: string): string => {
return input
.replace(/\\/g, '\\\\') // Escape backslashes
.replace(/\*/g, '\\*') // Escape asterisks
.replace(/\_/g, '\\_') // Escape underscores
.replace(/\#/g, '\\#') // Escape hash symbols
.replace(/\`/g, '\\`') // Escape backticks
.replace(/\-/g, '\\-') // Escape hyphens
.replace(/\+/g, '\\+') // Escape plus signs
.replace(/\./g, '\\.') // Escape periods
.replace(/\!/g, '\\!') // Escape exclamation marks
.replace(/\[/g, '\\[') // Escape opening square brackets
.replace(/\]/g, '\\]') // Escape closing square brackets
.replace(/\(/g, '\\(') // Escape opening parentheses
.replace(/\)/g, '\\)') // Escape closing parentheses
.replace(/\n/g, '\\n') // Escape newlines
}
export const convertArrayToMarkdownTableList = (
array: Array<{ label: string; value: string | number }>,
sameWidthLabels: boolean = true,
): string => {
const maxKeyLength = Math.max(...array.map(({ label }) => label.length))
return array
.map(({ label, value }) => {
const padding = !sameWidthLabels
? ''
: ' '.repeat(maxKeyLength - label.length + 1)
return `\`${label}:${padding}\` ${value}\n`
})
.join('')
}
interface TreeNode {
label: string
children?: TreeNode[]
}
export const convertNestedArrayToTreeList = (node: TreeNode): string => {
const level = 0
const result: string[] = []
const traverse = (node: TreeNode, level: number) => {
const padding = ' '.repeat(((level || 1) - 1) * 2)
result.push(`${padding}${level > 0 ? '∟ ' : ''}${node.label}`)
node.children?.forEach((child) => traverse(child, level + 1))
}
traverse(node, level)
return result.join('\n')
}
export const getOneLineCommit = (originCommit: string): string => {
const lines = originCommit.split('\n')
return lines[0] || ''
}
export const prTitleFormatValid = (title: string): boolean => {
const titleFormatRegex =
/^[a-zA-Z0-9]+(\([a-zA-Z0-9\-_]+\))?: [a-zA-Z0-9]+(?: [a-zA-Z0-9]+)*$/
return titleFormatRegex.test(title)
}