Files
test-repo/scripts/build/shims/method-replace-all.js
T
fisker Cheungandautofix-ci[bot] 011791fd6c Improve method transform (#18135)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-10-28 02:51:50 +08:00

22 lines
569 B
JavaScript

import { createMethodShim } from "./shared.js";
const stringReplaceAll =
String.prototype.replaceAll ??
function (pattern, replacement) {
// Simple detection to check if pattern is a `RegExp`
if (pattern.global) {
return this.replace(pattern, replacement);
}
// Doesn't work for substitutes, eg `.replaceAll("*", "\\$&")`
return this.split(pattern).join(replacement);
};
const replaceAll = createMethodShim("replaceAll", function () {
if (typeof this === "string") {
return stringReplaceAll;
}
});
export default replaceAll;