excludeChars
A function that excludes characters from a given string according to the specified options.
It is the exact opposite of extractChars, which keeps the specified types: it strips out the specified types instead.
By default, it excludes letters only ({ letters: true }). When options is passed, any option left out is treated as false, so set letters: true explicitly if you also want to exclude letters. The following options can be configured:
letters: Specifies whether to exclude letters from the string. Combining marks (accents, etc.) are excluded together.numbers: Specifies whether to exclude numbers from the string.specialCharacters: Specifies whether to exclude special characters from the string.whiteSpace: Specifies whether to exclude whitespace from the string.
Codeβ
Interfaceβ
typescript
interface ExcludeCharsOptions {
letters?: boolean;
numbers?: boolean;
specialCharacters?: boolean;
whiteSpace?: boolean;
}
function excludeChars(
value: string,
options?: ExcludeCharsOptions // default: { letters: true }
): string;
Usageβ
typescript
import { excludeChars } from '@modern-kit/utils';
const input = 'Hello, δΈη! μλ
νμΈμ 123 γγγ«γ‘γ― $100 + 200 = 300! π';
const withoutLetters = excludeChars(input); // ', ! 123 $100 + 200 = 300! π'
const withoutLettersAndWhiteSpace = excludeChars(input, {
letters: true,
whiteSpace: true,
}); // ',!123$100+200=300!π'
const withoutNumbersAndSpecialCharacters = excludeChars(input, {
numbers: true,
specialCharacters: true,
}); // 'Hello δΈη μλ
νμΈμ γγγ«γ‘γ― '
// Combining marks are not broken apart
excludeChars('JosΓ©'.normalize('NFD'), { numbers: true }); // 'JosΓ©'