Skip to content

Commit a3a7d76

Browse files
committed
feat(utils): add toTitleCase string util
1 parent b7772db commit a3a7d76

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

electron/common/string/__tests__/string-utils.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isBlank,
66
isEmpty,
77
sliceStart,
8+
toTitleCase,
89
toUpperSnakeCase,
910
unescapeEntities,
1011
} from '../string.utils.js';
@@ -183,6 +184,16 @@ describe('string-utils', () => {
183184
});
184185
});
185186

187+
describe('#toTitleCase', () => {
188+
it('returns the value in title case', () => {
189+
const value = 'foo bar baz';
190+
191+
const result = toTitleCase(value);
192+
193+
expect(result).toEqual('Foo Bar Baz');
194+
});
195+
});
196+
186197
describe('#sliceStart', () => {
187198
it('returns the original string, the matched pattern, and the remaining string when the pattern is found at the start of the string', () => {
188199
const text = 'foo bar baz';

electron/common/string/string.utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import snakeCase from 'lodash-es/snakeCase.js';
2+
import startCase from 'lodash-es/startCase.js';
23
import type { Maybe } from '../types.js';
34

45
/**
@@ -103,6 +104,13 @@ export const toUpperSnakeCase = (value: string): string => {
103104
return snakeCase(value).toUpperCase();
104105
};
105106

107+
/**
108+
* Returns the text, uppercasing the first letter of each word.
109+
*/
110+
export const toTitleCase = (value: string): string => {
111+
return startCase(value.toLowerCase());
112+
};
113+
106114
/**
107115
* Inspired by Ruby's String#slice method.
108116
* Slices the pattern from the start of the input text.

0 commit comments

Comments
 (0)