Skip to content

Commit 92c7f3c

Browse files
author
pr0fix
committed
implement "addMilk"-function w/ tests & update README
1 parent 1de7696 commit 92c7f3c

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ To install caffeinated-strings, run the following command:
2121
| Traditional Name | Coffee Name | Description |
2222
| ---------------- | -------------- | -------------------------------------------------------------------------------------------------------------------- |
2323
| `.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). |
2425
| `.includes()` | `hasMilk` | Returns true if given string is found within this string and false if not (like checking if coffee has milk or not). |
2526
| `.length` | `brewLength` | Returns the length of a string (how "long" the brew is). |
2627
| `=` | `pourCoffee` | Copies one string to another (like pouring coffee into a new cup). |
@@ -43,7 +44,18 @@ let brew = "coffee";
4344
console.log(sipAt(brew, 2)); // f
4445
```
4546

46-
2. `hasMilk(str, word)`
47+
2. `addMilk(str1, str2, delimiter)`
48+
Concatenates a string to another separated with delimiter (like pouring milk into a cup of coffee).
49+
Returns a `string`:
50+
51+
```js
52+
let brew = "coffee";
53+
let milk = "tastes very good with milk";
54+
let delimiter = ", ";
55+
console.log(addMilk(brew, milk, delimiter)); // coffee, tastes very good with milk
56+
```
57+
58+
3. `hasMilk(str, word)`
4759
Checks if word is found within string (like checking if coffee has milk or not).
4860
Returns `true` if word is in string, `false` if not:
4961

@@ -53,7 +65,7 @@ let word = "good";
5365
console.log(hasMilk(sentence, word)); // true
5466
```
5567

56-
3. `brewLength(str)`
68+
4. `brewLength(str)`
5769
Calculates the length of the input string (how "long" the brew is).
5870
Returns the length as `number`:
5971

@@ -62,7 +74,7 @@ let brew = "coffee";
6274
console.log(brewLength(brew)); // 6
6375
```
6476

65-
4. `pourCoffee(str)`
77+
5. `pourCoffee(str)`
6678
Copies one string to another (like pouring coffee into a new cup).
6779
Returns the copy as `string`:
6880

@@ -72,7 +84,7 @@ let cup = pourCoffee(original);
7284
console.log(cup); // "latte"
7385
```
7486

75-
5. `compareBeans(str1, str2)`
87+
6. `compareBeans(str1, str2)`
7688
Compares two strings (like comparing coffee beans for quality). Returns a `number`:
7789

7890
- `0` if the strings are identical.
@@ -84,23 +96,23 @@ let result = compareBeans("espresso", "latte");
8496
console.log(result); // 1 ('e' > 'l')
8597
```
8698

87-
6. `findFirstSip(str, char)`
99+
7. `findFirstSip(str, char)`
88100
Finds the first occurrence of a character in a string (like the first sip of coffee). Returns the substring from the first occurrence of the character as `string`:
89101

90102
```js
91103
let coffee = "macchiato";
92104
console.log(findFirstSip(coffee, "c")); // "cchiato"
93105
```
94106

95-
7. `findFlavor(str, substr)`
107+
8. `findFlavor(str, substr)`
96108
Finds a substring inside a string (like detecting flavors in coffee). Returns the substring starting from the first match as `string`:
97109

98110
```js
99111
let drink = "cappuccino";
100112
console.log(findFlavor(drink, "cc")); // "ccuccino"
101113
```
102114

103-
8. `coolDown(str)`
115+
9. `coolDown(str)`
104116
Transforms the string to lowercase (like cooling down a cup of coffee).
105117
Returns the input in lowercase as `string`:
106118

@@ -109,9 +121,9 @@ let drink = "ESPRESSO";
109121
console.log(froth(drink)); // "espresso"
110122
```
111123

112-
9. `froth(str)`
113-
Transforms the string to uppercase (like frothing the milk to create a creamy top).
114-
Returns the input in uppercase as `string`:
124+
10. `froth(str)`
125+
Transforms the string to uppercase (like frothing the milk to create a creamy top).
126+
Returns the input in uppercase as `string`:
115127

116128
```js
117129
let drink = "espresso";

