Files
test-repo/scripts/build/shims/method-replace-all.js
T

22 lines
585 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 = /* @__PURE__ */ createMethodShim("replaceAll", function () {
if (typeof this === "string") {
return stringReplaceAll;
}
});
export default replaceAll;