Skip to content

Commit

Permalink
APOSTROPHE Added apostrophe words (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrai committed Jan 12, 2021
1 parent d4ec588 commit 44882cd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 34 deletions.
4 changes: 2 additions & 2 deletions distribution/lib/background.bundle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions distribution/lib/main.bundle.js

Large diffs are not rendered by default.

44 changes: 31 additions & 13 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { names } from './name-constants';
import { names } from './name-constants'

const days = [
'Monday',
Expand All @@ -8,7 +8,7 @@ const days = [
'Friday',
'Saturday',
'Sunday',
];
]

const months = [
'January',
Expand All @@ -21,7 +21,7 @@ const months = [
'October',
'November',
'December',
];
]

const common_tech_words = [
'API',
Expand Down Expand Up @@ -73,7 +73,7 @@ const common_tech_words = [
'iPhone',
'iPod',
'iTunes',
];
]

const abbreviations = [
'AFR',
Expand Down Expand Up @@ -152,17 +152,35 @@ const abbreviations = [
'WSL',
'WTF',
'WTH',
];
]

let constants = days.concat(months, abbreviations, common_tech_words);
let words_with_apostrophe = {
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 constants = days.concat(months, abbreviations, common_tech_words)

let constants_map = constants.reduce((obj, val) => {
obj[val.toLowerCase()] = val
return obj
}, {})

//convert array to key-value pairs
export let constants_key_val = constants.reduce((obj, val) => {
obj[val.toLowerCase()] = val;
return obj;
}, {});
export let constants_key_val = { ...constants_map, ...words_with_apostrophe }

export let names_key_val = names.reduce((obj, val) => {
obj[val.toLowerCase()] = val;
return obj;
}, {});
obj[val.toLowerCase()] = val
return obj
}, {})
31 changes: 22 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ export function shouldCapitalise(text) {
return matches;
}

export function getMatchingAndCorrectedWords(text, key_val) {
export function getCaseInsensitiveMatchingAndCorrectedWords(text, key_val) {
return getMatchingAndCorrectedWords(text, key_val, true);
}

export function getCaseSensitiveMatchingAndCorrectedWords(text, key_val) {
return getMatchingAndCorrectedWords(text, key_val, false);
}

export function getMatchingAndCorrectedWords(text, key_val, case_insensitive) {
const lastWordRegex = /\b(\w+)\W$/;

let match = lastWordRegex.exec(text);
Expand All @@ -63,7 +71,10 @@ export function getMatchingAndCorrectedWords(text, key_val) {
const matchedWord = match[1];

if (matchedWord != null) {
let correctedWord = key_val[matchedWord.toLowerCase()];
let correctedWord =
case_insensitive === true
? key_val[matchedWord.toLowerCase()]
: key_val[matchedWord];

if (correctedWord != null) {
return [matchedWord, correctedWord];
Expand Down Expand Up @@ -211,19 +222,21 @@ export function capitaliseText(
return;
}

updateConstant(text, element, tagName, constants_key_val);
const case_sensitive = true;
updateConstant(text, element, tagName, constants_key_val, case_sensitive);

// console.log(should_capitalise_names);
if (should_capitalise_names) {
updateConstant(text, element, tagName, names_key_val);
updateConstant(text, element, tagName, names_key_val, !case_sensitive);
}
}

function updateConstant(text, element, tagName, key_val) {
const [matchedWord, correctedWord] = getMatchingAndCorrectedWords(
text,
key_val
);
function updateConstant(text, element, tagName, key_val, case_sensitive) {
const [matchedWord, correctedWord] =
case_sensitive === true
? getCaseSensitiveMatchingAndCorrectedWords(text, key_val)
: getCaseInsensitiveMatchingAndCorrectedWords(text, key_val);

if (matchedWord !== '') {
if (matchedWord !== correctedWord) {
let updatedStr = text.replace(matchedWord, correctedWord);
Expand Down
37 changes: 29 additions & 8 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ describe('utilities test', function() {
});
});

test('getIndexOfMatchingConstantWord_Days', () => {
test('getCaseInsensitiveMatchingAndCorrectedWords_Days', () => {
let str = 'I\'m the content of html Monday.';
let matchingAndCorrectWords = text =>
utils.getMatchingAndCorrectedWords(text, constants_key_val);
utils.getCaseInsensitiveMatchingAndCorrectedWords(text, constants_key_val);

expect(matchingAndCorrectWords(str)[0]).toBe('Monday');
expect(matchingAndCorrectWords(str)[1]).toBe('Monday');
Expand All @@ -265,10 +265,10 @@ describe('utilities test', function() {
expect(matchingAndCorrectWords(str)[1]).toBe('');
});

test('getIndexOfMatchingConstantWord_Months', () => {
test('getCaseInsensitiveMatchingAndCorrectedWords_Months', () => {
let str = 'I\'m the content of html january.';
let matchingAndCorrectWords = text =>
utils.getMatchingAndCorrectedWords(text, constants_key_val);
utils.getCaseInsensitiveMatchingAndCorrectedWords(text, constants_key_val);

expect(matchingAndCorrectWords(str)[0]).toBe('january');
expect(matchingAndCorrectWords(str)[1]).toBe('January');
Expand All @@ -282,10 +282,10 @@ describe('utilities test', function() {
expect(matchingAndCorrectWords(str)[1]).toBe('');
});

test('getIndexOfMatchingNameWords', () => {
test('getCaseInsensitiveMatchingAndCorrectedWords', () => {
let str = 'I\'m the content of html James.';
let matchingAndCorrectWords = text =>
utils.getMatchingAndCorrectedWords(text, names_key_val);
utils.getCaseInsensitiveMatchingAndCorrectedWords(text, names_key_val);

expect(matchingAndCorrectWords(str)[0]).toBe('James');
expect(matchingAndCorrectWords(str)[1]).toBe('James');
Expand All @@ -303,10 +303,10 @@ describe('utilities test', function() {
expect(matchingAndCorrectWords(str)[1]).toBe('');
});

test('getIndexOfMatchingNameWords_OtherPunctuation', () => {
test('getCaseInsensitiveMatchingAndCorrectedWords_OtherPunctuation', () => {
let str = 'I\'m the content of html \'James\'';
let matchingAndCorrectWords = text =>
utils.getMatchingAndCorrectedWords(text, names_key_val);
utils.getCaseInsensitiveMatchingAndCorrectedWords(text, names_key_val);

expect(matchingAndCorrectWords(str)[0]).toBe('James');
expect(matchingAndCorrectWords(str)[1]).toBe('James');
Expand All @@ -323,4 +323,25 @@ describe('utilities test', function() {
expect(matchingAndCorrectWords(str)[0]).toBe('');
expect(matchingAndCorrectWords(str)[1]).toBe('');
});

test('getCaseSensitiveMatchingAndCorrectedWords_ApostropheWords', () => {
let str = 'I cant.';
let matchingAndCorrectWords = text =>
utils.getCaseSensitiveMatchingAndCorrectedWords(text, constants_key_val);

expect(matchingAndCorrectWords(str)[0]).toBe('cant');
expect(matchingAndCorrectWords(str)[1]).toBe('can\'t');

str = 'I CANT ';
expect(matchingAndCorrectWords(str)[0]).toBe('');
expect(matchingAndCorrectWords(str)[1]).toBe('');

str = 'I wont ';
expect(matchingAndCorrectWords(str)[0]).toBe('wont');
expect(matchingAndCorrectWords(str)[1]).toBe('won\'t');

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

0 comments on commit 44882cd

Please sign in to comment.