Skip to content

Commit 7f98537

Browse files
committed
fix: create 2 more tests for special cases and change the function so they all pass
1 parent b9967fa commit 7f98537

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
const charRegex = new RegExp(findCharacter, "g");
2+
const metaCharRegex = /[.^$*+?{}\[\]\\|()]/g;
3+
const safeChar = findCharacter.replace(metaCharRegex, `\\$&`);
4+
const charRegex = new RegExp(safeChar, "g");
35
const charArr = [...stringOfCharacters.matchAll(charRegex)];
46
return charArr.length;
57
}

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,19 @@ test("should count 0 occurrences of a character", () => {
2929
const count = countChar(str, char);
3030
expect(count).toEqual(0);
3131
});
32+
33+
test("should be case sensitive and return only occurrences of same case characters", () => {
34+
const str = "Dodgers";
35+
const char = "D";
36+
const count = countChar(str, char);
37+
expect(count).toEqual(1);
38+
});
39+
40+
test("should count occurrences of regex metacharacters", () => {
41+
const str = "a*b*c";
42+
const char = "*";
43+
const count = countChar(str, char);
44+
expect(count).toEqual(2);
45+
});
46+
47+
// metacharacters . ^ $ * + ? { } [ ] \ | ( )

0 commit comments

Comments
 (0)