magic-string
Advanced tools
Comparing version 0.26.3 to 0.26.4
@@ -32,7 +32,6 @@ 'use strict'; | ||
// we make these non-enumerable, for sanity while debugging | ||
Object.defineProperties(this, { | ||
previous: { writable: true, value: null }, | ||
next: { writable: true, value: null }, | ||
}); | ||
{ | ||
this.previous = null; | ||
this.next = null; | ||
} | ||
} | ||
@@ -1048,3 +1047,3 @@ | ||
replace(searchValue, replacement) { | ||
_replaceRegexp(searchValue, replacement) { | ||
function getReplacement(match, str) { | ||
@@ -1072,3 +1071,3 @@ if (typeof replacement === 'string') { | ||
} | ||
if (typeof searchValue !== 'string' && searchValue.global) { | ||
if (searchValue.global) { | ||
const matches = matchAll(searchValue, this.original); | ||
@@ -1094,2 +1093,49 @@ matches.forEach((match) => { | ||
} | ||
_replaceString(string, replacement) { | ||
const { original } = this; | ||
const index = original.indexOf(string); | ||
if (index !== -1) { | ||
this.overwrite(index, index + string.length, replacement); | ||
} | ||
return this; | ||
} | ||
replace(searchValue, replacement) { | ||
if (typeof searchValue === 'string') { | ||
return this._replaceString(searchValue, replacement); | ||
} | ||
return this._replaceRegexp(searchValue, replacement); | ||
} | ||
_replaceAllString(string, replacement) { | ||
const { original } = this; | ||
const stringLength = string.length; | ||
for ( | ||
let index = original.indexOf(string); | ||
index !== -1; | ||
index = original.indexOf(string, index + stringLength) | ||
) { | ||
this.overwrite(index, index + stringLength, replacement); | ||
} | ||
return this; | ||
} | ||
replaceAll(searchValue, replacement) { | ||
if (typeof searchValue === 'string') { | ||
return this._replaceAllString(searchValue, replacement); | ||
} | ||
if (!searchValue.global) { | ||
throw new TypeError( | ||
'MagicString.prototype.replaceAll called with a non-global RegExp argument' | ||
); | ||
} | ||
return this._replaceRegexp(searchValue, replacement); | ||
} | ||
} | ||
@@ -1096,0 +1142,0 @@ |
@@ -34,7 +34,6 @@ (function (global, factory) { | ||
// we make these non-enumerable, for sanity while debugging | ||
Object.defineProperties(this, { | ||
previous: { writable: true, value: null }, | ||
next: { writable: true, value: null }, | ||
}); | ||
{ | ||
this.previous = null; | ||
this.next = null; | ||
} | ||
} | ||
@@ -1102,3 +1101,3 @@ | ||
replace(searchValue, replacement) { | ||
_replaceRegexp(searchValue, replacement) { | ||
function getReplacement(match, str) { | ||
@@ -1126,3 +1125,3 @@ if (typeof replacement === 'string') { | ||
} | ||
if (typeof searchValue !== 'string' && searchValue.global) { | ||
if (searchValue.global) { | ||
const matches = matchAll(searchValue, this.original); | ||
@@ -1148,2 +1147,49 @@ matches.forEach((match) => { | ||
} | ||
_replaceString(string, replacement) { | ||
const { original } = this; | ||
const index = original.indexOf(string); | ||
if (index !== -1) { | ||
this.overwrite(index, index + string.length, replacement); | ||
} | ||
return this; | ||
} | ||
replace(searchValue, replacement) { | ||
if (typeof searchValue === 'string') { | ||
return this._replaceString(searchValue, replacement); | ||
} | ||
return this._replaceRegexp(searchValue, replacement); | ||
} | ||
_replaceAllString(string, replacement) { | ||
const { original } = this; | ||
const stringLength = string.length; | ||
for ( | ||
let index = original.indexOf(string); | ||
index !== -1; | ||
index = original.indexOf(string, index + stringLength) | ||
) { | ||
this.overwrite(index, index + stringLength, replacement); | ||
} | ||
return this; | ||
} | ||
replaceAll(searchValue, replacement) { | ||
if (typeof searchValue === 'string') { | ||
return this._replaceAllString(searchValue, replacement); | ||
} | ||
if (!searchValue.global) { | ||
throw new TypeError( | ||
'MagicString.prototype.replaceAll called with a non-global RegExp argument' | ||
); | ||
} | ||
return this._replaceRegexp(searchValue, replacement); | ||
} | ||
} | ||
@@ -1150,0 +1196,0 @@ |
{ | ||
"name": "magic-string", | ||
"version": "0.26.3", | ||
"version": "0.26.4", | ||
"description": "Modify strings, generate sourcemaps", | ||
@@ -49,3 +49,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@rollup/plugin-node-resolve": "^13.3.0", | ||
"@rollup/plugin-node-resolve": "^14.1.0", | ||
"@rollup/plugin-replace": "^4.0.0", | ||
@@ -55,6 +55,6 @@ "benchmark": "^2.1.4", | ||
"conventional-changelog-cli": "^2.2.2", | ||
"eslint": "^8.23.0", | ||
"eslint": "^8.23.1", | ||
"mocha": "^10.0.0", | ||
"prettier": "^2.7.1", | ||
"rollup": "^2.78.1", | ||
"rollup": "^2.79.1", | ||
"source-map-js": "^1.0.2", | ||
@@ -61,0 +61,0 @@ "source-map-support": "^0.5.21" |
@@ -168,5 +168,5 @@ # magic-string | ||
### s.replace( regexp, substitution ) | ||
### s.replace( regexpOrString, substitution ) | ||
String replacement with RegExp or string, a replacer function is also supported. Returns `this`. | ||
String replacement with RegExp or string. When using a RegExp, replacer function is also supported. Returns `this`. | ||
@@ -187,2 +187,7 @@ ```ts | ||
### s.replaceAll( regexpOrString, substitution ) | ||
Same as `s.replace`, but replace all matched strings instead of just one. | ||
If `substitution` is a regex, then it must have the global (`g`) flag set, or a `TypeError` is thrown. Matches the behavior of the bultin [`String.property.replaceAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll). | ||
### s.remove( start, end ) | ||
@@ -189,0 +194,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
397173
3654
280