String Utilities
Overview
Section titled “Overview”String utilities provide functions for manipulating and formatting text strings in your game. These utilities are particularly useful for displaying text, formatting messages, and handling user input.
String Operations
Section titled “String Operations”Capitalizing Text
Section titled “Capitalizing Text”Capitalize the first letter of a string:
import { capitalize } from 'dill-pixel';
// Capitalize first letterconst text = capitalize('hello'); // "Hello"const name = capitalize('john'); // "John"
Capitalizing Words
Section titled “Capitalizing Words”Capitalize the first letter of each word in a string:
import { capitalizeWords } from 'dill-pixel';
// Capitalize each wordconst title = capitalizeWords('game over'); // "Game Over"const name = capitalizeWords('john doe'); // "John Doe"
Common Use Cases
Section titled “Common Use Cases”UI Text Formatting
Section titled “UI Text Formatting”import { capitalize, capitalizeWords } from 'dill-pixel';
class TextFormatter { // Format menu items formatMenuItem(text: string) { return capitalize(text); }
// Format dialog titles formatTitle(text: string) { return capitalizeWords(text); }}
Message System
Section titled “Message System”import { capitalize } from 'dill-pixel';
class MessageSystem { formatMessage(type: string, message: string) { return `${capitalize(type)}: ${message}`; }
// Example usage: displayNotification() { return this.formatMessage('info', 'level complete'); // "Info: level complete" }
displayError() { return this.formatMessage('error', 'connection lost'); // "Error: connection lost" }}
Name Generation
Section titled “Name Generation”import { capitalize, capitalizeWords } from 'dill-pixel';
class NameGenerator { generateCharacterName(prefix: string, title: string) { return `${capitalize(prefix)} ${capitalizeWords(title)}`; }
// Example usage: generateName() { return this.generateCharacterName('sir', 'dragon slayer'); // "Sir Dragon Slayer" }}
Best Practices
Section titled “Best Practices”-
Use appropriate function for your needs:
// Good: Using capitalize for single wordsconst name = capitalize('hero'); // "Hero"// Good: Using capitalizeWords for titlesconst title = capitalizeWords('the final battle'); // "The Final Battle" -
Create helper functions for common formats:
function formatGameTitle(name: string) {return capitalizeWords(name.toLowerCase());}function formatPlayerName(name: string) {return capitalize(name.trim());}
Performance Considerations
Section titled “Performance Considerations”Efficient Usage
Section titled “Efficient Usage”// Less efficient: Formatting on every framefunction update() { displayText(capitalizeWords(title)); // Don't do this}
// More efficient: Cache formatted textclass TextDisplay { private formattedTitle: string;
setTitle(title: string) { this.formattedTitle = capitalizeWords(title); }
render() { displayText(this.formattedTitle); }}
Tips and Tricks
Section titled “Tips and Tricks”-
Combine with other utilities:
import { capitalize, getZeroPaddedNumber } from 'dill-pixel';function formatLevel(level: number) {return `${capitalize('level')} ${getZeroPaddedNumber(level, 2)}`;} -
Create consistent text styles:
const TextStyles = {TITLE: (text: string) => capitalizeWords(text),MENU_ITEM: (text: string) => capitalize(text),DIALOG: (text: string) => capitalize(text.toLowerCase()),}; -
Handle multi-line text:
function formatParagraph(text: string) {return text.split('\n').map((line) => capitalize(line)).join('\n');}