CNCEC_APP/uni_modules/uview-pro/libs/function/trim.ts

22 lines
624 B
TypeScript
Raw Normal View History

2026-03-25 14:54:15 +08:00
/**
*
* @param str
* @param pos 'both' | 'left' | 'right' | 'all''both'
* @returns
*/
function trim(str: string, pos: 'both' | 'left' | 'right' | 'all' = 'both'): string {
if (pos === 'both') {
return str.replace(/^\s+|\s+$/g, '');
} else if (pos === 'left') {
return str.replace(/^\s*/, '');
} else if (pos === 'right') {
return str.replace(/(\s*$)/g, '');
} else if (pos === 'all') {
return str.replace(/\s+/g, '');
} else {
return str;
}
}
export default trim;