MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / replace

Method replace

benchmark/data.js:787–829  ·  view source on GitHub ↗
(searchValue, replacement)

Source from the content-addressed store, hash-verified

785 return this.original !== this.toString();
786 }
787 replace(searchValue, replacement) {
788 function getReplacement(match, str) {
789 if (typeof replacement === 'string') {
790 return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
791 if (i === '$') return '$';
792 if (i === '&') return match[0];
793 const num = +i;
794 if (num < match.length) return match[+i];
795 return `$${i}`;
796 });
797 } else {
798 return replacement(...match, match.index, str, match.groups);
799 }
800 }
801 function matchAll(re, str) {
802 let match;
803 const matches = [];
804 while ((match = re.exec(str))) {
805 matches.push(match);
806 }
807 return matches;
808 }
809 if (typeof searchValue !== 'string' && searchValue.global) {
810 const matches = matchAll(searchValue, this.original);
811 matches.forEach((match) => {
812 if (match.index != null)
813 this.overwrite(
814 match.index,
815 match.index + match[0].length,
816 getReplacement(match, this.original),
817 );
818 });
819 } else {
820 const match = this.original.match(searchValue);
821 if (match && match.index != null)
822 this.overwrite(
823 match.index,
824 match.index + match[0].length,
825 getReplacement(match, this.original),
826 );
827 }
828 return this;
829 }
830}
831const hasOwnProp = Object.prototype.hasOwnProperty;
832class Bundle {

Callers 9

trimEndMethod · 0.45
trimStartMethod · 0.45
indentMethod · 0.45
trimEndAbortedMethod · 0.45
trimStartAbortedMethod · 0.45
getReplacementMethod · 0.45
indentMethod · 0.45
trimStartMethod · 0.45
trimEndMethod · 0.45

Calls 1

overwriteMethod · 0.95

Tested by

no test coverage detected