Awesome
proposal-string-trim-characters
Proposal to add argument for .trim()
, .trimStart()
and .trimEnd()
to allow strip the specified characters from strings.
Status
This proposal is a stage-0 proposal and waiting for feedback.
Motivation
We often remove some leading/trailing whitespaces from the beginning or end (or both) of a string by trim
, trimStart
, and trimEnd
.
We can only remove the WhiteSpace and LineTerminator. If you want to remove some other strings who not defined by whitespace. There's no semantical and convenience way to do that.
Semantical & Convenience
The major point of the proposal is semantical and convenience.
For now. How could we remove some specific leading/trailing characters?
- regex
const str = "-_-abc-_-";
const characters = "-_";
const regex = new RegExp(`(^[${characters}]*)|([${characters}]*$)`, 'g')
console.log(str.replaceAll(regex, '')) // abc
- manually
const str = "-_-abc-_-";
const characters = "-_";
let start = 0;
while (characters.indexOf(str[start]) >= 0) {
start += 1;
}
let end = str.length - 1;
while (characters.indexOf(str[end]) >= 0) {
end -= 1;
}
console.log(str.substr(start, end - start + 1)) // abc
As you can see. For these both solutions, there's no idea about what does it doing when you see it, You have to pay attention on it to understand it.
Performance
And for the regex version, there's might also performance issue as TypeScript's implementations: jsbench.
Consolation
That's why we need this proposal. It's will add some semantic and convenience way to clearly representing the operation i want
. And as a possible bonus, it also reduces the amount of very poorly performing code we write.
Core API
Add an optional argument characters
into String.prototype.trim
, String.prototype.trimStart
and String.prototype.trimEnd
.
This argument will allow us which characters will be remove from the start or end (or both) from the string.
The definition of API will looks like:
interface String {
trim(characters?: string): string;
trimStart(characters?: string): string;
trimEnd(characters?: string): string;
}
With this proposal, we could use as:
const str = "-_-abc-_-";
const characters = "-_";
console.log(str.trim(characters)) // abc
console.log(str.trimStart(characters)) // abc-_-
console.log(str.trimEnd(characters)) // -_-abc
Prior art
- Lodash - lodash.trim, lodash.trimStart, lodash.trimEnd
- PHP - function.trim, function.ltrim, function.rtrim
- Python - str.strip, str.lstrip, str.rstrip
- C# - String.Trim, String.TrimStart, String.TrimEnd
- Go - Trim, TrimLeft, TrimRight, TrimPrefix, TrimSuffix
Previous discuss
- https://github.com/tc39/proposal-string-left-right-trim/issues/22
- https://esdiscuss.org/topic/string-trim-chars
- https://esdiscuss.org/topic/string-prototype-trimstart-string-prototype-trimend-with-a-given-string
Proposer
Champions:
- @Kingwl (Wenlu Wang, KWL)