-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add metadata,create capitalizeFirstLetter util + his test
- Loading branch information
Showing
5 changed files
with
82 additions
and
14 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
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,8 @@ | ||
export const capitalizeFirstLetter = (word: string) => { | ||
if (!word) { | ||
throw new Error("String can not be empty."); | ||
} | ||
return ( | ||
word.trim().charAt(0).toUpperCase() + word.trim().slice(1).toLowerCase() | ||
); | ||
}; |
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,29 @@ | ||
import { capitalizeFirstLetter } from "../src/utils"; | ||
|
||
describe("capitalizeFirstLetter utils", () => { | ||
test("capitalize the first letter of a word", () => { | ||
expect(capitalizeFirstLetter("test")).toBe("Test"); | ||
}); | ||
|
||
test("capitalize the first letter and makes other letters lowercase", () => { | ||
expect(capitalizeFirstLetter("tEST")).toBe("Test"); | ||
}); | ||
|
||
test("When string is empty, trigger a new error", () => { | ||
expect(() => { | ||
capitalizeFirstLetter(""); | ||
}).toThrow(); | ||
}); | ||
|
||
test("process strings with leading spaces", () => { | ||
expect(capitalizeFirstLetter(" hello")).toBe("Hello"); | ||
}); | ||
|
||
test("process strings with only one character", () => { | ||
expect(capitalizeFirstLetter("t")).toBe("T"); | ||
}); | ||
|
||
test("keep non-alphabetic characters unchanged at the start", () => { | ||
expect(capitalizeFirstLetter("123test")).toBe("123test"); | ||
}); | ||
}); |