-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-transform.ts
More file actions
236 lines (221 loc) · 7.56 KB
/
Copy pathtext-transform.ts
File metadata and controls
236 lines (221 loc) · 7.56 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import type * as Types from '@app/types.ts'
/**
* Unicode-aware text case transformer.
* @description Applies case transformations using grapheme segmentation.
*/
export class TextTransform {
/** ASCII letter detection pattern */
private static readonly alphaPattern: RegExp = /[a-zA-Z]/
/** Unicode grapheme cluster segmenter */
private static readonly segmenter: Intl.Segmenter = new Intl.Segmenter(undefined, {
granularity: 'grapheme'
})
/** Sentence start detection pattern */
private static readonly sentencePattern: RegExp = /(^\s*\p{L}|[.!?]\s+\p{L})/gu
/**
* Apply alternating lower-upper case.
* @description Toggles case on each alphabetic character sequentially.
* @param text - Input string to transform
* @returns Result with alternating-cased text or error
*/
static alternatingCase(text: string): Types.TextshiftResult {
return TextTransform.withValidation(text, (validText) => {
let alphaIndex: number = 0
return TextTransform.toGraphemes(validText).map((grapheme) => {
if (TextTransform.alphaPattern.test(grapheme)) {
const transformed: string = alphaIndex % 2 === 0
? grapheme.toLowerCase()
: grapheme.toUpperCase()
alphaIndex++
return transformed
}
return grapheme
}).join('')
})
}
/**
* Capitalize first letter per word.
* @description Uppercases first character, lowercases rest per word.
* @param text - Input string to transform
* @returns Result with capitalized text or error
*/
static capitalizedCase(text: string): Types.TextshiftResult {
return TextTransform.withValidation(
text,
(validText) => TextTransform.mapWords(validText, (word) => TextTransform.capitalizeWord(word))
)
}
/**
* Swap upper and lower case.
* @description Inverts case of every alphabetic character.
* @param text - Input string to transform
* @returns Result with inverse-cased text or error
*/
static inverseCase(text: string): Types.TextshiftResult {
return TextTransform.withValidation(
text,
(validText) =>
TextTransform.toGraphemes(validText).map((grapheme) => {
const lower: string = grapheme.toLowerCase()
const upper: string = grapheme.toUpperCase()
if (grapheme === upper && grapheme !== lower) {
return lower
}
if (grapheme === lower && grapheme !== upper) {
return upper
}
return grapheme
}).join('')
)
}
/**
* Convert text to lowercase.
* @description Lowercases all characters in the string.
* @param text - Input string to transform
* @returns Result with lowercased text or error
*/
static lowercase(text: string): Types.TextshiftResult {
return TextTransform.withValidation(text, (validText) => validText.toLowerCase())
}
/**
* Normalize whitespace and blank lines.
* @description Collapses spaces, trims lines, limits consecutive newlines.
* @param text - Input string to normalize
* @returns Result with normalized text or error
*/
static normalize(text: string): Types.TextshiftResult {
return TextTransform.withValidation(text, (validText) =>
validText
.split(/\r?\n/)
.map((line) => line.replace(/[ \t]+/g, ' ').trim())
.join('\n')
.replace(/\n{3,}/g, '\n\n')
.trim())
}
/**
* Reverse grapheme cluster order.
* @description Reverses text preserving Unicode grapheme clusters.
* @param text - Input string to reverse
* @returns Result with reversed text or error
*/
static reverse(text: string): Types.TextshiftResult {
return TextTransform.withValidation(
text,
(validText) => TextTransform.toGraphemes(validText).toReversed().join('')
)
}
/**
* Capitalize first letter per sentence.
* @description Lowercases text then uppercases sentence starts.
* @param text - Input string to transform
* @returns Result with sentence-cased text or error
*/
static sentenceCase(text: string): Types.TextshiftResult {
return TextTransform.withValidation(
text,
(validText) =>
TextTransform.mapLines(validText, (trimmedLine) =>
trimmedLine.toLowerCase().replace(
TextTransform.sentencePattern,
(match) => match.toUpperCase()
))
)
}
/**
* Convert text to uppercase.
* @description Uppercases all characters in the string.
* @param text - Input string to transform
* @returns Result with uppercased text or error
*/
static uppercase(text: string): Types.TextshiftResult {
return TextTransform.withValidation(text, (validText) => validText.toUpperCase())
}
/**
* Capitalize a single word.
* @description Uppercases first grapheme, lowercases the rest.
* @param word - Single word to capitalize
* @returns Word with first letter uppercased
*/
private static capitalizeWord(word: string): string {
const graphemes: string[] = TextTransform.toGraphemes(word)
if (graphemes.length === 0) {
return word
}
return graphemes[0]!.toUpperCase() + graphemes.slice(1).join('').toLowerCase()
}
/**
* Apply mapper to each text line.
* @description Preserves indentation while transforming trimmed content.
* @param text - Multi-line input string
* @param lineMapper - Transform function for trimmed lines
* @returns Text with each line transformed
*/
private static mapLines(
text: string,
lineMapper: (trimmedLine: string) => string
): string {
return text.split(/\r?\n/).map((line) => {
if (line.trim() === '') {
return line
}
const indentation: string = /^\s*/.exec(line)![0]!
return indentation + lineMapper(line.trim())
}).join('\n')
}
/**
* Apply mapper to each word.
* @description Splits lines into words and transforms each.
* @param text - Input string to process
* @param wordMapper - Transform function for each word
* @returns Text with each word transformed
*/
private static mapWords(
text: string,
wordMapper: (word: string) => string
): string {
return TextTransform.mapLines(text, (trimmedLine) => {
const words: string[] = TextTransform.splitWords(trimmedLine)
if (words.length === 0) {
return trimmedLine
}
return words.map(wordMapper).join(' ')
})
}
/**
* Split text into word tokens.
* @description Normalizes whitespace and splits into non-empty words.
* @param text - Input string to split
* @returns Array of non-empty word strings
*/
private static splitWords(text: string): string[] {
return text.replace(/\s+/g, ' ').split(/\s+/).filter((w) => w.length > 0)
}
/**
* Split text into grapheme clusters.
* @description Uses Intl.Segmenter for Unicode-safe splitting.
* @param text - Input string to segment
* @returns Array of grapheme cluster strings
*/
private static toGraphemes(text: string): string[] {
return Array.from(TextTransform.segmenter.segment(text), (s) => s.segment)
}
/**
* Validate input and apply transform.
* @description Checks string type, handles empty, then transforms.
* @param text - Input string to validate
* @param transform - Transform function for valid input
* @returns Result with transformed text or error
*/
private static withValidation(
text: string,
transform: (validText: string) => string
): Types.TextshiftResult {
if (typeof text !== 'string') {
return { error: 'Input must be a string' }
}
if (text === '') {
return { data: '' }
}
return { data: transform(text) }
}
}