src/addMilk.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Concatenates a string to another separated with delimiter.
3+
*
4+
* @param str1 The first input string.
5+
* @param str2 The seconds input string.
6+
* @param delimiter The delimiter to use in between input strings.
7+
*
8+
* @returns A string containing both input strings and delimiter in between, or `null` if either of inputs isn't a string.
9+
*
10+
* @example
11+
* ```typescript
12+
* addMilk("coffee tastes", "very good with milk", ", "); // Returns "coffee tastes, very good with milk"
13+
* addMilk(123); // Returns null
14+
* ```
15+
*/
16+
export function addMilk(
17+
str1: string,
18+
str2: string,
19+
delimiter: string
20+
): string | null {
21+
if (
22+
typeof str1 !== "string" ||
23+
typeof str2 !== "string" ||
24+
typeof delimiter !== "string"
25+
) {
26+
return null;
27+
}
28+
29+
let i = 0;
30+
let j = 0;
31+
let result = "";
32+
33+
while (str1[i] !== undefined) {
34+
result += str1[i];
35+
i++;
36+
}
37+
38+
result += delimiter;
39+
40+
while (str2[j] !== undefined) {
41+
result += str2[j];
42+
j++;
43+
}
44+
45+
return result;
46+
}

src/coolDown.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* Transforms the input string to lowercase.
3+
*
4+
* @param str The input string.
5+
*
6+
* @returns The input string in lowercase, or `null` if the input is not a string.
7+
*
8+
* @example
9+
* ```typescript
10+
* coolDown("ESPRESSO"); // Returns "espresso"
11+
* coolDown(123); // Returns null
12+
* ```
13+
*/
114
export function coolDown(str: string): string | null {
215
if (typeof str !== "string") {
316
return null;

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { addMilk } from "./addMilk";
12
export { brewLength } from "./brewLength";
23
export { compareBeans } from "./compareBeans";
34
export { coolDown } from "./coolDown";

tests/addMilk.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { addMilk } from "../src/index";
2+
3+
test("addMilk should return the two strings concatenated with delimiter in between", () => {
4+
expect(addMilk("coffee tastes", "very good with milk", " ")).toBe(
5+
"coffee tastes very good with milk"
6+
);
7+
expect(addMilk("coffee tastes", "very good with milk", ",")).toBe(
8+
"coffee tastes,very good with milk"
9+
);
10+
expect(addMilk("coffee tastes", "very good with milk", " ! ")).toBe(
11+
"coffee tastes ! very good with milk"
12+
);
13+
});
14+
15+
test("addMilk should return null for non strings", () => {
16+
expect(addMilk(0 as any, 0 as any, " ")).toBeNull();
17+
expect(addMilk("coffee", true as any, " ")).toBeNull();
18+
expect(addMilk({} as any, "coffee", " ")).toBeNull();
19+
expect(addMilk("coffee", "tastes very good", [] as any)).toBeNull();
20+
expect(addMilk(undefined as any, "tastes very good", " ")).toBeNull();
21+
expect(addMilk(null as any, null as any, null as any)).toBeNull();
22+
});

tests/compareBeans.test.ts

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

3-
test("compareBeans should return '>0'", () => {
3+
test("compareBeans should return 1", () => {
44
expect(compareBeans("cappuccino", "americano")).toBe(1);
55
});
66

7-
test("compareBeans should return '<0'", () => {
7+
test("compareBeans should return -1", () => {
88
expect(compareBeans("americano", "cappuccino")).toBe(-1);
99
});
1010

11-
test("compareBeans should return '0'", () => {
11+
test("compareBeans should return 0", () => {
1212
expect(compareBeans("americano", "americano")).toBe(0);
1313
});
1414

0 commit comments

Comments
 (0)