Skip to content

Commit f6798c9

Browse files
MC-1594: fix english title apostrophe formatting (#1224)
1 parent 7d4c610 commit f6798c9

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/_shared/utils/applyApTitleCase.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('applyApTitleCase', () => {
130130
const sentencesWithContractions = [
131131
{
132132
result: "Here's what you haven't noticed 'foo bar' foo'S: foo Bar",
133-
expected: "Here's What You Haven't Noticed 'Foo Bar' Foo'S: Foo Bar",
133+
expected: "Here's What You Haven't Noticed 'Foo Bar' Foo's: Foo Bar",
134134
},
135135
];
136136
sentencesWithContractions.forEach((swc) => {
@@ -145,7 +145,9 @@ describe('lowercaseAfterApostrophe', () => {
145145
expect(result).toEqual("foo's");
146146
});
147147
it('lowercase letter after apostrophe, ignore string in quotes, & return new string', () => {
148-
const result = lowercaseAfterApostrophe("'Foo' foo'S DaY's");
149-
expect(result).toEqual("'Foo' foo's DaY's");
148+
const result = lowercaseAfterApostrophe(
149+
"'Foo' foo'S DaY's You'Ll 'foo Bar foo'Ss'",
150+
);
151+
expect(result).toEqual("'Foo' foo's DaY's You'll 'foo Bar foo'ss'");
150152
});
151153
});

src/_shared/utils/applyApTitleCase.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@ export const SEPARATORS = /(:\s*|\s+|[-‑–—,:;!?()“”'‘"])/; // Includ
99
export const stop = STOP_WORDS.split(' ');
1010

1111
/**
12-
* Format a string: Capture the letter after an apostrophe at the end of a
13-
* sentence (without requiring a space) or with a white space following the letter.
12+
* Format a string: Match the letter after an apostrophe & capture the apostrophe and matched char.
1413
* Lowercase the captured letter & return the formatted string.
1514
* @param input
1615
* @returns {string}
1716
*/
1817
export const lowercaseAfterApostrophe = (input: string): string => {
19-
// matches a char (num or letter) right after an apostrophe,
20-
// only if the apostrophe is preceded by a char & is followed
21-
// by a space or end of the str.
22-
const regex = /(?<=\w)'(\w)(?=\s|$)/g;
18+
// matches an apostrophe followed by a char
19+
// ensures only the first char after the apostrophe is converted to lowercase
20+
const regex = /(?<=\w)(')(\w)/g;
2321

24-
return input.replace(regex, (match, p1) => {
25-
return `'${p1.toLowerCase()}`; // Replace with the apostrophe and the lowercase letter
22+
return input.replace(regex, (match, p1, char) => {
23+
return `'${char.toLowerCase()}`; // Lowercase the first char after the apostrophe
2624
});
2725
};
2826

0 commit comments

Comments
 (0)