Implement the String.prototype.remove

Hi guys!

The motivation behind this proposal is to simplify the text replacement within JavaScript. Currently, to remove some text in a string, you need to use the replace or replaceAll methods passing an empty string as a second parameter. This is an ugly way. So this proposal doesn't change the memory value and removes the second parameter.

How it works

It receives one string or RegExp that returns a new string.

Cases

// Imports omitted…

"bitcoin-mainnet".remove("-mainnet")
// "bitcoin"

"bitcoin-mainnet".remove(/-mainnet/)
// "bitcoin"

"bitcoin mainnet mainnet".remove("mainnet", { trim: true })
// "bitcoin"

"bitcoin mainnet mainnet".remove(/mainnet/, { trim: true })
// "bitcoin"

Install the polyfill

// Use your favorite package manager

npm install @hotequil/proposal-remove-string

Import the polyfill

// Import it in your main, index or app file

import "@hotequil/proposal-remove-string"

Your proposal is motivated by not having to type , "" in a call to replaceAll? And you also want a feature where you can use , { trim: true } instead of .trim()?

1 Like

Hi, @michael!

It's not a good idea to have the .trim(). I removed it.

About the other question, a native .remove() method is more semantic and declarative than using .replace() or .replaceAll(). It explicitly communicates the intent to delete a string, reducing boilerplate and making the code more readable by avoiding 'magic' empty strings.

It's not magic, though - it's literally saying "replace X with nothing". I agree there's a very tiny readability benefit to having "remove", but I'm not sure it's worth it to add something to the language just for that.

1 Like

Yes, that makes sense.