> It wouldn't necessarily break existing API, since String.prototype.replace currently accepts only RegExp or strings.
Not quite accurate. It accepts anything with a `Symbol.replace` property, or a string. Given that, what you're describing can be implemented as ``` Map.prototype[Symbol.replace] = function(str) { for(const [key, value] of this) { str = str.replace(key, value); } return str; }; ``` > I don't know if the ECMAScript spec mandates preserving a particular order to a Map's elements. It does, so you're good there. > Detecting collisions between matching regular expressions or strings. I think this would be my primary concern, but no so much ordering as expectations. Like if you did ``` "1".replace(new Map([ ['1', '2'], ['2', '3], ]); ``` is the result `2` or `3`? `3` seems surprising to me, at least in the general sense, because there was no `2` in the original input, but it's also hard to see how you'd spec the behavior to avoid that if general regex replacement is supported. On Fri, May 18, 2018 at 9:47 AM, Alex Vincent <ajvinc...@gmail.com> wrote: > Reading [1] in the digests, I think there might actually be an API > improvement that is doable. > > Suppose the String.prototype.replace API allowed passing in a single > argument, a Map instance where the keys were strings or regular expressions > and the values were replacement strings or functions. > > Advantages: > * Shorthand - instead of writing str.replace(a, b).replace(c, > d).replace(e, f)... you get str.replace(regExpMap) > * Reusable - the same regular expression/string map could be used for > several strings (assuming of course the user didn't just abstract the call > into a separate function) > * Modifiable on demand - developers could easily add new regular > expression matches to the map object, or remove them > * It wouldn't necessarily break existing API, since > String.prototype.replace currently accepts only RegExp or strings. > > Disadvantages / reasons not to do it: > * Detecting collisions between matching regular expressions or strings. > If two regular expressions match the same string, or a regular expression > and a search string match, the expected results may vary because a Map's > elements might not be consistently ordered. I don't know if the ECMAScript > spec mandates preserving a particular order to a Map's elements. > - if we preserve the same chaining capability > (str.replace(map1).replace(map2)...), > this might not be a big problem. > > The question is, how often do people chain replace calls together? > > * It's not particularly hard to chain several replace calls together. > It's just verbose, which might not be a high enough burden to overcome for > adding API. > > That's my two cents for the day. Thoughts? > > [1] https://esdiscuss.org/topic/adding-map-directly-to-string-prototype > > -- > "The first step in confirming there is a bug in someone else's work is > confirming there are no bugs in your own." > -- Alexander J. Vincent, June 30, 2001 > > _______________________________________________ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss