Text & Slugs
Text manipulation and slug generation utilities
The text utilities help with string manipulation, slug generation, and formatting.
Importing
import { text } from 'arky-sdk/utils';
Slug Generation
Create Slug
text.slugify('Hello World!');
// Returns: "hello-world"
text.slugify(' Multiple Spaces ');
// Returns: "multiple-spaces"
text.slugify('Café & Résumé');
// Returns: "cafe-resume"
text.slugify('Product (2024 Edition)');
// Returns: "product-2024-edition"
With Options
text.slugify('Hello World', {
separator: '_', // Use underscore
lowercase: true, // Force lowercase
maxLength: 50 // Truncate long slugs
});
// Returns: "hello_world"
Unique Slug
// Generate unique slug from existing slugs
const existingSlugs = ['product', 'product-1', 'product-2'];
text.uniqueSlug('Product', existingSlugs);
// Returns: "product-3"
Text Formatting
Capitalize
text.capitalize('hello world');
// Returns: "Hello world"
text.capitalizeWords('hello world');
// Returns: "Hello World"
Truncate
text.truncate('This is a long text that needs truncating', 20);
// Returns: "This is a long te..."
text.truncate('This is a long text', 20, { suffix: ' [more]' });
// Returns: "This is a long [more]"
text.truncateWords('This is a long text that needs truncating', 4);
// Returns: "This is a long..."
Strip HTML
text.stripHtml('<p>Hello <strong>World</strong></p>');
// Returns: "Hello World"
Escape HTML
text.escapeHtml('<script>alert("xss")</script>');
// Returns: "<script>alert("xss")</script>"
Pluralization
text.pluralize('item', 1);
// Returns: "item"
text.pluralize('item', 5);
// Returns: "items"
text.pluralize('category', 2);
// Returns: "categories"
// Custom plurals
text.pluralize('person', 3, 'people');
// Returns: "people"
With Count
text.pluralizeWithCount('item', 1);
// Returns: "1 item"
text.pluralizeWithCount('item', 5);
// Returns: "5 items"
String Utilities
Camelize
text.camelize('user-name');
// Returns: "userName"
text.camelize('product_category');
// Returns: "productCategory"
Dasherize
text.dasherize('userName');
// Returns: "user-name"
text.dasherize('ProductCategory');
// Returns: "product-category"
Underscore
text.underscore('userName');
// Returns: "user_name"
text.underscore('ProductCategory');
// Returns: "product_category"
Search & Matching
Highlight Search Terms
text.highlight('Hello World', 'world');
// Returns: "Hello <mark>World</mark>"
text.highlight('Hello World', 'world', {
tag: 'strong',
className: 'highlight'
});
// Returns: "Hello <strong class=\"highlight\">World</strong>"
Fuzzy Match
text.fuzzyMatch('hello', 'helo');
// Returns: { match: true, score: 0.8 }
text.fuzzyMatch('product', 'prduct');
// Returns: { match: true, score: 0.85 }
Excerpt Generation
text.excerpt('This is a long paragraph of text...', {
length: 100,
suffix: '...',
preserveWords: true
});
From Markdown
text.excerptFromMarkdown(`
# Heading
This is the **first paragraph** with some content.
## Second Section
More content here.
`, 100);
// Returns: "This is the first paragraph with some content..."
Template Strings
Simple Interpolation
text.interpolate('Hello, {{name}}!', { name: 'John' });
// Returns: "Hello, John!"
text.interpolate('Order #{{orderId}} total: {{total}}', {
orderId: '12345',
total: '$99.99'
});
// Returns: "Order #12345 total: $99.99"
URL Utilities
Extract Domain
text.extractDomain('https://www.example.com/path');
// Returns: "example.com"
Build Query String
text.buildQueryString({
search: 'hello world',
page: 1,
sort: 'newest'
});
// Returns: "search=hello%20world&page=1&sort=newest"
Parse Query String
text.parseQueryString('search=hello&page=2');
// Returns: { search: 'hello', page: '2' }
Complete Example
// Product page helper
function createProductMeta(product: Product) {
const slug = text.slugify(product.name);
const excerpt = text.excerpt(product.description, { length: 160 });
const title = text.capitalizeWords(product.name);
return {
slug,
title,
metaDescription: excerpt,
url: `/products/${slug}`,
breadcrumb: text.capitalizeWords(product.category)
};
}
// Search results highlighting
function highlightSearchResults(results: Product[], query: string) {
return results.map(product => ({
...product,
highlightedName: text.highlight(product.name, query),
highlightedDescription: text.highlight(
text.truncate(product.description, 200),
query
)
}));
}
Tip
Use text.slugify for URL-safe identifiers and text.sanitize for user-generated content to prevent XSS attacks.