Skip to content

Commit 426479e

Browse files
committed
Add capitalize helper
1 parent 59750a6 commit 426479e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/core/src/helpers/strings.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,20 @@ describe('Strings Helpers', () => {
116116
expect(filterSensitiveInfoFromRepositoryUrl(input)).toBe(expected);
117117
});
118118
});
119+
120+
describe('capitalize', () => {
121+
test.each([
122+
['hello world', 'Hello World'],
123+
['hello', 'Hello'],
124+
['HELLO', 'Hello'],
125+
['hELLO', 'Hello'],
126+
['hELLO wORLD', 'Hello World'],
127+
['hELLO wORLD!', 'Hello World!'],
128+
['hELLO wORLD! 123', 'Hello World! 123'],
129+
['', ''],
130+
])('Should capitalize "%s" => "%s"', async (str, expected) => {
131+
const { capitalize } = await import('@dd/core/helpers/strings');
132+
expect(capitalize(str)).toBe(expected);
133+
});
134+
});
119135
});

packages/core/src/helpers/strings.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,12 @@ export const filterSensitiveInfoFromRepositoryUrl = (repositoryUrl: string = '')
6060
}
6161
};
6262

63+
// Capitalize the first letter of each word in a string.
64+
export const capitalize = (str: string) =>
65+
str
66+
.split(' ')
67+
.map((st) => st.charAt(0).toUpperCase() + st.slice(1).toLowerCase())
68+
.join(' ');
69+
6370
let index = 0;
6471
export const getUniqueId = () => `${Date.now()}.${performance.now()}.${++index}`;

0 commit comments

Comments
 (0)