String.prototype.replace() problems with JSON.stringify() and serialization of Objects

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Jul 29 16:27:54 PDT 2014


that's the side effect of having fat arrow in the house, people need to
pray the parser/engine is smart enough to do not create and trash it every
time. all the time, during the loop or everywhere else is abused ... or,
you simply do what you said already, you reuse code and create the callback
outside the loop once which is the preferred choice since quite ever.

I actually recycle functions in .forEach and Array extras too, I'v never
found it inconvenient or less elegant when the operation is not a one off
that needs closure but a complete, non closure dependent, operation similar
to what generic replacers are.

My .02


On Tue, Jul 29, 2014 at 1:22 PM, Alex Kit <alex.kit at atmajs.com> wrote:

>  split-join to get a literal replace
>
> You really think this is a good design, when for simple string-replace
> one should use string-split to create an array, and afterwards join it?
> This is a hacky workaround.
>
> better familiarity with the existing capabilities
>
> Rick has pointed for a function replacer. It is definitely better, but
> lets assume, you perform this in a loop. I am sure you don’t want to create
> the function on each iteration (especially when performance matters).
>
> for(let line of lines){
>    yield line.replace('foo', () => x);
> }
>
> OK, we can initialize it before and pass it as a delegate. But do anybody
> consider this as a clean and correct way to do simple string replacement?
> And yes, this replacement patterns exist already for ages. But does this
> mean we should not move towards better APIs?
>
> - Alex
>
> ----
>
> From: f.bagnardi at gmail.com
> Date: Tue, 29 Jul 2014 09:49:44 -0700
> Subject: Re: String.prototype.replace() problems with JSON.stringify() and
> serialization of Objects
> To: andrea.giammarchi at gmail.com
> CC: es-discuss at mozilla.org
>
> You can also do a split-join to get a literal replace.
>
>  “I’ll sell this for {cost}.”.split(“{cost}”).join(“$5.00”);
>
>  “I’ll sell this for $5.00.”
>
> On Tue, Jul 29, 2014 at 9:36 AM, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>
> '0101'.replace('0', function(){return 1}) == '1101'
>
> so your last two are the same:
>
> replace(string, callback) -> called one time, using the first indexOf and
> replacing it invoking the callback
> replace(string, string) -> called one time, using the first indexOf and
> replacing it via provided string and checking $
>
>
>
> On Mon, Jul 28, 2014 at 10:29 PM, Christoph Martens <cmartensms at gmail.com>
> wrote:
>
>  On 28.07.2014 17:24, Rick Waldron wrote:
>
>
>
>
> On Mon, Jul 28, 2014 at 11:16 AM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
>
> On 7/28/14, 11:09 AM, Rick Waldron wrote:
>
>    var y = x.replace('{{blob}}', function() {
>      return data;
>    });
>
>
>  In fairness, that's the sort of thing that gives off a "WAT smell". Code
> like this without a comment that explains the indirection is just asking
> someone to "simplify" it, breaking it in the process...
>
>
>  I don't disagree with your feedback, but JS has had special semantics
> for "$" (with ’ or n) character in the replaceValue since ES3 (just short
> of 15 years). I didn't say that the solution was obvious, just that it
> required familiarity ;)
>
>  Rick
>
>
> _______________________________________________
> es-discuss mailing listes-discuss at mozilla.orghttps://mail.mozilla.org/listinfo/es-discuss
>
>
> Hey Rick,
>
> To be honest, I didn't know the trick with the dummy filter function
> returning the plain data. I expected that it was executed each time a match
> was found (similar to Array filter functions).
>
> I think for external guys writing JavaScript, the WAT effect is pretty
> huge. Either we should change the behaviour of the parameters (which is bad
> for legacy implementations) or the flags to disable it optionally.
>
> As I wasn't familiar with the replace behaviour before, I would expect the
> method to behave differently, depending on the arguments:
>
>
> replace(regexp, string) -> parse string and see if $ is in there
> replace(regexp, callback) -> callback is called each time regexp is matched
> replace(string, callback) -> only called once
> replace(string, string) -> called one time, using the first indexOf and
> replacing it
>
>
> ~Chris
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
> *_* es-discuss mailing list es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140729/e5935d5a/attachment-0001.html>


More information about the es-discuss mailing list