-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Issue with "String.prototype.replaceAll()": need Polyfill + Suggestion #9871
Comments
I think Chris Ferdinand's String.replaceAll() polyfill is much easier to understand. /**
* String.prototype.replaceAll() polyfill
* @author Chris Ferdinandi
* @license MIT
*/
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(str, newStr){
// If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
// If a string
return this.replace(new RegExp(str, 'g'), newStr);
};
} |
@digi-booster thanks for the responding.
Also thank you again because the idea made me realize that my way have some issues too:
Result (Easiest for understanding): if (!String.prototype.replaceAll) { // Check if a native function exist
String.prototype.replaceAll = function(search, replace) { // Define the function by closest input names (For good info in consoles)
return this.replace(
(search instanceof RegExp) ? search // Check if the input is already a RegExp then pass it
: new RegExp( String(search).replace(/[.^$*+?()[{|\\]/g, "\\$&") , "g"), // Else, replace all reserved characters with '\' then make a global 'g' RegExp
replace);
}
} Also we can check if the input RegExp is |
For polyfills, I'm ok to link to one as this feature is from 2020, but we shouldn't host it on MDN as we can't really maintain them. So we want is an externally-hosted polyfill that we will link to in the Browser Compatibility section below the table. |
@teoli2003 Thank you. |
Such incomplete and not completely correct in some cases polyfills cause too many problems since someone tries to use them in production. After that, we have issues like this zloirock/core-js#341 |
@zloirock Dear Denis, i am generally agree with you. Also i am not agree to let libraries/plugins touch or block native functions, if their are already exist! |
@PAPIONbit polyfills should fix incorrect implementations that already exist - it's how they work. Your example works not as should work by the spec in too many cases, more other - it adds an enumerable property to Yes, the educational purpose is good - but as I wrote above - too many people try to use polyfills from MDN in production. Yes, introducing a big library is not required in many cases - but if it's not 100% proper by the spec as it could - it should not pollute the prototype for avoiding usage of this code in cases that it could break. It could be a function-helper, but not a property of the |
Yes, If they really can handle it 100% (as you said). In your case could core-js do it? No, just because of a new option in setting properties. My simple suggestion as i said is not a big deal but
I really appreciate your efforts, but the option (Changing native properties) wisely exist till today to if we can, manage native functions for solving problems as fast and easiest that possible, we do it. |
What do you mean?
Not a problem, but only even in the scope of your code that's not exposed outside - overwise, it should maximally follow the standard or it will cause significant problems - I see it too often.
You could ask it, for example, the authors of this proposal, I'm just an implementor. I can assume, for example, that this shows intent more clearly. The main use case, sure, is a string argument.
What do you mean?
You added a link above - it's not a big problem to implement the required abstract ECMAScript operations out of scope
Yes, sure, since now it's missed in the feature detection. However, I don't think that they will do it - it was added only in the final stages.
"Wisely" here is the main word - and it's the main problem that too many developers do not understand what's allowed and what can break the Web. |
@zloirock You got me wrong, Also not friendly. These kind of words will not make the issue more clear, so the subject is finished for me after this:
Bravo that you are wise. Just to respect your response:
So you mean lets we do not use our minds for public exposes? It means you are going to indirectly tell to let not use a library's like Don't give me wrong again, i am not going to say this:
So? :)
No, Sure. Because logically you can not know future. (Years ago, I was telling same things to my friends about why not trust
. |
Rather your answer sounds not friendly. I'm just trying to explain the dangers of naive global polyfills.
Polyfills should not handle such cases since it's a case of an environment broken in the userland. It's strange to expect that if you break your environment, like
Not in the case of something that pollutes global. Just remember why
I mean only that it's a bad practice and dangerous to publish not maximally proper global polyfills. It's fine to publish proper polyfills as packages, for example,
We can be 100% accurate in most cases when no one breaks global in userland, and naive polyfills are a case that could break it.
I even don't know what to say. For example, from adding an enumerable property to
See the link above.
Yes, sure, since it's the answer to your concrete example from the question and I perfectly know how it works in this case. More other, it will break future standard features only if those standards will break the current userland code that's impossible - those features just will not be added in this case. |
@zloirock Dear Denis! If you be honest and calm down, you will get what am i saying. I said the subject between you and me closed for me. Because of expecting a rude behavior that is not depend to your high & professional personality:
! (Actually i am not interested anymore to even talk, but you answered. and i try to focus on parts that is useful.)
(I think you have to check again without prejudice)
If you do not try to caricaturize it (
I was not talking about what we do about future, I mean we have to accept, our hands (As developers in script level) are closed if for example ECMAScript deciders change their mind. That's a fact.
You can never have double standard, If It can not be officially referred / It can have serious bugs and not trusted / It not worth it / ...
You know what to say :), You said well (IT is friendly, Thank you). So:
More? I think these:
Will give me more evidence to it worth looking at the subject again. In case of we accept if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (search, replace) {
return this.replace( Object.prototype.toString.call(x) === "[object String]" ? new RegExp( search.replace(/[.^$*+?()[{|\\]/g, "\\$&"), "g") : search, replace);
}
} Is it?
|
Distributing a non-configurable broken polyfill is bullshit, obviously. I don't understand why this offends you. Too many useless words. We can't call such a broken environment "modified", it's just broken. It is generally recognized as bad practice. Modification of global prototypes is allowed only to maximally proper polyfills. This is a fact and it is not negotiable. I can't understand, what are you trying to show?
Nope, the previous option was better. Just a simple example that will break too much userland code, I already 2 times wrote about enumerability: for (var key in '') console.log(key); // => 'replaceAll' In case you were adding it as a non-enumerable property - I wouldn't even start this discussion. |
This comment has been minimized.
This comment has been minimized.
I do not intend to continue this useless conversation, good luck. |
It seems more appropriate that MDN avoid to mention an external link to a Private/Unofficial library/plugin as polyfill. Because:
Also i think independent polyfills still better be exist in main page of MDN references, but:
My suggested options for
|
As already explained in #9871 (comment), we’re not going to host polyfills any longer in any part of MDN. I’m going ahead and closing this, since the conversation in the comments doesn’t seem to be leading toward a resolution. |
MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
Incomplete
I think the page need a polyfill, a good example:
(Please inform me if it need breakdown and comments)
MDN Content page report details
en-us/web/javascript/reference/global_objects/string/replaceall
The text was updated successfully, but these errors were encountered: