-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
06e27d3
commit 8a29028
Showing
5 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Capitalizes the first letter of a string. | ||
* | ||
* @example | ||
* capitalize("hello"); // "Hello" | ||
* capitalize("world"); // "World" | ||
* | ||
* @param {string} input - The string to capitalize. | ||
* | ||
* @returns {string} - The capitalized string. | ||
* | ||
* @throws {TypeError} - If the input is not a string. | ||
* | ||
* @function capitalize | ||
* @memberof Strings | ||
*/ | ||
export function capitalize(input) { | ||
if (typeof input !== "string") throw new TypeError("Input must be a string"); | ||
|
||
if (input.length === 0) return ""; | ||
|
||
return input.charAt(0).toUpperCase() + input.slice(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { capitalize } from "./capitalize.js"; | ||
|
||
|
||
/** | ||
* Functions utils for strings | ||
* | ||
* @namespace Strings | ||
*/ | ||
export { | ||
capitalize | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Strings } from "./src/main"; | ||
const { capitalize } = Strings; | ||
|
||
describe("Strings/capitalize.js", () => { | ||
it("capitalize a lowercase word", () => { | ||
expect(capitalize("beep")).toBe("Beep"); | ||
}); | ||
|
||
it("capitalize an already capitalized word", () => { | ||
expect(capitalize("Boop")).toBe("Boop"); | ||
}); | ||
|
||
it("capitalize an empty string", () => { | ||
expect(capitalize("")).toBe(""); | ||
}); | ||
|
||
it("handle strings with multiple words (only capitalize the first letter)", () => { | ||
expect(capitalize("beep boop")).toBe("Beep boop"); | ||
}); | ||
|
||
it("throw an error for non-string input", () => { | ||
expect(() => capitalize(123)).toThrow(TypeError); | ||
expect(() => capitalize(null)).toThrow(TypeError); | ||
expect(() => capitalize(undefined)).toThrow(TypeError); | ||
expect(() => capitalize({})).toThrow(TypeError); | ||
}); | ||
}); |