Skip to content

Commit 848194d

Browse files
author
pr0fix
committed
implement "refillCup" method
1 parent fada2e5 commit 848194d

File tree

4 files changed

+97
-17
lines changed

4 files changed

+97
-17
lines changed

README.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ To install caffeinated-strings, run the following command:
1818

1919
## 📚 Functions
2020

21-
| Traditional Name | Coffee Name | Description |
22-
| ---------------- | -------------- | -------------------------------------------------------------------------------------------------------------------- |
23-
| `.charAt()` | `sipAt` | Returns the character of a string at given index (like taking a single sip of coffee). |
24-
| `.concat()` | `addMilk` | Concatenates a string to another separated with delimiter (like pouring milk into a cup of coffee). |
25-
| `.includes()` | `hasMilk` | Returns true if given string is found within this string and false if not (like checking if coffee has milk or not). |
26-
| `.length` | `brewLength` | Returns the length of a string (how "long" the brew is). |
27-
| `=` | `pourCoffee` | Copies one string to another (like pouring coffee into a new cup). |
28-
| `===, >, <` | `compareBeans` | Compares two strings (like comparing coffee beans for quality). |
29-
| `.indexOf()` | `findFirstSip` | Finds the first occurrence of a **character** in a string (like the first sip of coffee). |
30-
| `.indexOf()` | `findFlavor` | Finds the first occurrence of a **substring** inside a string (like detecting flavors in coffee). |
31-
| `.toLowerCase()` | `coolDown` | Transforms the string to lowercase (like cooling down a cup of coffee). |
32-
| `.toUpperCase()` | `froth` | Transforms the string to uppercase (like frothing the milk to create a creamy top). |
21+
| Traditional Name | Coffee Name | Description |
22+
| ---------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
23+
| `.charAt()` | `sipAt` | Returns the character of a string at given index (like taking a single sip of coffee). |
24+
| `.concat()` | `addMilk` | Concatenates a string to another separated with delimiter (like pouring milk into a cup of coffee). |
25+
| `.includes()` | `hasMilk` | Returns true if given string is found within this string and false if not (like checking if coffee has milk or not). |
26+
| `.length` | `brewLength` | Returns the length of a string (how "long" the brew is). |
27+
| `=` | `pourCoffee` | Copies one string to another (like pouring coffee into a new cup). |
28+
| `===, >, <` | `compareBeans` | Compares two strings (like comparing coffee beans for quality). |
29+
| `.indexOf()` | `findFirstSip` | Finds the first occurrence of a **character** in a string (like the first sip of coffee). |
30+
| `.indexOf()` | `findFlavor` | Finds the first occurrence of a **substring** inside a string (like detecting flavors in coffee). |
31+
| `.repeat()` | `refillCup` | Constructs a new string which contains a specified number of copies of the input string (like refilling a coffee cup a specific amount of times). |
32+
| `.toLowerCase()` | `coolDown` | Transforms the string to lowercase (like cooling down a cup of coffee). |
33+
| `.toUpperCase()` | `froth` | Transforms the string to uppercase (like frothing the milk to create a creamy top). |
3334

3435
## 🔧 Usage
3536

@@ -112,16 +113,24 @@ let drink = "cappuccino";
112113
console.log(findFlavor(drink, "cc")); // "ccuccino"
113114
```
114115

115-
9. `coolDown(str)`
116-
Transforms the string to lowercase (like cooling down a cup of coffee).
117-
Returns the input in lowercase as `string`:
116+
9. `refillCup(str, count)`
117+
Constructs a new string with copies of input string the amount of times specified. Returns the new string concatenated together:
118+
119+
```js
120+
let coffee = "cappuccino ";
121+
console.log(refillCup(drink, 3)); // "cappuccino cappuccino cappuccino "
122+
```
123+
124+
10. `coolDown(str)`
125+
Transforms the string to lowercase (like cooling down a cup of coffee).
126+
Returns the input in lowercase as `string`:
118127

119128
```js
120129
let drink = "ESPRESSO";
121130
console.log(froth(drink)); // "espresso"
122131
```
123132

124-
10. `froth(str)`
133+
11. `froth(str)`
125134
Transforms the string to uppercase (like frothing the milk to create a creamy top).
126135
Returns the input in uppercase as `string`:
127136

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { coolDown } from "./coolDown";
55
export { findFirstSip } from "./findFirstSip";
66
export { findFlavor } from "./findFlavor";
77
export { froth } from "./froth";
8+
export { hasMilk } from "./hasMilk";
89
export { pourCoffee } from "./pourCoffee";
10+
export { refillCup } from "./refillCup";
911
export { sipAt } from "./sipAt";
10-
export { hasMilk } from "./hasMilk";

src/refillCup.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Constructs a new string which contains the specified number of copies of the input string.
3+
*
4+
* @param str The input string
5+
* @param count The input count
6+
*
7+
* @returns A string with specified number of copies of input string, concatenated together.
8+
*
9+
* @example
10+
* ```typescript
11+
* refillCup("coffee ", 2); // Returns "coffee coffee "
12+
* refillCup("coffee ", -1); // Returns null
13+
* refillCup("", 2); // Returns null
14+
* ```
15+
*/
16+
export function refillCup(str: string, count: number): string | null {
17+
if (typeof str !== "string" || typeof count !== "number" || count <= 0) {
18+
return null;
19+
}
20+
21+
let isEmpty = true;
22+
23+
for (const _ of str) {
24+
isEmpty = false;
25+
break;
26+
}
27+
28+
if (isEmpty) {
29+
return null;
30+
}
31+
32+
let result = "";
33+
34+
while (count > 0) {
35+
result += str;
36+
count--;
37+
}
38+
39+
return result;
40+
}

tests/refillCup.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { refillCup } from "../src/index";
2+
3+
test("refillCup should return the correct string the right amount of times", () => {
4+
expect(refillCup("coffee ", 6)).toBe(
5+
"coffee coffee coffee coffee coffee coffee "
6+
);
7+
expect(refillCup("cappuccino", 2)).toBe("cappuccinocappuccino");
8+
});
9+
10+
test("refillCup should work with extended Latin alphabets and special characters", () => {
11+
expect(refillCup("café", 2)).toBe("cafécafé");
12+
expect(refillCup("123!@#ääööåå", 2)).toBe("123!@#ääööåå123!@#ääööåå");
13+
});
14+
15+
test("refillCup should return empty string when the input string is empty", () => {
16+
expect(refillCup("", 500)).toBe(null);
17+
});
18+
19+
test("refillCup should return null for count lower and equal to 0", () => {
20+
expect(refillCup("coffee", -100)).toBe(null);
21+
});
22+
23+
test("refillCup should return null for non string or number values", () => {
24+
expect(refillCup(0 as any, "cappuccino" as any)).toBeNull();
25+
expect(refillCup("cappuccino" as any, true as any)).toBeNull();
26+
expect(refillCup({} as any, "cappuccino" as any)).toBeNull();
27+
expect(refillCup("cappuccino" as any, [] as any)).toBeNull();
28+
expect(refillCup(undefined as any, "cappuccino" as any)).toBeNull();
29+
expect(refillCup("cappuccino" as any, null as any)).toBeNull();
30+
});

0 commit comments

Comments
 (0)