3

As you can see in the picture bellow there is an unexpected behavior of the trim() function, because is stripping the á

enter image description here

This code is part of javascript file that I've included in many projects without problems, but now a client of mine is facing this issue. The sad part is that i haven't access to the source code project client.

Questions

  1. Is it posible to override the implementation of native trim() function in Javascript?
  2. What could the reasons of this behavior?
Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • 4
    Where are you running this? I tried in chrome, firefox and Node.js and it seems to work for me. – Viktor W May 13 '20 at 11:06
  • 1
    not reproducible in chrome `"olá".trim()` btw... – Denis Tsoi May 13 '20 at 11:07
  • 2
    It is possible to override the `String.prototype.trim` method, but it is **highly discouraged** - https://stackoverflow.com/questions/6885404/javascript-override-methods. Perhaps that's what's happening here and the original author did just that, and it's exactly why you shouldn't do it. – goto May 13 '20 at 11:09
  • 1
    To add to @goto1's comment: using `String.prototype.trim = () => 'Hello'` returns only `Hello` when you use `trim`. – mahalde May 13 '20 at 11:10
  • 3
    Could you try adding the following code right before trim: `String.prototype.trim = function () { return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); };` - it polyfills the correct "trim" function, which works for me. If it works with this, you know you have a bad trim for some reason. – Viktor W May 13 '20 at 11:11
  • Could you search for `String.prototype.trim` in the project and check if any overrides are added? – adiga May 13 '20 at 11:29
  • function myTrim(x) { return x.replace(/^\s+|\s+$/gm,''); } function myFunction() { var str = myTrim(" Hello World! "); alert(str); } – Devanshu May 13 '20 at 11:32

1 Answers1

3

Trying your code in a regular browser show that it works as expected - jsfiddle.

Is it possible to override the implementation of native trim() function in JavaScript?

Yes it is possible but it is considered a bad practice. I'll show how to do that because I think the code has it overridden already. Here is how to do it:

String.prototype.trim = x => console.log('trim override');
'olá'.trim(); // trim override

What could be the reasons for this behavior?

Unless it's run in an unexpected environment it is most likely that the original author has trim overridden to something that is different from native implementation. I would suggest you try to paste the polyfill with original implementation in to the console and checking if running it with that will fix the issue.

Here is the polyfill:

String.prototype.trim = function () {
  return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
console.log('olá'.trim());

If the output of this code gives you the correct result (not trimming á) you found the issue.

Timofey Biryukov
  • 379
  • 3
  • 13
  • 3
    I would suggest figuring out where and why the polyfill was made, and fixing it from the root. This could break existing code if they wanted this behaviour. – Viktor W May 13 '20 at 11:38