Skip to content

Commit

Permalink
MC-1594: fix english title apostrophe formatting (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
katerinachinnappan authored Nov 19, 2024
1 parent 7d4c610 commit f6798c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/_shared/utils/applyApTitleCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('applyApTitleCase', () => {
const sentencesWithContractions = [
{
result: "Here's what you haven't noticed 'foo bar' foo'S: foo Bar",
expected: "Here's What You Haven't Noticed 'Foo Bar' Foo'S: Foo Bar",
expected: "Here's What You Haven't Noticed 'Foo Bar' Foo's: Foo Bar",
},
];
sentencesWithContractions.forEach((swc) => {
Expand All @@ -145,7 +145,9 @@ describe('lowercaseAfterApostrophe', () => {
expect(result).toEqual("foo's");
});
it('lowercase letter after apostrophe, ignore string in quotes, & return new string', () => {
const result = lowercaseAfterApostrophe("'Foo' foo'S DaY's");
expect(result).toEqual("'Foo' foo's DaY's");
const result = lowercaseAfterApostrophe(
"'Foo' foo'S DaY's You'Ll 'foo Bar foo'Ss'",
);
expect(result).toEqual("'Foo' foo's DaY's You'll 'foo Bar foo'ss'");
});
});
14 changes: 6 additions & 8 deletions src/_shared/utils/applyApTitleCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ export const SEPARATORS = /(:\s*|\s+|[-‑–—,:;!?()“”'‘"])/; // Includ
export const stop = STOP_WORDS.split(' ');

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

return input.replace(regex, (match, p1) => {
return `'${p1.toLowerCase()}`; // Replace with the apostrophe and the lowercase letter
return input.replace(regex, (match, p1, char) => {
return `'${char.toLowerCase()}`; // Lowercase the first char after the apostrophe
});
};

Expand Down

0 comments on commit f6798c9

Please sign in to comment.