@@ -9,3 +9,111 @@ export const pascalToUpperSnake = (str: string): string => {
9
9
. toUpperCase ( ) // Converts everything to uppercase
10
10
. replace ( / ^ _ / , '' ) ; // Removes the initial underscore if it exists
11
11
} ;
12
+
13
+
14
+ /**
15
+ * An array of strings containing various special character patterns and names.
16
+ * The strings in this array include names or identifiers preceded by
17
+ * special characters such as dashes, question marks, or periods.
18
+ * Some entries also contain only special characters or brace-like structures.
19
+ */
20
+ const specialChars = [
21
+ '—Huggo' ,
22
+ '?—Nick Riganas' ,
23
+ '.—Rhino' ,
24
+ '—Eric Johnson' ,
25
+ '—Jwelch5742' ,
26
+ '—yusufpiskin' ,
27
+ '—Maths Jesperson' ,
28
+ '—Mark Logan' ,
29
+ '—Snow Leopard' ,
30
+ '—Claudio Carvalho, Rio de Janeiro, Brazil' ,
31
+ '—Johnny-the-Film-Sentinel-2187' ,
32
+ '—grantss' ,
33
+ '—Ed Stephan' ,
34
+ '—filmfactsman' ,
35
+ '—Col Needham' ,
36
+ '—Tony Fontana' ,
37
+ '—garykmcd' ,
38
+ '—' ,
39
+ '?—' ,
40
+ '.—' ,
41
+ '{}' ,
42
+ ] ;
43
+
44
+
45
+ /**
46
+ * A regular expression pattern used for matching and validating email addresses.
47
+ *
48
+ * This pattern supports common email formats, where:
49
+ * - The local part can contain alphanumeric characters, dots, underscores,
50
+ * percent signs, plus signs, and hyphens.
51
+ * - The domain part is structured as one or more labels separated by dots,
52
+ * with each label containing alphanumeric characters or hyphens.
53
+ * - The top-level domain must be at least two characters long and consist
54
+ * only of alphabetic characters.
55
+ *
56
+ * Flags:
57
+ * - The "g" flag enables global matching, allowing multiple occurrences
58
+ * of email addresses to be matched in a single string.
59
+ */
60
+ const emailRegex = / \b [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 . _ % + - ] * @ (?: [ a - z A - Z 0 - 9 - ] + \. ) + [ a - z A - Z ] { 2 , } \b / g;
61
+
62
+
63
+ /**
64
+ * Removes special characters from the given text string and returns the cleaned string.
65
+ *
66
+ * @param {string } text - The input string from which special characters will be removed.
67
+ * @return {string } - The resulting string after special characters are removed.
68
+ */
69
+ function removeSpecialChars ( text : string ) : string {
70
+ // Loop through the special characters and replace them with spaces
71
+ for ( const char of specialChars ) {
72
+ text = text . replace ( char , '' ) . trim ( ) ;
73
+ }
74
+ return text ;
75
+ }
76
+
77
+ /**
78
+ * Detects whether the given text contains a valid email address pattern.
79
+ *
80
+ * @param {string } text - The string to be checked for an email address.
81
+ * @return {boolean } Returns `true` if the text contains a valid email address, otherwise `false`.
82
+ */
83
+ function detectEmail ( text : string ) : boolean {
84
+ return emailRegex . test ( text ) ;
85
+ }
86
+
87
+ /**
88
+ * Removes an email address from the provided text string, if present.
89
+ *
90
+ * @param {string } text - The input string potentially containing an email address.
91
+ * @return {string } The modified string with the email address removed and trimmed of extra spaces.
92
+ */
93
+ function removeEmail ( text : string ) : string {
94
+ return text . replace ( emailRegex , '' ) . trim ( ) ;
95
+ }
96
+
97
+ /**
98
+ * Processes and trims extra content from a publication text, including special characters,
99
+ * email addresses, and patterns at the end of the text.
100
+ *
101
+ * @param {string } text - The input string representing the publication content to be processed.
102
+ * @returns {string } The processed and cleaned text after applying trimming rules.
103
+ */
104
+ export const trimPublicationContentExtraText = ( text : string ) : string => {
105
+ const hasEmail = detectEmail ( text ) ;
106
+
107
+ if ( hasEmail ) {
108
+ text = removeEmail ( text ) ;
109
+ }
110
+
111
+ let cleanedText = removeSpecialChars ( text ) ;
112
+
113
+ // Verify if the last character is a period, if not, add one
114
+ if ( ! cleanedText . endsWith ( '.' ) ) {
115
+ cleanedText += '.' ;
116
+ }
117
+
118
+ return `${ cleanedText } ` ;
119
+ } ;
0 commit comments