Proposal:String-toSpliced

Proposal: GitHub - electroluxcode/proposal-string-toSpliced: This proposal suggests adding a new method in JavaScript to allow for more convenient string manipulation patterns

Hi everyone,

I want to introduce a new proposal for making toSpliced statements for String more flexible in JavaScript. it similar to the toSpliced method of an array

here is some example

let str1 = "Hello world";
let newStr1 = str1.toSpliced(5, 0, "beautiful ");
console.log(newStr1); // Outputs: Hello beautiful world

let str3 = "JavaScript is great";
let newStr3 = str3.toSpliced(10, 5);
console.log(newStr3); // Outputs: JavaScript is

Motivation

in my development work, I often deal with rich text and use the APIs of highlight and range, which provide two properties: startOffset and endOffset. When implementing the function of replacing search highlights, it is necessary to perform replacements based on startIndex, endIndex, and the text provided by the user. I found that without this implementation of toSpliced to string, quite a bit of additional code needs to be written to handle this logic.

and currently, JavaScript developers often rely on a combination of string methods such as substring, slice, and concatenation to achieve substring manipulation. This can lead to complex and error-prone code. The toSpliced method simplifies these operations, making string manipulation more straightforward and reducing the cognitive load on developers.

I've put more details in the GitHub repo. I'm looking for a TC39 member to champion this proposal.

Any feedback or questions are welcome!,thanks!

An issue with the index based string methods is that they are the code-units based and String.prototype.with() - #6 by michael

Also just a note that if this did exist I would expect it to be toSpliced instead of splice because strings are immutable.

2 Likes

Thank you for your reply. changing to tosplice indeed better aligns with the immutability semantics of string . I will update my description and the README on GitHub accordingly. Thanks again.