Skip to content

String Utilities

String utilities provide essential tools to transform, compare, and format strings in a type-safe, ergonomic way. Use these helpers for case conversion, similarity checks, truncation, and more.

📚 Quick Reference

Problem

Implement 📚 quick reference in a production-friendly way with @vielzeug/toolkit while keeping setup and cleanup explicit.

Runnable Example

The snippet below is copy-paste runnable in a TypeScript project with @vielzeug/toolkit installed.

MethodDescription
camelCaseConvert a string to camelCase.
endsWithType-safe suffix check.
escapeEscape HTML entities.
kebabCaseConvert a string to kebab-case.
padPad text to a target length.
pascalCaseConvert a string to PascalCase.
snakeCaseConvert a string to snake_case.
startsWithType-safe prefix check.
titleCaseConvert text to title case.
truncateTruncate a string to a given length with an optional suffix.
unescapeUnescape HTML entities.
wordsSplit text into normalized words.
similarityCompute the similarity score (0 to 1) between two strings.

💡 Practical Examples

Case Conversion

ts
import { camelCase, kebabCase, pascalCase, snakeCase } from '@vielzeug/toolkit';

const input = 'Hello World-from Vielzeug';

camelCase(input); // 'helloWorldFromVielzeug'
kebabCase(input); // 'hello-world-from-vielzeug'
pascalCase(input); // 'HelloWorldFromVielzeug'
snakeCase(input); // 'hello_world_from_vielzeug'

Formatting & Comparison

ts
import { endsWith, escape, pad, similarity, startsWith, titleCase, truncate, unescape, words } from '@vielzeug/toolkit';

// Truncate
const longText = 'Vielzeug is a Swiss-army knife for TypeScript developers.';
truncate(longText, 20); // 'Vielzeug is a Swi...'

// Similarity
similarity('apple', 'apply'); // 0.8
similarity('hello', 'world'); // 0.2

startsWith('vielzeug', 'viel'); // true
endsWith('vielzeug', 'zeug'); // true
pad('hi', 6, '.'); // '..hi..'
titleCase('helloWorld-test_case'); // 'Hello World Test Case'
words('helloWorld-test_case'); // ['hello', 'World', 'test', 'case']
const html = escape('<b>Hi</b>'); // '&lt;b&gt;Hi&lt;/b&gt;'
unescape(html); // '<b>Hi</b>'

🔗 All String Utilities

Expected Output

  • The example runs without type errors in a standard TypeScript setup.
  • The main flow produces the behavior described in the recipe title.

Common Pitfalls

  • Forgetting cleanup/dispose calls can leak listeners or stale state.
  • Skipping explicit typing can hide integration issues until runtime.
  • Not handling error branches makes examples harder to adapt safely.