Skip to content

Commit

Permalink
CAP-LAST Capitalising Last Word Only Instead Of All The Occurrences O…
Browse files Browse the repository at this point in the history
…f The Word (#89)

* CAP-LAST Capitalising Last Word Only Instead Of All The Occurrences Of The Word

* CAP-LAST Added Tests
  • Loading branch information
hrai committed Mar 14, 2021
1 parent 84d87ab commit d51709f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
4 changes: 2 additions & 2 deletions distribution/lib/main.bundle.js

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,25 @@ function updateConstant(text, element, tagName, keyValuePairs, caseSensitive) {

if (matchedWord !== '') {
if (matchedWord !== correctedWord) {
let updatedStr = text.replace(matchedWord, correctedWord);
const updatedStr = getUpdatedString(text, matchedWord, correctedWord);
setText(element, tagName, updatedStr, false);
}
}
}

export function getUpdatedString(text, matchedWord, correctedWord) {
if (text && matchedWord && correctedWord) {
const splitAt = index => x => [x.slice(0, index), x.slice(index)];
const arr = splitAt(-1)(text);

const updatedStr =
arr[0].replace(new RegExp(matchedWord + '$'), correctedWord) + arr[1];
return updatedStr;
}

return text;
}

export function getCapitalisedContentForI(text) {
const lastTwoChars = text.slice(-2);
const updatedStr =
Expand Down
55 changes: 45 additions & 10 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 @@ -414,7 +414,7 @@ describe('utilities test', function () {

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 +433,7 @@ describe('utilities test', function () {
let wordsToExclude = ['january'];
let caseInsensitive = true;

let matchingAndCorrectWords = (text) =>
let matchingAndCorrectWords = text =>
utils.getMatchingAndCorrectedWords(
text,
constantsKeyValuePairs,
Expand All @@ -448,4 +448,39 @@ describe('utilities test', function () {
expect(matchingAndCorrectWords(str)[0]).toBe('');
expect(matchingAndCorrectWords(str)[1]).toBe('');
});

test('getUpdatedString_ChangesSentence', () => {
expect(
utils.getUpdatedString('these es indexes are as es ', 'es', 'ES')
).toBe('these es indexes are as ES ');
expect(
utils.getUpdatedString('these es indexes are as es!', 'es', 'ES')
).toBe('these es indexes are as ES!');
expect(
utils.getUpdatedString('these es indexes are as es?', 'es', 'ES')
).toBe('these es indexes are as ES?');

expect(
utils.getUpdatedString('these james and james ', 'james', 'James')
).toBe('these james and James ');
expect(
utils.getUpdatedString('these james and james.', 'james', 'James')
).toBe('these james and James.');
expect(
utils.getUpdatedString('these james and james!', 'james', 'James')
).toBe('these james and James!');
});

test('getUpdatedString_DoesNotChangeSentence', () => {
expect(utils.getUpdatedString('these james and ', 'james', 'James')).toBe(
'these james and '
);
expect(utils.getUpdatedString('these james and james!', '', '')).toBe(
'these james and james!'
);
expect(utils.getUpdatedString('', '', '')).toBe('');
expect(
utils.getUpdatedString('these james and james!', '', undefined)
).toBe('these james and james!');
});
});

0 comments on commit d51709f

Please sign in to comment.