Viết hoa các chữ cái đầu mỗi từ trong chuỗi Javascript

Hàm javascript helper (với typescript) ngắn gọn giúp viết hoa (uppercase) các chữ cái đầu mỗi từ trong chuỗi (string) chỉ với 1 dòng code.

#JavaScript version

const uppercaseWords = (str) =>
    str
        .split(' ')
        .map((w) => `${w.charAt(0).toUpperCase()}${w.slice(1)}`)
        .join(' ');

// Or
const uppercaseWords = (str) => str.replace(/^(.)|\s+(.)/g, (c) => c.toUpperCase());

#TypeScript version

const uppercaseWords = (str: string): string =>
    str
        .split(' ')
        .map((w) => `${w.charAt(0).toUpperCase()}${w.slice(1)}`)
        .join(' ');

// Or
const uppercaseWords = (str: string): string => str.replace(/^(.)|\s+(.)/g, (c) => c.toUpperCase());

#Examples

uppercaseWords('hello world'); // 'Hello World'