Skip to content

Commit 9d9d046

Browse files
author
pr0fix
committed
implement "coolDown"-function w/ tests & update "froth" to accept extended Latin alphabets
1 parent 0b200e3 commit 9d9d046

File tree

7 files changed

+56
-5
lines changed

7 files changed

+56
-5
lines changed

src/coolDown.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function coolDown(str: string): string | null {
2+
if (typeof str !== "string") {
3+
return null;
4+
}
5+
6+
let i = 0;
7+
let result = "";
8+
9+
while (str[i] !== undefined) {
10+
result += String.fromCharCode(
11+
(str.charCodeAt(i) > 64 && str.charCodeAt(i) < 91) ||
12+
(str.charCodeAt(i) > 191 && str.charCodeAt(i) < 224)
13+
? str.charCodeAt(i) + 32
14+
: str.charCodeAt(i)
15+
);
16+
i++;
17+
}
18+
19+
return result;
20+
}

src/froth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* froth(123); // Returns null
1212
* ```
1313
*/
14-
export function froth(str: string) {
14+
export function froth(str: string): string | null {
1515
if (typeof str !== "string") {
1616
return null;
1717
}
@@ -21,7 +21,8 @@ export function froth(str: string) {
2121

2222
while (str[i] !== undefined) {
2323
result += String.fromCharCode(
24-
str.charCodeAt(i) > 96 && str.charCodeAt(i) < 123
24+
(str.charCodeAt(i) > 96 && str.charCodeAt(i) < 123) ||
25+
(str.charCodeAt(i) > 223 && str.charCodeAt(i) < 256)
2526
? str.charCodeAt(i) - 32
2627
: str.charCodeAt(i)
2728
);

src/hasMilk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* ```
1717
*/
1818

19-
export function hasMilk(str: string, word: string) {
19+
export function hasMilk(str: string, word: string): boolean | null {
2020
if (typeof str !== "string" || typeof word !== "string") {
2121
return null;
2222
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { brewLength } from "./brewLength";
22
export { compareBeans } from "./compareBeans";
3+
export { coolDown } from "./coolDown";
34
export { findFirstSip } from "./findFirstSip";
45
export { findFlavor } from "./findFlavor";
56
export { froth } from "./froth";

src/pourCoffee.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* pourCoffee(123); // Returns null
1212
* ```
1313
*/
14-
export function pourCoffee(str: string) {
14+
export function pourCoffee(str: string): string | null {
1515
if (typeof str !== "string") {
1616
return null;
1717
}

tests/coolDown.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { coolDown } from "../src/index";
2+
3+
test("coolDown should return string in lowercase", () => {
4+
expect(coolDown("COFFEE")).toBe("coffee");
5+
expect(coolDown("cOfFeE")).toBe("coffee");
6+
});
7+
8+
test("coolDown should return string with extended Latin alphabets in lowercase", () => {
9+
expect(coolDown("ÄÖÅ")).toBe("äöå");
10+
});
11+
12+
test("coolDown should not return a string containing uppercase letters", () => {
13+
expect(coolDown("coffee")).not.toBe("COFFEE");
14+
expect(coolDown("COFFEE")).not.toBe("COFFEE");
15+
expect(coolDown("cOfFeE")).not.toBe("COFFEE");
16+
});
17+
18+
test("coolDown should return null for non strings", () => {
19+
expect(coolDown(0 as any)).toBeNull();
20+
expect(coolDown(true as any)).toBeNull();
21+
expect(coolDown({} as any)).toBeNull();
22+
expect(coolDown([] as any)).toBeNull();
23+
expect(coolDown(undefined as any)).toBeNull();
24+
expect(coolDown(null as any)).toBeNull();
25+
});

tests/froth.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { froth } from "../src/index";
22

3-
test("froth to return string in uppercase", () => {
3+
test("froth should return string in uppercase", () => {
44
expect(froth("coffee")).toBe("COFFEE");
55
expect(froth("cOfFeE")).toBe("COFFEE");
66
});
77

8+
test("froth should return string with extended Latin alphabets in uppercase", () => {
9+
expect(froth("äöå")).toBe("ÄÖÅ");
10+
});
11+
812
test("froth should not return a string containing lowercase letters", () => {
913
expect(froth("coffee")).not.toBe("coffee");
1014
expect(froth("COFFEE")).not.toBe("coffee");

0 commit comments

Comments
 (0)