Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't change the behavior of String.prototype.replaceAll globally #17

Open
kevinoid opened this issue Feb 16, 2024 · 0 comments
Open

Don't change the behavior of String.prototype.replaceAll globally #17

kevinoid opened this issue Feb 16, 2024 · 0 comments

Comments

@kevinoid
Copy link

jgeXml unconditionally overwrites String.prototype.replaceAll with its own version, which does not handle a Function as its second argument according to the spec:

jgeXml/common.js

Lines 3 to 9 in 82cb8c7

Object.defineProperty(String.prototype,'replaceAll',{
value: function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
},
enumerable: false}
);

This can break other code in the same project in interesting ways. For example, running the code

console.log('Before:', 'example'.replaceAll('e', (match, offset) => offset ? 'e!' : 'E'));
require('jgexml');
console.log('After:', 'example'.replaceAll('e', (match, offset) => offset ? 'e!' : 'E'));

prints

Before: Example!
After: (match, offset) => offset ? 'e!' : 'E'xampl(match, offset) => offset ? 'e!' : 'E'

Note that jgexml is not used or called, simply required. This is surprising and was frustrating to track down.

Could you consider not modifying String.prototype.replaceAll (or any global variables or their properties) or, if you must, only polyfilling it when absent and using a spec-conformant polyfill?

Thanks for considering,
Kevin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant