Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hub-common): have capitalize() handle the empty string, change titlelize() to stop stripping white space #1084

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/common/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,11 @@ export const isNil = (value: unknown) => value == null;
* @returns Word
*/
export const capitalize = (word: string) => {
// upper case first letter and return as element in array for backwards compatibility
const chars = Array.from(word);
chars[0] = chars[0].toUpperCase();
// account for the empty string
if (chars.length) {
chars[0] = chars[0].toUpperCase();
}
return chars.join("");
};

Expand Down
1 change: 0 additions & 1 deletion packages/common/src/utils/titleize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { capitalize } from "../util";
*/
export function titleize(value: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this function isn't used anywhere except the gallery component

return value
.replace(/\s\s+/g, " ")
.split(" ")
.map((k) => capitalize(k))
.join(" ");
Expand Down
2 changes: 1 addition & 1 deletion packages/common/test/utils/titleize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe("titleize", function () {
it("capitalizes every word in a sentence", function () {
expect(titleize("hello")).toBe("Hello");
expect(titleize("hello world")).toBe("Hello World");
expect(titleize("hello world")).toBe("Hello World");
expect(titleize("hello world")).toBe("Hello World");
expect(titleize("helloworld")).toBe("Helloworld");
expect(titleize("hello-world")).toBe("Hello-world");
});
Expand Down