From 9bbff2f63df0c58382e302edfa6cb2bf6ba77e44 Mon Sep 17 00:00:00 2001 From: Agent Symphony Date: Tue, 8 Sep 2026 22:04:06 -0500 Subject: [PATCH] Add titleCase utility to capitalize every word --- README.md | 4 +++- src/strings.js | 7 ++++++- tests/strings.test.js | 26 +++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 86b654c..46fcea5 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,10 @@ A tiny, dependency-free collection of everyday JavaScript utility functions — the kind of small helpers every project ends up writing at some point. ```js -const { capitalize, slugify, unique, sum, chunk } = require('pull-toolkit'); +const { capitalize, titleCase, slugify, unique, sum, chunk } = require('pull-toolkit'); capitalize('hello world'); // "Hello world" +titleCase('hello world'); // "Hello World" slugify('Hello, World!'); // "hello-world" unique([1, 2, 2, 3]); // [1, 2, 3] sum([1, 2, 3, 4]); // 10 @@ -39,6 +40,7 @@ npm test ### Strings - `capitalize(str)` — uppercases the first letter of a string. +- `titleCase(str)` — uppercases the first letter of each whitespace-separated word, preserving spacing and the remaining letters. - `slugify(str)` — converts a string into a URL-friendly slug. ### Arrays diff --git a/src/strings.js b/src/strings.js index 57c267c..dcf1b06 100644 --- a/src/strings.js +++ b/src/strings.js @@ -4,6 +4,11 @@ function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); } +/** Capitalizes each whitespace-separated word without changing spacing or other letters. */ +function titleCase(str) { + return str.replace(/\S+/g, capitalize); +} + /** Converts a string into a URL-friendly slug, e.g. "Hello World!" -> "hello-world". */ function slugify(str) { return str @@ -13,4 +18,4 @@ function slugify(str) { .replace(/^-+|-+$/g, ''); } -module.exports = { capitalize, slugify }; +module.exports = { capitalize, titleCase, slugify }; diff --git a/tests/strings.test.js b/tests/strings.test.js index c854b0e..f490e3a 100644 --- a/tests/strings.test.js +++ b/tests/strings.test.js @@ -1,6 +1,6 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { capitalize } = require('../src/strings'); +const { capitalize, titleCase } = require('../src/strings'); test('capitalize uppercases the first letter', () => { assert.equal(capitalize('hello'), 'Hello'); @@ -9,3 +9,27 @@ test('capitalize uppercases the first letter', () => { test('capitalize handles an empty string', () => { assert.equal(capitalize(''), ''); }); + +test('titleCase uppercases the first letter of every word', () => { + assert.equal(titleCase('hello world'), 'Hello World'); + assert.equal(titleCase('hello'), 'Hello'); +}); + +test('titleCase handles empty and whitespace-only strings', () => { + assert.equal(titleCase(''), ''); + assert.equal(titleCase(' \t\n'), ' \t\n'); +}); + +test('titleCase preserves whitespace between and around words', () => { + assert.equal(titleCase(' hello\tworld\nfrom javascript '), ' Hello\tWorld\nFrom Javascript '); +}); + +test('titleCase preserves the remaining letters of each word', () => { + assert.equal(titleCase('javaScript HTML'), 'JavaScript HTML'); +}); + +test('titleCase is available from the package entry point', () => { + const toolkit = require('../src'); + assert.equal(toolkit.titleCase, titleCase); + assert.equal(toolkit.titleCase('hello world'), 'Hello World'); +});