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οΌ