Skip to content

Commit 5f2dba7

Browse files
authored
feat: new hasChanged method (#202)
1 parent cd74ea2 commit 5f2dba7

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ The returned sourcemap has two (non-enumerable) methods attached for convenience
117117
code += '\n//# sourceMappingURL=' + map.toUrl();
118118
```
119119

120+
### s.hasChanged()
121+
122+
Indicates if the string has been changed.
123+
120124
### s.indent( prefix[, options] )
121125

122126
Prefixes each line of the string with `prefix`. If `prefix` is not supplied, the indentation will be guessed from the original content, falling back to a single tab character. Returns `this`.

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ export default class MagicString {
217217
isEmpty(): boolean;
218218
length(): number;
219219

220+
/**
221+
* Indicates if the string has been changed.
222+
*/
223+
hasChanged(): boolean;
224+
220225
original: string;
221226
/**
222227
* Returns the generated string.

src/MagicString.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,10 @@ export default class MagicString {
713713
return this;
714714
}
715715

716+
hasChanged() {
717+
return this.original !== this.toString();
718+
}
719+
716720
replace(searchValue, replacement) {
717721
function getReplacement(match) {
718722
if (typeof replacement === 'string') {

test/MagicString.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,26 @@ describe('MagicString', () => {
12781278
});
12791279
});
12801280

1281+
describe('hasChanged', () => {
1282+
it('should works', () => {
1283+
const s = new MagicString(' abcde fghijkl ');
1284+
1285+
assert.ok(!s.hasChanged());
1286+
1287+
assert.ok(s.clone().prepend(' ').hasChanged());
1288+
assert.ok(s.clone().overwrite(1, 2, 'b').hasChanged());
1289+
assert.ok(s.clone().remove(1, 6).hasChanged());
1290+
1291+
s.trim();
1292+
1293+
assert.ok(s.hasChanged());
1294+
1295+
const clone = s.clone();
1296+
1297+
assert.ok(clone.hasChanged());
1298+
});
1299+
});
1300+
12811301
describe('replace', () => {
12821302
it('works with string replace', () => {
12831303
const code = '1 2 1 2';

0 commit comments

Comments
 (0)