Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,4 +18,4 @@ function slugify(str) {
.replace(/^-+|-+$/g, '');
}

module.exports = { capitalize, slugify };
module.exports = { capitalize, titleCase, slugify };
26 changes: 25 additions & 1 deletion tests/strings.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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');
});