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

Reply via email to