Skip to content

Commit 0f6b3ee

Browse files
author
pr0fix
committed
minor: implement "sip" w/ tests & update README
1 parent 21237ef commit 0f6b3ee

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ To install caffeinated-strings, run the following command:
3131
| `.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). |
3232
| `.toLowerCase()` | `coolDown` | Transforms the string to lowercase (like cooling down a cup of coffee). |
3333
| `.toUpperCase()` | `froth` | Transforms the string to uppercase (like frothing the milk to create a creamy top). |
34+
| `.trim()` | `sip` | Removes whitespace from both ends of string (like sipping away the excess on top). |
3435

3536
## 🔧 Usage
3637

@@ -127,7 +128,7 @@ console.log(refillCup(drink, 3)); // "cappuccino cappuccino cappuccino "
127128

128129
```js
129130
let drink = "ESPRESSO";
130-
console.log(froth(drink)); // "espresso"
131+
console.log(coolDown(drink)); // "espresso"
131132
```
132133

133134
11. `froth(str)`
@@ -138,3 +139,12 @@ console.log(froth(drink)); // "espresso"
138139
let drink = "espresso";
139140
console.log(froth(drink)); // "ESPRESSO"
140141
```
142+
143+
12. `sip(str)`
144+
Removes whitespace from both ends of the string (like sipping away the excess on top).
145+
Returns a new `string` without modifying the original:
146+
147+
```js
148+
let drink = " espresso is lovely!! ";
149+
console.log(sip(drink)); // "espresso is lovely!!"
150+
```

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export { froth } from "./froth";
88
export { hasMilk } from "./hasMilk";
99
export { pourCoffee } from "./pourCoffee";
1010
export { refillCup } from "./refillCup";
11+
export { sip } from "./sip";
1112
export { sipAt } from "./sipAt";

src/refillCup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* @param str The input string
55
* @param count The input count
66
*
7-
* @returns A string with specified number of copies of input string, concatenated together.
7+
* @returns A string with specified number of copies of input string concatenated together,
8+
* or `null` if the inputs are of wrong type.
89
*
910
* @example
1011
* ```typescript

src/sip.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Removes whitespace from both ends of the input string.
3+
*
4+
* @param str The input string
5+
*
6+
* @returns A new string with whitespace removed without modifying the original string,
7+
* or `null` if the input is not a string or is empty.
8+
*
9+
* @example
10+
* ```typescript
11+
* sip(" coffee tastes good! "); // Returns "coffee tastes good!"
12+
* sip(123); // Returns null
13+
* sip(""); // Returns null
14+
* ```
15+
*/
16+
export function sip(str: string): string | null {
17+
if (typeof str !== "string" || str === "") {
18+
return null;
19+
}
20+
21+
let start = 0;
22+
23+
while (
24+
str[start] === " " ||
25+
str[start] === "\t" ||
26+
str[start] === "\n" ||
27+
str[start] === "\r"
28+
) {
29+
start++;
30+
}
31+
32+
let end = 0;
33+
while (str[end] !== undefined) {
34+
end++;
35+
}
36+
37+
end--;
38+
while (
39+
end >= start &&
40+
(str[end] === " " ||
41+
str[end] === "\t" ||
42+
str[end] === "\n" ||
43+
str[end] === "\r")
44+
) {
45+
end--;
46+
}
47+
48+
let result = "";
49+
for (let i = start; i <= end; i++) {
50+
result += str[i];
51+
}
52+
return result;
53+
}

tests/sip.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { sip } from "../src/index";
2+
3+
test("sip should remove whitespace from string", () => {
4+
expect(sip(" coffee ")).toBe("coffee");
5+
});
6+
7+
test("sip should remove whitespace correctly for strings with spaces", () => {
8+
expect(sip(" coffee is what we love! ")).toBe("coffee is what we love!");
9+
expect(sip("we love coffee")).toBe("we love coffee");
10+
});
11+
12+
test("sip should remove whitespace from strings with special characters", () => {
13+
expect(sip(" café ")).toBe("café");
14+
expect(sip(" 123!@# ")).toBe("123!@#");
15+
});
16+
17+
test("sip should return null for an empty string", () => {
18+
expect(sip("")).toBeNull();
19+
});
20+
21+
test("sip should return null for non strings", () => {
22+
expect(sip(0 as any)).toBeNull();
23+
expect(sip(true as any)).toBeNull();
24+
expect(sip({} as any)).toBeNull();
25+
expect(sip([] as any)).toBeNull();
26+
expect(sip(undefined as any)).toBeNull();
27+
expect(sip(null as any)).toBeNull();
28+
});

0 commit comments

Comments
 (0)