Strings
trimDashes
1.0.0- Dependencies:
- ∟Direct: 0
- ∟Peer: 0
- Source code ↗
- Check on NPM ↗
Hidden
Overview
Section titled “Overview”Removes leading and trailing dashes from a string while preserving dashes within the string content.
trimDashes(/str/);Features
Section titled “Features”- Removes one or more leading dashes from the beginning
- Removes one or more trailing dashes from the end
- Preserves dashes within the string content
- Returns empty string for invalid inputs
- Handles multiple consecutive dashes
- Simple and intuitive API
- No side effects - does not modify original string
- Comprehensive testing
Arguments
Section titled “Arguments”| Arg | Type | Default Value | Required |
str | string | - | Yes |
Use the trimDashes() function to remove leading and trailing dashes from strings.
import { trimDashes } from '@inpulse-ui/utils';
const result = trimDashes('-hello-world-');console.log(result); // "hello-world"Handles multiple consecutive dashes:
console.log(trimDashes('---start')); // "start"console.log(trimDashes('end---')); // "end"console.log(trimDashes('---both-ends---')); // "both-ends"console.log(trimDashes('----multiple----')); // "multiple"Preserves internal dashes:
console.log(trimDashes('-hello-world-test-')); // "hello-world-test"console.log(trimDashes('--api-key-config--')); // "api-key-config"console.log(trimDashes('-user-profile-settings-')); // "user-profile-settings"Returns empty string for invalid inputs:
console.log(trimDashes('')); // ""console.log(trimDashes(null)); // ""console.log(trimDashes(undefined)); // ""console.log(trimDashes(123)); // ""Handles edge cases:
console.log(trimDashes('-')); // ""console.log(trimDashes('---')); // ""console.log(trimDashes('hello')); // "hello" (no change)console.log(trimDashes('-hello')); // "hello"console.log(trimDashes('hello-')); // "hello"Examples
Section titled “Examples”URL slug cleanup:
const rawSlugs = [ '-blog-post-title-', '--product-name--', '---category-name---', '-user-profile-'];
const cleanSlugs = rawSlugs.map(slug => trimDashes(slug));console.log(cleanSlugs);// ['blog-post-title', 'product-name', 'category-name', 'user-profile']File name processing:
const fileNames = [ '-config-file-.json', '--user-data--.csv', '---backup-file---.sql'];
const cleanFileNames = fileNames.map(fileName => { const [name, ext] = fileName.split('.'); return `${trimDashes(name)}.${ext}`;});
console.log(cleanFileNames);// ['config-file.json', 'user-data.csv', 'backup-file.sql']CSS class name cleanup:
const cssClasses = [ '-btn-primary-', '--nav-item--', '---form-control---', '-text-center-'];
const cleanClasses = cssClasses.map(className => trimDashes(className));console.log(cleanClasses);// ['btn-primary', 'nav-item', 'form-control', 'text-center']Data processing pipeline - cleaning up generated identifiers:
import { trimDashes } from '@inpulse-ui/utils';
// Raw data from API with inconsistent formattingconst apiData = [ { id: '-user-123-', name: 'John Doe' }, { id: '--product-456--', name: 'Widget' }, { id: '---order-789---', name: 'Order #789' }, { id: 'clean-id', name: 'Already Clean' }];
// Clean up the identifiersconst cleanedData = apiData.map(item => ({ ...item, id: trimDashes(item.id), slug: trimDashes(item.name.toLowerCase().replace(/\s+/g, '-'))}));
console.log(cleanedData);// [// { id: 'user-123', name: 'John Doe', slug: 'john-doe' },// { id: 'product-456', name: 'Widget', slug: 'widget' },// { id: 'order-789', name: 'Order #789', slug: 'order-#789' },// { id: 'clean-id', name: 'Already Clean', slug: 'already-clean' }// ]
// Use cleaned data for URLsconst urls = cleanedData.map(item => `/items/${item.id}/${item.slug}`);console.log(urls);// [// '/items/user-123/john-doe',// '/items/product-456/widget',// '/items/order-789/order-#789',// '/items/clean-id/already-clean'// ]Built with by Jo Santana in Brazil.
© 2026 Inpulse. All rights reserved.