forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ts
More file actions
111 lines (98 loc) · 3.02 KB
/
Copy pathstring.ts
File metadata and controls
111 lines (98 loc) · 3.02 KB
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
import { sumBy } from 'lodash'
export const truncateString = (str: string, maxLength: number) => {
if (str.length <= maxLength) {
return str
}
return str.slice(0, maxLength) + '...'
}
export const replaceNonStandardPlaceholderComments = (
content: string,
replacement: string
): string => {
const commentPatterns = [
// JSX comments (match this first)
{
regex:
/{\s*\/\*\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?\s*\*\/\s*}/gi,
placeholder: replacement,
},
// C-style comments (C, C++, Java, JavaScript, TypeScript, etc.)
{
regex:
/\/\/\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?/gi,
placeholder: replacement,
},
{
regex:
/\/\*\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?\s*\*\//gi,
placeholder: replacement,
},
// Python, Ruby, R comments
{
regex:
/#\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?/gi,
placeholder: replacement,
},
// HTML-style comments
{
regex:
/<!--\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?\s*-->/gi,
placeholder: replacement,
},
// SQL, Haskell, Lua comments
{
regex:
/--\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?/gi,
placeholder: replacement,
},
// MATLAB comments
{
regex:
/%\s*\.{3}.*(?:rest|unchanged|keep|file|existing|some).*(?:\s*\.{3})?/gi,
placeholder: replacement,
},
]
let updatedContent = content
for (const { regex, placeholder } of commentPatterns) {
updatedContent = updatedContent.replaceAll(regex, placeholder)
}
return updatedContent
}
export const randBoolFromStr = (str: string) => {
return sumBy(str.split(''), (char) => char.charCodeAt(0)) % 2 === 0
}
export const pluralize = (count: number, word: string) => {
if (count === 1) return `${count} ${word}`
// Handle words ending in 'y' (unless preceded by a vowel)
if (word.endsWith('y') && !word.match(/[aeiou]y$/)) {
return `${count} ${word.slice(0, -1) + 'ies'}`
}
// Handle words ending in s, sh, ch, x, z, o
if (word.match(/[sxz]$/) || word.match(/[cs]h$/) || word.match(/o$/)) {
return `${count} ${word + 'es'}`
}
// Handle words ending in f/fe
if (word.endsWith('f')) {
return `${count} ${word.slice(0, -1) + 'ves'}`
}
if (word.endsWith('fe')) {
return `${count} ${word.slice(0, -2) + 'ves'}`
}
return `${count} ${word + 's'}`
}
/**
* Safely replaces all occurrences of a search string with a replacement string,
* escaping special replacement patterns (like $) in the replacement string.
*/
export const capitalize = (str: string) => {
if (!str) return str
return str.charAt(0).toUpperCase() + str.slice(1)
}
export const safeReplace = (
content: string,
searchStr: string,
replaceStr: string
): string => {
const escapedReplaceStr = replaceStr.replace(/\$/g, '$$$$')
return content.replace(searchStr, escapedReplaceStr)
}