-
Notifications
You must be signed in to change notification settings - Fork 1
/
strUcwords.ts
40 lines (34 loc) · 1.4 KB
/
strUcwords.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import escapeRegExp from './utils/escapeRegExp.js';
export const STR_UCWORDS_CHARS = [' ', '\t', '\r', '\n', '\r\n', '\f', '\v'];
/**
* Uppercase the first character of each word in a string.
*
* @param {string} value - The target string
* @param {string | string[]} [separators=[' ', '\t', '\r', '\n', '\r\n', '\f', '\v']] - The characters used to separate words
* @returns {string}
*
* @example
* ```js
* strUcwords('hello world!'); // 'Hello World!'
* strUcwords('hello|world!'); // 'Hello|world!'
* strUcwords('hello|world!', '|'); // 'Hello|World!'
* strUcwords("mike o'hara"); // "Mike O'hara"
* strUcwords("mike o'hara", "'"); // "Mike o'Hara"
* strUcwords("mike o'hara", [' ', "'"]); // "Mike O'Hara"
* ```
*/
export default function strUcwords(value: string, separators: string | string[] = STR_UCWORDS_CHARS): string {
if (typeof separators === 'string') {
separators = [separators];
}
if (separators.length === 1 && separators[0] === '') {
separators = [' '];
}
/**
* @see https://www.freecodecamp.org/news/how-to-capitalize-words-in-javascript
*
* -> mySentence.replace(/(^\w{1})|(\s+\w{1})/g, letter => letter.toUpperCase());
*/
const regExpStr = separators.reduce((acc, separator) => `${acc}|(${escapeRegExp(separator)}+\\w{1})`, '(^\\w{1})');
return (value + '').replace(new RegExp(regExpStr, 'g'), (letter) => letter.toUpperCase());
}