Skip to content

Commit

Permalink
feat(#131): add capitalize function (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva authored Jan 20, 2025
1 parent 06e27d3 commit 8a29028
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
----
## [1.4 - New utils](https://github.com/201flaviosilva/utilidades/milestone/3)

### [1.4.2] - XX-XX-2024 (WIP)
### [1.4.2] - XX-XX-2025 (WIP)
#### Added
- `Arrays/isItemInCommon` - Checks if an item is in common between two arrays
- `Arrays/isItemInCommon` - Checks if an item is in common between two arrays;
- `Strings/capitalize` - Capitalizes the first letter of a string;


### [1.4.1] - 16-05-2024
Expand Down
23 changes: 23 additions & 0 deletions src/Strings/capitalize.js
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);
}
11 changes: 11 additions & 0 deletions src/Strings/index.js
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
};
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Arrays from "./Arrays/index.js";
import * as DataStructures from "./DataStructures/index.js";
import * as Maths from "./Maths/index.js";
import * as Physics from "./Physics/index.js";
import * as Strings from "./Strings/index.js";

import { EventSystem, EventSystemInstance } from "./EventSystem.js";
import { Fibonacci, fibonacciCustomSequence, fibonacciSequence, fibonacciUntil, recursiveFibonacci } from "./Fibonacci.js";
Expand Down Expand Up @@ -29,6 +30,7 @@ export {
DataStructures,
Maths,
Physics,
Strings,

// Class
EventSystem, EventSystemInstance,
Expand Down
27 changes: 27 additions & 0 deletions tests/Strings/allEqual.test.js
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);
});
});

0 comments on commit 8a29028

Please sign in to comment.