@oxog/string
Version:
Comprehensive string manipulation utilities with zero dependencies
83 lines (82 loc) • 2.46 kB
JavaScript
import { removeAccents as unicodeRemoveAccents } from '../utils/unicode';
export function trim(str, chars) {
if (!str || typeof str !== 'string')
return '';
if (!chars) {
return str.trim();
}
const pattern = new RegExp(`^[${escapeRegExp(chars)}]+|[${escapeRegExp(chars)}]+$`, 'g');
return str.replace(pattern, '');
}
export function trimStart(str, chars) {
if (!str || typeof str !== 'string')
return '';
if (!chars) {
return str.trimStart();
}
const pattern = new RegExp(`^[${escapeRegExp(chars)}]+`, 'g');
return str.replace(pattern, '');
}
export function trimEnd(str, chars) {
if (!str || typeof str !== 'string')
return '';
if (!chars) {
return str.trimEnd();
}
const pattern = new RegExp(`[${escapeRegExp(chars)}]+$`, 'g');
return str.replace(pattern, '');
}
export function removeExtraSpaces(str) {
if (!str || typeof str !== 'string')
return '';
return str.replace(/\s+/g, ' ').trim();
}
export function normalizeWhitespace(str) {
if (!str || typeof str !== 'string')
return '';
return str
.replace(/[\t\n\r\f\v]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
export function removeNonPrintable(str) {
if (!str || typeof str !== 'string')
return '';
return str.replace(/[\x00-\x1F\x7F-\x9F]/g, '');
}
export function stripHtml(str) {
if (!str || typeof str !== 'string')
return '';
return str
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, '')
.replace(/<[^>]*>/g, '')
.replace(/&[#\w]+;/g, (entity) => {
const htmlEntities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
' ': ' ',
'©': '©',
'®': '®',
'™': '™'
};
return htmlEntities[entity] || entity;
});
}
export function stripAnsi(str) {
if (!str || typeof str !== 'string')
return '';
const ansiRegex = /\x1b\[[0-9;]*m/g;
return str.replace(ansiRegex, '');
}
export function removeAccents(str) {
if (!str || typeof str !== 'string')
return '';
return unicodeRemoveAccents(str);
}
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}