Skip to content

Commit

Permalink
Added Title Case Correction For Words With Apostrophe (#91)
Browse files Browse the repository at this point in the history
* CLEANUP Cleanup

* CLEANUP Cleanup

* CLEANUP Cleanup

* CLEANUP Added Title Case Correction For Words With Apostrophe

* CLEANUP Cleanup
  • Loading branch information
hrai committed Mar 14, 2021
1 parent 877789b commit 54b47f8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Firefox/Microsoft Edge Chromium add-on to automatically capitalise words while t
## Features

- Capitalise the first letter of a sentence.
- Capitalise `I`.
- Capitalise constants such as [days](src/constants.js#L4), [months](src/constants.js#L14)
- Capitalise common [abbreviations](src/abbreviation-constants.js#L1)
- Capitalise the word `I`.
- Capitalise common [names](src/name-constants.js#L1)
- Add apostrophe to common [English words](src/constants.js#L99)
- Capitalise common [abbreviations](src/abbreviation-constants.js#L1)
- Capitalise constants such as [days](src/constants.js#L4), [months](src/constants.js#L14)
- Add apostrophe to common [English words](src/constants.js#L27)

## Configuration/Settings

Expand Down
4 changes: 2 additions & 2 deletions distribution/lib/background.bundle.js

Large diffs are not rendered by default.

51 changes: 33 additions & 18 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ const months = [
'December',
]

const wordsWithApostrophe = {
arent: "aren't",
doesnt: "doesn't",
cant: "can't",
wont: "won't",
dont: "don't",
shes: "she's",
hes: "he's",
theres: "there's",
theyre: "they're",
youve: "you've",
youre: "you're",
couldnt: "couldn't",
shouldnt: "shouldn't",
wouldnt: "wouldn't",
}

const commonTechWords = [
'AI',
'API',
Expand Down Expand Up @@ -99,23 +116,6 @@ const commonTechWords = [
'iTunes',
]

let wordsWithApostrophe = {
arent: "aren't",
doesnt: "doesn't",
cant: "can't",
wont: "won't",
dont: "don't",
shes: "she's",
hes: "he's",
theres: "there's",
theyre: "they're",
youve: "you've",
youre: "you're",
couldnt: "couldn't",
shouldnt: "shouldn't",
wouldnt: "wouldn't",
}

let commonLocalAbbreviations = ['Syd', 'Melb']

let constants = days.concat(months, commonTechWords, commonLocalAbbreviations)
Expand All @@ -127,8 +127,23 @@ let stringToKeyValuePairs = (obj, val) => {

let constantsMap = constants.reduce(stringToKeyValuePairs, {})

let toTitleCase = (str) => {
return str.replace(/\w\S*/g, (txt) => {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
})
}

let wordsWithApostropheTitleCase = {}
for (const [key, value] of Object.entries(wordsWithApostrophe)) {
wordsWithApostropheTitleCase[toTitleCase(key)] = toTitleCase(value)
}

//convert array to key-value pairs
export let constantsKeyValuePairs = { ...constantsMap, ...wordsWithApostrophe }
export let constantsKeyValuePairs = {
...constantsMap,
...wordsWithApostrophe,
...wordsWithApostropheTitleCase,
}

export let namesKeyValuePairs = names.reduce(stringToKeyValuePairs, {})
export let abbreviationsKeyValuePairs = abbreviations.reduce(
Expand Down
36 changes: 24 additions & 12 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../src/constants.js';
const $ = require('jquery');

describe('utilities test', function() {
describe('utilities test', function () {
test('getCapitalisedContent', () => {
expect(utils.getCapitalisedContent('blah')).toBe('blaH');
expect(utils.getCapitalisedContent('i')).toBe('I');
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('utilities test', function() {

test('getCaseInsensitiveMatchingAndCorrectedWords_Days', () => {
let str = 'I\'m the content of html Monday.';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
constantsKeyValuePairs
Expand All @@ -274,7 +274,7 @@ describe('utilities test', function() {

test('getCaseInsensitiveMatchingAndCorrectedWords_Months', () => {
let str = 'I\'m the content of html january.';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
constantsKeyValuePairs
Expand All @@ -294,7 +294,7 @@ describe('utilities test', function() {

test('getCaseInsensitiveMatchingAndCorrectedWords', () => {
let str = 'I\'m the content of html James.';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
namesKeyValuePairs
Expand All @@ -318,7 +318,7 @@ describe('utilities test', function() {

test('getCaseInsensitiveMatchingAndCorrectedWords_OtherPunctuation', () => {
let str = 'I\'m the content of html \'James\'';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
namesKeyValuePairs
Expand All @@ -342,7 +342,7 @@ describe('utilities test', function() {

test('getCaseInsensitiveMatchingAndCorrectedWords_CompanyNames', () => {
let str = 'I\'m the content of html \'GitHub\'';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
namesKeyValuePairs
Expand All @@ -366,7 +366,7 @@ describe('utilities test', function() {

test('getCaseInsensitiveMatchingAndCorrectedWords_Abbreviations', () => {
let str = 'I\'m the content of html.';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
abbreviationsKeyValuePairs
Expand All @@ -390,7 +390,7 @@ describe('utilities test', function() {

test('getCaseSensitiveMatchingAndCorrectedWords_ApostropheWords', () => {
let str = 'I cant.';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseSensitiveMatchingAndCorrectedWords(
text,
constantsKeyValuePairs
Expand All @@ -406,15 +406,27 @@ describe('utilities test', function() {
str = 'I wont ';
expect(matchingAndCorrectWords(str)[0]).toBe('wont');
expect(matchingAndCorrectWords(str)[1]).toBe('won\'t');
});

test('getCaseSensitiveMatchingAndCorrectedWords_ApostropheWords_TitleCase', () => {
let str = 'Doesnt.';
let matchingAndCorrectWords = (text) =>
utils.getCaseSensitiveMatchingAndCorrectedWords(
text,
constantsKeyValuePairs
);

expect(matchingAndCorrectWords(str)[0]).toBe('Doesnt');
expect(matchingAndCorrectWords(str)[1]).toBe('Doesn\'t');

str = 'I Wont.';
expect(matchingAndCorrectWords(str)[0]).toBe('');
expect(matchingAndCorrectWords(str)[1]).toBe('');
expect(matchingAndCorrectWords(str)[0]).toBe('Wont');
expect(matchingAndCorrectWords(str)[1]).toBe('Won\'t');
});

test('getCaseInsensitiveMatchingAndCorrectedWords_LocalAbbreviations', () => {
let str = 'I\'m the content of html \'syd\'';
let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getCaseInsensitiveMatchingAndCorrectedWords(
text,
constantsKeyValuePairs
Expand All @@ -433,7 +445,7 @@ describe('utilities test', function() {
let wordsToExclude = ['january'];
let caseInsensitive = true;

let matchingAndCorrectWords = text =>
let matchingAndCorrectWords = (text) =>
utils.getMatchingAndCorrectedWords(
text,
constantsKeyValuePairs,
Expand Down

0 comments on commit 54b47f8

Please sign in to comment.